Code

commit: suggest --amend --reset-author to fix commiter identity
[git.git] / notes.c
diff --git a/notes.c b/notes.c
index cc92cf351a5d8abb7ad2890660d484644c359dfb..70d00135eb5b67cd6f21b416cde2ae4a1967fd0a 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -263,11 +263,13 @@ static int note_tree_consolidate(struct int_node *tree,
  * To remove a leaf_node:
  * Search to the tree location appropriate for the given leaf_node's key:
  * - If location does not hold a matching entry, abort and do nothing.
+ * - Copy the matching entry's value into the given entry.
  * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
  * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
  */
-static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
-               unsigned char n, struct leaf_node *entry)
+static void note_tree_remove(struct notes_tree *t,
+               struct int_node *tree, unsigned char n,
+               struct leaf_node *entry)
 {
        struct leaf_node *l;
        struct int_node *parent_stack[20];
@@ -282,6 +284,7 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
                return; /* key mismatch, nothing to remove */
 
        /* we have found a matching entry */
+       hashcpy(entry->val_sha1, l->val_sha1);
        free(l);
        *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
 
@@ -838,7 +841,7 @@ static int string_list_add_one_ref(const char *path, const unsigned char *sha1,
 {
        struct string_list *refs = cb;
        if (!unsorted_string_list_has_string(refs, path))
-               string_list_append(path, refs);
+               string_list_append(refs, path);
        return 0;
 }
 
@@ -851,7 +854,7 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
                if (get_sha1(glob, sha1))
                        warning("notes ref %s is invalid", glob);
                if (!unsorted_string_list_has_string(list, glob))
-                       string_list_append(glob, list);
+                       string_list_append(list, glob);
        }
 }
 
@@ -877,14 +880,6 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
        strbuf_release(&globbuf);
 }
 
-static int string_list_add_refs_from_list(struct string_list_item *item,
-                                         void *cb)
-{
-       struct string_list *list = cb;
-       string_list_add_refs_by_glob(list, item->string);
-       return 0;
-}
-
 static int notes_display_config(const char *k, const char *v, void *cb)
 {
        int *load_refs = cb;
@@ -947,30 +942,18 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
        load_subtree(t, &root_tree, t->root, 0);
 }
 
-struct load_notes_cb_data {
-       int counter;
-       struct notes_tree **trees;
-};
-
-static int load_one_display_note_ref(struct string_list_item *item,
-                                    void *cb_data)
-{
-       struct load_notes_cb_data *c = cb_data;
-       struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
-       init_notes(t, item->string, combine_notes_ignore, 0);
-       c->trees[c->counter++] = t;
-       return 0;
-}
-
 struct notes_tree **load_notes_trees(struct string_list *refs)
 {
+       struct string_list_item *item;
+       int counter = 0;
        struct notes_tree **trees;
-       struct load_notes_cb_data cb_data;
        trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
-       cb_data.counter = 0;
-       cb_data.trees = trees;
-       for_each_string_list(load_one_display_note_ref, refs, &cb_data);
-       trees[cb_data.counter] = NULL;
+       for_each_string_list_item(item, refs) {
+               struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
+               init_notes(t, item->string, combine_notes_ignore, 0);
+               trees[counter++] = t;
+       }
+       trees[counter] = NULL;
        return trees;
 }
 
@@ -983,7 +966,7 @@ void init_display_notes(struct display_notes_opt *opt)
        assert(!display_notes_trees);
 
        if (!opt || !opt->suppress_default_notes) {
-               string_list_append(default_notes_ref(), &display_notes_refs);
+               string_list_append(&display_notes_refs, default_notes_ref());
                display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
                if (display_ref_env) {
                        string_list_add_refs_from_colon_sep(&display_notes_refs,
@@ -995,10 +978,12 @@ void init_display_notes(struct display_notes_opt *opt)
 
        git_config(notes_display_config, &load_config_refs);
 
-       if (opt && opt->extra_notes_refs)
-               for_each_string_list(string_list_add_refs_from_list,
-                                    opt->extra_notes_refs,
-                                    &display_notes_refs);
+       if (opt && opt->extra_notes_refs) {
+               struct string_list_item *item;
+               for_each_string_list_item(item, opt->extra_notes_refs)
+                       string_list_add_refs_by_glob(&display_notes_refs,
+                                                    item->string);
+       }
 
        display_notes_trees = load_notes_trees(&display_notes_refs);
        string_list_clear(&display_notes_refs, 0);
@@ -1021,17 +1006,20 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
        note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
 }
 
-void remove_note(struct notes_tree *t, const unsigned char *object_sha1)
+int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
 {
        struct leaf_node l;
 
        if (!t)
                t = &default_notes_tree;
        assert(t->initialized);
-       t->dirty = 1;
        hashcpy(l.key_sha1, object_sha1);
        hashclr(l.val_sha1);
        note_tree_remove(t, t->root, 0, &l);
+       if (is_null_sha1(l.val_sha1)) // no note was removed
+               return 1;
+       t->dirty = 1;
+       return 0;
 }
 
 const unsigned char *get_note(struct notes_tree *t,
@@ -1083,7 +1071,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)
        return ret;
 }
 
-void prune_notes(struct notes_tree *t)
+void prune_notes(struct notes_tree *t, int flags)
 {
        struct note_delete_list *l = NULL;
 
@@ -1094,7 +1082,10 @@ void prune_notes(struct notes_tree *t)
        for_each_note(t, 0, prune_notes_helper, &l);
 
        while (l) {
-               remove_note(t, l->sha1);
+               if (flags & NOTES_PRUNE_VERBOSE)
+                       printf("%s\n", sha1_to_hex(l->sha1));
+               if (!(flags & NOTES_PRUNE_DRYRUN))
+                       remove_note(t, l->sha1);
                l = l->next;
        }
 }