Code

lstat_cache: optionally return match_len
[git.git] / notes.c
diff --git a/notes.c b/notes.c
index 197824439826881fc0de3275353ec1b74d86cfa3..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);
 
@@ -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(refs, load_one_display_note_ref, &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;
 }
 
@@ -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(opt->extra_notes_refs,
-                                    string_list_add_refs_from_list,
-                                    &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,