From a97a74686d70a318cd802003498054cc1e8b0ae2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 9 Oct 2009 12:21:57 +0200 Subject: Introduce commit notes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit notes are blobs which are shown together with the commit message. These blobs are taken from the notes ref, which you can configure by the config variable core.notesRef, which in turn can be overridden by the environment variable GIT_NOTES_REF. The notes ref is a branch which contains "files" whose names are the names of the corresponding commits (i.e. the SHA-1). The rationale for putting this information into a ref is this: we want to be able to fetch and possibly union-merge the notes, maybe even look at the date when a note was introduced, and we want to store them efficiently together with the other objects. This patch has been improved by the following contributions: - Thomas Rast: fix core.notesRef documentation - Tor Arne Vestbø: fix printing of multi-line notes - Alex Riesen: Using char array instead of char pointer costs less BSS - Johan Herland: Plug leak when msg is good, but msglen or type causes return Signed-off-by: Johannes Schindelin Signed-off-by: Thomas Rast Signed-off-by: Tor Arne Vestbø Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano get_commit_notes(): Plug memory leak when 'if' triggers, but not because of read_sha1_file() failure --- notes.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 notes.c (limited to 'notes.c') diff --git a/notes.c b/notes.c new file mode 100644 index 0000000000..66379ffd22 --- /dev/null +++ b/notes.c @@ -0,0 +1,70 @@ +#include "cache.h" +#include "commit.h" +#include "notes.h" +#include "refs.h" +#include "utf8.h" +#include "strbuf.h" + +static int initialized; + +void get_commit_notes(const struct commit *commit, struct strbuf *sb, + const char *output_encoding) +{ + static const char utf8[] = "utf-8"; + struct strbuf name = STRBUF_INIT; + unsigned char sha1[20]; + char *msg, *msg_p; + unsigned long linelen, msglen; + enum object_type type; + + if (!initialized) { + const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT); + if (env) + notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT); + else if (!notes_ref_name) + notes_ref_name = GIT_NOTES_DEFAULT_REF; + if (notes_ref_name && read_ref(notes_ref_name, sha1)) + notes_ref_name = NULL; + initialized = 1; + } + + if (!notes_ref_name) + return; + + strbuf_addf(&name, "%s:%s", notes_ref_name, + sha1_to_hex(commit->object.sha1)); + if (get_sha1(name.buf, sha1)) + return; + + if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen || + type != OBJ_BLOB) { + free(msg); + return; + } + + if (output_encoding && *output_encoding && + strcmp(utf8, output_encoding)) { + char *reencoded = reencode_string(msg, output_encoding, utf8); + if (reencoded) { + free(msg); + msg = reencoded; + msglen = strlen(msg); + } + } + + /* we will end the annotation by a newline anyway */ + if (msglen && msg[msglen - 1] == '\n') + msglen--; + + strbuf_addstr(sb, "\nNotes:\n"); + + for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) { + linelen = strchrnul(msg_p, '\n') - msg_p; + + strbuf_addstr(sb, " "); + strbuf_add(sb, msg_p, linelen); + strbuf_addch(sb, '\n'); + } + + free(msg); +} -- cgit v1.2.1 From fd53c9eb445815696bf84c4701b9af73b5d7f50d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 9 Oct 2009 12:21:59 +0200 Subject: Speed up git notes lookup To avoid looking up each and every commit in the notes ref's tree object, which is very expensive, speed things up by slurping the tree object's contents into a hash_map. The idea for the hashmap singleton is from David Reiss, initial benchmarking by Jeff King. Note: the implementation allows for arbitrary entries in the notes tree object, ignoring those that do not reference a valid object. This allows you to annotate arbitrary branches, or objects. This patch has been improved by the following contributions: - Junio C Hamano: fixed an obvious error in initialize_hash_map() Signed-off-by: Johannes Schindelin Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 102 insertions(+), 10 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 66379ffd22..2b66723f5f 100644 --- a/notes.c +++ b/notes.c @@ -4,15 +4,112 @@ #include "refs.h" #include "utf8.h" #include "strbuf.h" +#include "tree-walk.h" + +struct entry { + unsigned char commit_sha1[20]; + unsigned char notes_sha1[20]; +}; + +struct hash_map { + struct entry *entries; + off_t count, size; +}; static int initialized; +static struct hash_map hash_map; + +static int hash_index(struct hash_map *map, const unsigned char *sha1) +{ + int i = ((*(unsigned int *)sha1) % map->size); + + for (;;) { + unsigned char *current = map->entries[i].commit_sha1; + + if (!hashcmp(sha1, current)) + return i; + + if (is_null_sha1(current)) + return -1 - i; + + if (++i == map->size) + i = 0; + } +} + +static void add_entry(const unsigned char *commit_sha1, + const unsigned char *notes_sha1) +{ + int index; + + if (hash_map.count + 1 > hash_map.size >> 1) { + int i, old_size = hash_map.size; + struct entry *old = hash_map.entries; + + hash_map.size = old_size ? old_size << 1 : 64; + hash_map.entries = (struct entry *) + xcalloc(sizeof(struct entry), hash_map.size); + + for (i = 0; i < old_size; i++) + if (!is_null_sha1(old[i].commit_sha1)) { + index = -1 - hash_index(&hash_map, + old[i].commit_sha1); + memcpy(hash_map.entries + index, old + i, + sizeof(struct entry)); + } + free(old); + } + + index = hash_index(&hash_map, commit_sha1); + if (index < 0) { + index = -1 - index; + hash_map.count++; + } + + hashcpy(hash_map.entries[index].commit_sha1, commit_sha1); + hashcpy(hash_map.entries[index].notes_sha1, notes_sha1); +} + +static void initialize_hash_map(const char *notes_ref_name) +{ + unsigned char sha1[20], commit_sha1[20]; + unsigned mode; + struct tree_desc desc; + struct name_entry entry; + void *buf; + + if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) || + get_tree_entry(commit_sha1, "", sha1, &mode)) + return; + + buf = fill_tree_descriptor(&desc, sha1); + if (!buf) + die("Could not read %s for notes-index", sha1_to_hex(sha1)); + + while (tree_entry(&desc, &entry)) + if (!get_sha1(entry.path, commit_sha1)) + add_entry(commit_sha1, entry.sha1); + free(buf); +} + +static unsigned char *lookup_notes(const unsigned char *commit_sha1) +{ + int index; + + if (!hash_map.size) + return NULL; + + index = hash_index(&hash_map, commit_sha1); + if (index < 0) + return NULL; + return hash_map.entries[index].notes_sha1; +} void get_commit_notes(const struct commit *commit, struct strbuf *sb, const char *output_encoding) { static const char utf8[] = "utf-8"; - struct strbuf name = STRBUF_INIT; - unsigned char sha1[20]; + unsigned char *sha1; char *msg, *msg_p; unsigned long linelen, msglen; enum object_type type; @@ -23,17 +120,12 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb, notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT); else if (!notes_ref_name) notes_ref_name = GIT_NOTES_DEFAULT_REF; - if (notes_ref_name && read_ref(notes_ref_name, sha1)) - notes_ref_name = NULL; + initialize_hash_map(notes_ref_name); initialized = 1; } - if (!notes_ref_name) - return; - - strbuf_addf(&name, "%s:%s", notes_ref_name, - sha1_to_hex(commit->object.sha1)); - if (get_sha1(name.buf, sha1)) + sha1 = lookup_notes(commit->object.sha1); + if (!sha1) return; if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen || -- cgit v1.2.1 From c56fcc89b951f3e8c9240ea02676b2eef5417da6 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Fri, 9 Oct 2009 12:22:04 +0200 Subject: Add flags to get_commit_notes() to control the format of the note string This patch adds the following flags to get_commit_notes() for adjusting the format of the produced note string: - NOTES_SHOW_HEADER: Print "Notes:" line before the notes contents - NOTES_INDENT: Indent notes contents by 4 spaces Suggested-by: Johannes Schindelin Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 2b66723f5f..b7d79e1900 100644 --- a/notes.c +++ b/notes.c @@ -106,7 +106,7 @@ static unsigned char *lookup_notes(const unsigned char *commit_sha1) } void get_commit_notes(const struct commit *commit, struct strbuf *sb, - const char *output_encoding) + const char *output_encoding, int flags) { static const char utf8[] = "utf-8"; unsigned char *sha1; @@ -148,12 +148,14 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb, if (msglen && msg[msglen - 1] == '\n') msglen--; - strbuf_addstr(sb, "\nNotes:\n"); + if (flags & NOTES_SHOW_HEADER) + strbuf_addstr(sb, "\nNotes:\n"); for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) { linelen = strchrnul(msg_p, '\n') - msg_p; - strbuf_addstr(sb, " "); + if (flags & NOTES_INDENT) + strbuf_addstr(sb, " "); strbuf_add(sb, msg_p, linelen); strbuf_addch(sb, '\n'); } -- cgit v1.2.1 From 27d57564102a98950bf4398daeeb14a15154478f Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Fri, 9 Oct 2009 12:22:06 +0200 Subject: Teach notes code to free its internal data structures on request There's no need to be rude to memory-concious callers... This patch has been improved by the following contributions: - Junio C Hamano: avoid old-style declaration Signed-off-by: Junio C Hamano Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index b7d79e1900..a5d888d772 100644 --- a/notes.c +++ b/notes.c @@ -105,6 +105,13 @@ static unsigned char *lookup_notes(const unsigned char *commit_sha1) return hash_map.entries[index].notes_sha1; } +void free_notes(void) +{ + free(hash_map.entries); + memset(&hash_map, 0, sizeof(struct hash_map)); + initialized = 0; +} + void get_commit_notes(const struct commit *commit, struct strbuf *sb, const char *output_encoding, int flags) { -- cgit v1.2.1 From 23123aecf8418a6b0ec23378555ed78c438ae894 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Fri, 9 Oct 2009 12:22:07 +0200 Subject: Teach the notes lookup code to parse notes trees with various fanout schemes The semantics used when parsing notes trees (with regards to fanout subtrees) follow Dscho's proposal fairly closely: - No concatenation/merging of notes is performed. If there are several notes objects referencing a given commit, only one of those objects are used. - If a notes object for a given commit is present in the "root" notes tree, no subtrees are consulted; the object in the root tree is used directly. - If there are more than one subtree that prefix-matches the given commit, only the subtree with the longest matching prefix is consulted. This means that if the given commit is e.g. "deadbeef", and the notes tree have subtrees "de" and "dead", then the following paths in the notes tree are searched: "deadbeef", "dead/beef". Note that "de/adbeef" is NOT searched. - Fanout directories (subtrees) must references a whole number of bytes from the SHA1 sum they subdivide. E.g. subtrees "dead" and "de" are acceptable; "d" and "dea" are not. - Multiple levels of fanout are allowed. All the above rules apply recursively. E.g. "de/adbeef" is preferred over "de/adbe/ef", etc. This patch changes the in-memory datastructure for holding parsed notes: Instead of holding all note (and subtree) entries in a hash table, a simple 16-tree structure is used instead. The tree structure consists of 16-arrays as internal nodes, and note/subtree entries as leaf nodes. The tree is traversed by indexing subsequent nibbles of the search key until a leaf node is encountered. If a subtree entry is encountered while searching for a note, the subtree is unpacked into the 16-tree structure, and the search continues into that subtree. The new algorithm performs significantly better in the cases where only a fraction of the notes need to be looked up (this is assumed to be the common case for notes lookup). The new code even performs marginally better in the worst case (where _all_ the notes are looked up). In addition to this, comes the massive performance win associated with organizing the notes tree according to some fanout scheme. Even a simple 2/38 fanout scheme is dramatically quicker to traverse (going from tens of seconds to sub-second runtimes). As for memory usage, the new code is marginally better than the old code in the worst case, but in the case of looking up only some notes from a notes tree with proper fanout, the new code uses only a small fraction of the memory needed to hold the entire notes tree. However, there is one casualty of this patch. The old notes lookup code was able to parse notes that were associated with non-SHA1s (e.g. refs). The new code requires the referenced object to be named by a SHA1 sum. Still, this is not considered a major setback, since the notes infrastructure was not originally intended to annotate objects outside the Git object database. Cc: Johannes Schindelin Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 317 ++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 248 insertions(+), 69 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index a5d888d772..210c4b2263 100644 --- a/notes.c +++ b/notes.c @@ -6,109 +6,288 @@ #include "strbuf.h" #include "tree-walk.h" -struct entry { - unsigned char commit_sha1[20]; - unsigned char notes_sha1[20]; +/* + * Use a non-balancing simple 16-tree structure with struct int_node as + * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a + * 16-array of pointers to its children. + * The bottom 2 bits of each pointer is used to identify the pointer type + * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL) + * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node * + * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node * + * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node * + * + * The root node is a statically allocated struct int_node. + */ +struct int_node { + void *a[16]; }; -struct hash_map { - struct entry *entries; - off_t count, size; +/* + * Leaf nodes come in two variants, note entries and subtree entries, + * distinguished by the LSb of the leaf node pointer (see above). + * As a note entry, the key is the SHA1 of the referenced commit, and the + * value is the SHA1 of the note object. + * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the + * referenced commit, using the last byte of the key to store the length of + * the prefix. The value is the SHA1 of the tree object containing the notes + * subtree. + */ +struct leaf_node { + unsigned char key_sha1[20]; + unsigned char val_sha1[20]; }; -static int initialized; -static struct hash_map hash_map; +#define PTR_TYPE_NULL 0 +#define PTR_TYPE_INTERNAL 1 +#define PTR_TYPE_NOTE 2 +#define PTR_TYPE_SUBTREE 3 -static int hash_index(struct hash_map *map, const unsigned char *sha1) -{ - int i = ((*(unsigned int *)sha1) % map->size); +#define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3) +#define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3)) +#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type))) - for (;;) { - unsigned char *current = map->entries[i].commit_sha1; +#define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f) - if (!hashcmp(sha1, current)) - return i; +#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ + (memcmp(key_sha1, subtree_sha1, subtree_sha1[19])) - if (is_null_sha1(current)) - return -1 - i; +static struct int_node root_node; - if (++i == map->size) - i = 0; +static int initialized; + +static void load_subtree(struct leaf_node *subtree, struct int_node *node, + unsigned int n); + +/* + * To find a leaf_node: + * 1. Start at the root node, with n = 0 + * 2. Use the nth nibble of the key as an index into a: + * - If a[n] is an int_node, recurse into that node and increment n + * - If a leaf_node with matching key, return leaf_node (assert note entry) + * - If a matching subtree entry, unpack that subtree entry (and remove it); + * restart search at the current level. + * - Otherwise, we end up at a NULL pointer, or a non-matching leaf_node. + * Backtrack out of the recursion, one level at a time and check a[0]: + * - If a[0] at the current level is a matching subtree entry, unpack that + * subtree entry (and remove it); restart search at the current level. + */ +static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, + const unsigned char *key_sha1) +{ + struct leaf_node *l; + unsigned char i = GET_NIBBLE(n, key_sha1); + void *p = tree->a[i]; + + switch(GET_PTR_TYPE(p)) { + case PTR_TYPE_INTERNAL: + l = note_tree_find(CLR_PTR_TYPE(p), n + 1, key_sha1); + if (l) + return l; + break; + case PTR_TYPE_NOTE: + l = (struct leaf_node *) CLR_PTR_TYPE(p); + if (!hashcmp(key_sha1, l->key_sha1)) + return l; /* return note object matching given key */ + break; + case PTR_TYPE_SUBTREE: + l = (struct leaf_node *) CLR_PTR_TYPE(p); + if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { + /* unpack tree and resume search */ + tree->a[i] = NULL; + load_subtree(l, tree, n); + free(l); + return note_tree_find(tree, n, key_sha1); + } + break; + case PTR_TYPE_NULL: + default: + assert(!p); + break; } + + /* + * Did not find key at this (or any lower) level. + * Check if there's a matching subtree entry in tree->a[0]. + * If so, unpack tree and resume search. + */ + p = tree->a[0]; + if (GET_PTR_TYPE(p) != PTR_TYPE_SUBTREE) + return NULL; + l = (struct leaf_node *) CLR_PTR_TYPE(p); + if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { + /* unpack tree and resume search */ + tree->a[0] = NULL; + load_subtree(l, tree, n); + free(l); + return note_tree_find(tree, n, key_sha1); + } + return NULL; } -static void add_entry(const unsigned char *commit_sha1, - const unsigned char *notes_sha1) +/* + * To insert a leaf_node: + * 1. Start at the root node, with n = 0 + * 2. Use the nth nibble of the key as an index into a: + * - If a[n] is NULL, store the tweaked pointer directly into a[n] + * - If a[n] is an int_node, recurse into that node and increment n + * - If a[n] is a leaf_node: + * 1. Check if they're equal, and handle that (abort? overwrite?) + * 2. Create a new int_node, and store both leaf_nodes there + * 3. Store the new int_node into a[n]. + */ +static int note_tree_insert(struct int_node *tree, unsigned char n, + const struct leaf_node *entry, unsigned char type) { - int index; - - if (hash_map.count + 1 > hash_map.size >> 1) { - int i, old_size = hash_map.size; - struct entry *old = hash_map.entries; - - hash_map.size = old_size ? old_size << 1 : 64; - hash_map.entries = (struct entry *) - xcalloc(sizeof(struct entry), hash_map.size); - - for (i = 0; i < old_size; i++) - if (!is_null_sha1(old[i].commit_sha1)) { - index = -1 - hash_index(&hash_map, - old[i].commit_sha1); - memcpy(hash_map.entries + index, old + i, - sizeof(struct entry)); - } - free(old); + struct int_node *new_node; + const struct leaf_node *l; + int ret; + unsigned char i = GET_NIBBLE(n, entry->key_sha1); + void *p = tree->a[i]; + assert(GET_PTR_TYPE(entry) == PTR_TYPE_NULL); + switch(GET_PTR_TYPE(p)) { + case PTR_TYPE_NULL: + assert(!p); + tree->a[i] = SET_PTR_TYPE(entry, type); + return 0; + case PTR_TYPE_INTERNAL: + return note_tree_insert(CLR_PTR_TYPE(p), n + 1, entry, type); + default: + assert(GET_PTR_TYPE(p) == PTR_TYPE_NOTE || + GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE); + l = (const struct leaf_node *) CLR_PTR_TYPE(p); + if (!hashcmp(entry->key_sha1, l->key_sha1)) + return -1; /* abort insert on matching key */ + new_node = (struct int_node *) + xcalloc(sizeof(struct int_node), 1); + ret = note_tree_insert(new_node, n + 1, + CLR_PTR_TYPE(p), GET_PTR_TYPE(p)); + if (ret) { + free(new_node); + return -1; + } + tree->a[i] = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); + return note_tree_insert(new_node, n + 1, entry, type); } +} - index = hash_index(&hash_map, commit_sha1); - if (index < 0) { - index = -1 - index; - hash_map.count++; +/* Free the entire notes data contained in the given tree */ +static void note_tree_free(struct int_node *tree) +{ + unsigned int i; + for (i = 0; i < 16; i++) { + void *p = tree->a[i]; + switch(GET_PTR_TYPE(p)) { + case PTR_TYPE_INTERNAL: + note_tree_free(CLR_PTR_TYPE(p)); + /* fall through */ + case PTR_TYPE_NOTE: + case PTR_TYPE_SUBTREE: + free(CLR_PTR_TYPE(p)); + } } +} - hashcpy(hash_map.entries[index].commit_sha1, commit_sha1); - hashcpy(hash_map.entries[index].notes_sha1, notes_sha1); +/* + * Convert a partial SHA1 hex string to the corresponding partial SHA1 value. + * - hex - Partial SHA1 segment in ASCII hex format + * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40 + * - sha1 - Partial SHA1 value is written here + * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20 + * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format). + * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2). + * Pads sha1 with NULs up to sha1_len (not included in returned length). + */ +static int get_sha1_hex_segment(const char *hex, unsigned int hex_len, + unsigned char *sha1, unsigned int sha1_len) +{ + unsigned int i, len = hex_len >> 1; + if (hex_len % 2 != 0 || len > sha1_len) + return -1; + for (i = 0; i < len; i++) { + unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); + if (val & ~0xff) + return -1; + *sha1++ = val; + hex += 2; + } + for (; i < sha1_len; i++) + *sha1++ = 0; + return len; } -static void initialize_hash_map(const char *notes_ref_name) +static void load_subtree(struct leaf_node *subtree, struct int_node *node, + unsigned int n) { - unsigned char sha1[20], commit_sha1[20]; - unsigned mode; + unsigned char commit_sha1[20]; + unsigned int prefix_len; + int status; + void *buf; struct tree_desc desc; struct name_entry entry; - void *buf; + + buf = fill_tree_descriptor(&desc, subtree->val_sha1); + if (!buf) + die("Could not read %s for notes-index", + sha1_to_hex(subtree->val_sha1)); + + prefix_len = subtree->key_sha1[19]; + assert(prefix_len * 2 >= n); + memcpy(commit_sha1, subtree->key_sha1, prefix_len); + while (tree_entry(&desc, &entry)) { + int len = get_sha1_hex_segment(entry.path, strlen(entry.path), + commit_sha1 + prefix_len, 20 - prefix_len); + if (len < 0) + continue; /* entry.path is not a SHA1 sum. Skip */ + len += prefix_len; + + /* + * If commit SHA1 is complete (len == 20), assume note object + * If commit SHA1 is incomplete (len < 20), assume note subtree + */ + if (len <= 20) { + unsigned char type = PTR_TYPE_NOTE; + struct leaf_node *l = (struct leaf_node *) + xcalloc(sizeof(struct leaf_node), 1); + hashcpy(l->key_sha1, commit_sha1); + hashcpy(l->val_sha1, entry.sha1); + if (len < 20) { + l->key_sha1[19] = (unsigned char) len; + type = PTR_TYPE_SUBTREE; + } + status = note_tree_insert(node, n, l, type); + assert(!status); + } + } + free(buf); +} + +static void initialize_notes(const char *notes_ref_name) +{ + unsigned char sha1[20], commit_sha1[20]; + unsigned mode; + struct leaf_node root_tree; if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) || get_tree_entry(commit_sha1, "", sha1, &mode)) return; - buf = fill_tree_descriptor(&desc, sha1); - if (!buf) - die("Could not read %s for notes-index", sha1_to_hex(sha1)); - - while (tree_entry(&desc, &entry)) - if (!get_sha1(entry.path, commit_sha1)) - add_entry(commit_sha1, entry.sha1); - free(buf); + hashclr(root_tree.key_sha1); + hashcpy(root_tree.val_sha1, sha1); + load_subtree(&root_tree, &root_node, 0); } static unsigned char *lookup_notes(const unsigned char *commit_sha1) { - int index; - - if (!hash_map.size) - return NULL; - - index = hash_index(&hash_map, commit_sha1); - if (index < 0) - return NULL; - return hash_map.entries[index].notes_sha1; + struct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1); + if (found) + return found->val_sha1; + return NULL; } void free_notes(void) { - free(hash_map.entries); - memset(&hash_map, 0, sizeof(struct hash_map)); + note_tree_free(&root_node); + memset(&root_node, 0, sizeof(struct int_node)); initialized = 0; } @@ -127,7 +306,7 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb, notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT); else if (!notes_ref_name) notes_ref_name = GIT_NOTES_DEFAULT_REF; - initialize_hash_map(notes_ref_name); + initialize_notes(notes_ref_name); initialized = 1; } -- cgit v1.2.1 From ef8db638cc96abaf166bbafe31752219f3d2cdc2 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Fri, 9 Oct 2009 12:22:09 +0200 Subject: Refactor notes code to concatenate multiple notes annotating the same object Currently, having multiple notes referring to the same commit from various locations in the notes tree is strongly discouraged, since only one of those notes will be parsed and shown. This patch teaches the notes code to _concatenate_ multiple notes that annotate the same commit. Notes are concatenated by creating a new blob object containing the concatenation of the notes in question, and replacing them with the concatenated note in the internal notes tree structure. Getting the concatenation right requires being more proactive in unpacking subtree entries in the internal notes tree structure, so that we don't return a note prematurely (i.e. before having found all other notes that annotate the same object). As such, this patch may incur a small performance penalty. Suggested-by: Sam Vilain Re-suggested-by: Johannes Schindelin Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 243 ++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 161 insertions(+), 82 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 210c4b2263..50a4672d7c 100644 --- a/notes.c +++ b/notes.c @@ -59,115 +59,196 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, unsigned int n); /* - * To find a leaf_node: + * Search the tree until the appropriate location for the given key is found: * 1. Start at the root node, with n = 0 - * 2. Use the nth nibble of the key as an index into a: - * - If a[n] is an int_node, recurse into that node and increment n - * - If a leaf_node with matching key, return leaf_node (assert note entry) + * 2. If a[0] at the current level is a matching subtree entry, unpack that + * subtree entry and remove it; restart search at the current level. + * 3. Use the nth nibble of the key as an index into a: + * - If a[n] is an int_node, recurse from #2 into that node and increment n * - If a matching subtree entry, unpack that subtree entry (and remove it); * restart search at the current level. - * - Otherwise, we end up at a NULL pointer, or a non-matching leaf_node. - * Backtrack out of the recursion, one level at a time and check a[0]: - * - If a[0] at the current level is a matching subtree entry, unpack that - * subtree entry (and remove it); restart search at the current level. + * - Otherwise, we have found one of the following: + * - a subtree entry which does not match the key + * - a note entry which may or may not match the key + * - an unused leaf node (NULL) + * In any case, set *tree and *n, and return pointer to the tree location. */ -static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, - const unsigned char *key_sha1) +static void **note_tree_search(struct int_node **tree, + unsigned char *n, const unsigned char *key_sha1) { struct leaf_node *l; - unsigned char i = GET_NIBBLE(n, key_sha1); - void *p = tree->a[i]; + unsigned char i; + void *p = (*tree)->a[0]; + if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) { + l = (struct leaf_node *) CLR_PTR_TYPE(p); + if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { + /* unpack tree and resume search */ + (*tree)->a[0] = NULL; + load_subtree(l, *tree, *n); + free(l); + return note_tree_search(tree, n, key_sha1); + } + } + + i = GET_NIBBLE(*n, key_sha1); + p = (*tree)->a[i]; switch(GET_PTR_TYPE(p)) { case PTR_TYPE_INTERNAL: - l = note_tree_find(CLR_PTR_TYPE(p), n + 1, key_sha1); - if (l) - return l; - break; - case PTR_TYPE_NOTE: - l = (struct leaf_node *) CLR_PTR_TYPE(p); - if (!hashcmp(key_sha1, l->key_sha1)) - return l; /* return note object matching given key */ - break; + *tree = CLR_PTR_TYPE(p); + (*n)++; + return note_tree_search(tree, n, key_sha1); case PTR_TYPE_SUBTREE: l = (struct leaf_node *) CLR_PTR_TYPE(p); if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { /* unpack tree and resume search */ - tree->a[i] = NULL; - load_subtree(l, tree, n); + (*tree)->a[i] = NULL; + load_subtree(l, *tree, *n); free(l); - return note_tree_find(tree, n, key_sha1); + return note_tree_search(tree, n, key_sha1); } - break; - case PTR_TYPE_NULL: + /* fall through */ default: - assert(!p); - break; + return &((*tree)->a[i]); } +} - /* - * Did not find key at this (or any lower) level. - * Check if there's a matching subtree entry in tree->a[0]. - * If so, unpack tree and resume search. - */ - p = tree->a[0]; - if (GET_PTR_TYPE(p) != PTR_TYPE_SUBTREE) - return NULL; - l = (struct leaf_node *) CLR_PTR_TYPE(p); - if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { - /* unpack tree and resume search */ - tree->a[0] = NULL; - load_subtree(l, tree, n); - free(l); - return note_tree_find(tree, n, key_sha1); +/* + * To find a leaf_node: + * Search to the tree location appropriate for the given key: + * If a note entry with matching key, return the note entry, else return NULL. + */ +static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, + const unsigned char *key_sha1) +{ + void **p = note_tree_search(&tree, &n, key_sha1); + if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) { + struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p); + if (!hashcmp(key_sha1, l->key_sha1)) + return l; } return NULL; } +/* Create a new blob object by concatenating the two given blob objects */ +static int concatenate_notes(unsigned char *cur_sha1, + const unsigned char *new_sha1) +{ + char *cur_msg, *new_msg, *buf; + unsigned long cur_len, new_len, buf_len; + enum object_type cur_type, new_type; + int ret; + + /* read in both note blob objects */ + new_msg = read_sha1_file(new_sha1, &new_type, &new_len); + if (!new_msg || !new_len || new_type != OBJ_BLOB) { + free(new_msg); + return 0; + } + cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len); + if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) { + free(cur_msg); + free(new_msg); + hashcpy(cur_sha1, new_sha1); + return 0; + } + + /* we will separate the notes by a newline anyway */ + if (cur_msg[cur_len - 1] == '\n') + cur_len--; + + /* concatenate cur_msg and new_msg into buf */ + buf_len = cur_len + 1 + new_len; + buf = (char *) xmalloc(buf_len); + memcpy(buf, cur_msg, cur_len); + buf[cur_len] = '\n'; + memcpy(buf + cur_len + 1, new_msg, new_len); + + free(cur_msg); + free(new_msg); + + /* create a new blob object from buf */ + ret = write_sha1_file(buf, buf_len, "blob", cur_sha1); + free(buf); + return ret; +} + /* * To insert a leaf_node: - * 1. Start at the root node, with n = 0 - * 2. Use the nth nibble of the key as an index into a: - * - If a[n] is NULL, store the tweaked pointer directly into a[n] - * - If a[n] is an int_node, recurse into that node and increment n - * - If a[n] is a leaf_node: - * 1. Check if they're equal, and handle that (abort? overwrite?) - * 2. Create a new int_node, and store both leaf_nodes there - * 3. Store the new int_node into a[n]. + * Search to the tree location appropriate for the given leaf_node's key: + * - If location is unused (NULL), store the tweaked pointer directly there + * - If location holds a note entry that matches the note-to-be-inserted, then + * concatenate the two notes. + * - If location holds a note entry that matches the subtree-to-be-inserted, + * then unpack the subtree-to-be-inserted into the location. + * - If location holds a matching subtree entry, unpack the subtree at that + * location, and restart the insert operation from that level. + * - Else, create a new int_node, holding both the node-at-location and the + * node-to-be-inserted, and store the new int_node into the location. */ -static int note_tree_insert(struct int_node *tree, unsigned char n, - const struct leaf_node *entry, unsigned char type) +static void note_tree_insert(struct int_node *tree, unsigned char n, + struct leaf_node *entry, unsigned char type) { struct int_node *new_node; - const struct leaf_node *l; - int ret; - unsigned char i = GET_NIBBLE(n, entry->key_sha1); - void *p = tree->a[i]; - assert(GET_PTR_TYPE(entry) == PTR_TYPE_NULL); - switch(GET_PTR_TYPE(p)) { + struct leaf_node *l; + void **p = note_tree_search(&tree, &n, entry->key_sha1); + + assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ + l = (struct leaf_node *) CLR_PTR_TYPE(*p); + switch(GET_PTR_TYPE(*p)) { case PTR_TYPE_NULL: - assert(!p); - tree->a[i] = SET_PTR_TYPE(entry, type); - return 0; - case PTR_TYPE_INTERNAL: - return note_tree_insert(CLR_PTR_TYPE(p), n + 1, entry, type); - default: - assert(GET_PTR_TYPE(p) == PTR_TYPE_NOTE || - GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE); - l = (const struct leaf_node *) CLR_PTR_TYPE(p); - if (!hashcmp(entry->key_sha1, l->key_sha1)) - return -1; /* abort insert on matching key */ - new_node = (struct int_node *) - xcalloc(sizeof(struct int_node), 1); - ret = note_tree_insert(new_node, n + 1, - CLR_PTR_TYPE(p), GET_PTR_TYPE(p)); - if (ret) { - free(new_node); - return -1; + assert(!*p); + *p = SET_PTR_TYPE(entry, type); + return; + case PTR_TYPE_NOTE: + switch (type) { + case PTR_TYPE_NOTE: + if (!hashcmp(l->key_sha1, entry->key_sha1)) { + /* skip concatenation if l == entry */ + if (!hashcmp(l->val_sha1, entry->val_sha1)) + return; + + if (concatenate_notes(l->val_sha1, + entry->val_sha1)) + die("failed to concatenate note %s " + "into note %s for commit %s", + sha1_to_hex(entry->val_sha1), + sha1_to_hex(l->val_sha1), + sha1_to_hex(l->key_sha1)); + free(entry); + return; + } + break; + case PTR_TYPE_SUBTREE: + if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1, + entry->key_sha1)) { + /* unpack 'entry' */ + load_subtree(entry, tree, n); + free(entry); + return; + } + break; + } + break; + case PTR_TYPE_SUBTREE: + if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) { + /* unpack 'l' and restart insert */ + *p = NULL; + load_subtree(l, tree, n); + free(l); + note_tree_insert(tree, n, entry, type); + return; } - tree->a[i] = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); - return note_tree_insert(new_node, n + 1, entry, type); + break; } + + /* non-matching leaf_node */ + assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE || + GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); + new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1); + note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p)); + *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); + note_tree_insert(new_node, n + 1, entry, type); } /* Free the entire notes data contained in the given tree */ @@ -220,7 +301,6 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, { unsigned char commit_sha1[20]; unsigned int prefix_len; - int status; void *buf; struct tree_desc desc; struct name_entry entry; @@ -254,8 +334,7 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, l->key_sha1[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } - status = note_tree_insert(node, n, l, type); - assert(!status); + note_tree_insert(node, n, l, type); } } free(buf); -- cgit v1.2.1 From 488bdf2ebe6e99fb30ad958a710b0b3f737b4d0f Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Thu, 3 Dec 2009 04:53:54 +0100 Subject: Fix crasher on encountering SHA1-like non-note in notes tree When loading a notes tree, the code primarily looks for SHA1-like paths whose total length (discounting directory separators) are 40 chars (interpreted as valid note entries) or less (interpreted as subtree entries that may in turn contain note entries when unpacked). However, there is an additional condition that must hold for valid subtree entries: They must be _tree_ objects (duh). This patch adds an appropriate test for this condition, thereby fixing the crash that occured when passing a non-tree object to the tree-walk API. The patch also adds another selftest verifying correct behaviour of non-notes in note trees. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 50a4672d7c..023adce982 100644 --- a/notes.c +++ b/notes.c @@ -331,6 +331,8 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, hashcpy(l->key_sha1, commit_sha1); hashcpy(l->val_sha1, entry.sha1); if (len < 20) { + if (!S_ISDIR(entry.mode)) + continue; /* entry cannot be subtree */ l->key_sha1[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } -- cgit v1.2.1 From 0ab1faae39173be6126364461c1be86542e6b17d Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:09 +0100 Subject: Minor cosmetic fixes to notes.c Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 023adce982..47e38a10a8 100644 --- a/notes.c +++ b/notes.c @@ -1,7 +1,6 @@ #include "cache.h" #include "commit.h" #include "notes.h" -#include "refs.h" #include "utf8.h" #include "strbuf.h" #include "tree-walk.h" @@ -93,7 +92,7 @@ static void **note_tree_search(struct int_node **tree, i = GET_NIBBLE(*n, key_sha1); p = (*tree)->a[i]; - switch(GET_PTR_TYPE(p)) { + switch (GET_PTR_TYPE(p)) { case PTR_TYPE_INTERNAL: *tree = CLR_PTR_TYPE(p); (*n)++; @@ -195,7 +194,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ l = (struct leaf_node *) CLR_PTR_TYPE(*p); - switch(GET_PTR_TYPE(*p)) { + switch (GET_PTR_TYPE(*p)) { case PTR_TYPE_NULL: assert(!*p); *p = SET_PTR_TYPE(entry, type); @@ -257,7 +256,7 @@ static void note_tree_free(struct int_node *tree) unsigned int i; for (i = 0; i < 16; i++) { void *p = tree->a[i]; - switch(GET_PTR_TYPE(p)) { + switch (GET_PTR_TYPE(p)) { case PTR_TYPE_INTERNAL: note_tree_free(CLR_PTR_TYPE(p)); /* fall through */ @@ -274,7 +273,7 @@ static void note_tree_free(struct int_node *tree) * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40 * - sha1 - Partial SHA1 value is written here * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20 - * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format). + * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)). * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2). * Pads sha1 with NULs up to sha1_len (not included in returned length). */ -- cgit v1.2.1 From a7e7eff66206829f7752c565198dbe6f40ef72a0 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:10 +0100 Subject: Notes API: get_commit_notes() -> format_note() + remove the commit restriction There is really no reason why only commit objects can be annotated. By changing the struct commit parameter to get_commit_notes() into a sha1 we gain the ability to annotate any object type. To reflect this in the function naming as well, we rename get_commit_notes() to format_note(). This patch also fixes comments and variable names throughout notes.c as a consequence of the removal of the unnecessary 'commit' restriction. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 47e38a10a8..4ee4fec233 100644 --- a/notes.c +++ b/notes.c @@ -1,5 +1,4 @@ #include "cache.h" -#include "commit.h" #include "notes.h" #include "utf8.h" #include "strbuf.h" @@ -24,10 +23,10 @@ struct int_node { /* * Leaf nodes come in two variants, note entries and subtree entries, * distinguished by the LSb of the leaf node pointer (see above). - * As a note entry, the key is the SHA1 of the referenced commit, and the + * As a note entry, the key is the SHA1 of the referenced object, and the * value is the SHA1 of the note object. * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the - * referenced commit, using the last byte of the key to store the length of + * referenced object, using the last byte of the key to store the length of * the prefix. The value is the SHA1 of the tree object containing the notes * subtree. */ @@ -210,7 +209,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, if (concatenate_notes(l->val_sha1, entry->val_sha1)) die("failed to concatenate note %s " - "into note %s for commit %s", + "into note %s for object %s", sha1_to_hex(entry->val_sha1), sha1_to_hex(l->val_sha1), sha1_to_hex(l->key_sha1)); @@ -298,7 +297,7 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len, static void load_subtree(struct leaf_node *subtree, struct int_node *node, unsigned int n) { - unsigned char commit_sha1[20]; + unsigned char object_sha1[20]; unsigned int prefix_len; void *buf; struct tree_desc desc; @@ -311,23 +310,23 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, prefix_len = subtree->key_sha1[19]; assert(prefix_len * 2 >= n); - memcpy(commit_sha1, subtree->key_sha1, prefix_len); + memcpy(object_sha1, subtree->key_sha1, prefix_len); while (tree_entry(&desc, &entry)) { int len = get_sha1_hex_segment(entry.path, strlen(entry.path), - commit_sha1 + prefix_len, 20 - prefix_len); + object_sha1 + prefix_len, 20 - prefix_len); if (len < 0) continue; /* entry.path is not a SHA1 sum. Skip */ len += prefix_len; /* - * If commit SHA1 is complete (len == 20), assume note object - * If commit SHA1 is incomplete (len < 20), assume note subtree + * If object SHA1 is complete (len == 20), assume note object + * If object SHA1 is incomplete (len < 20), assume note subtree */ if (len <= 20) { unsigned char type = PTR_TYPE_NOTE; struct leaf_node *l = (struct leaf_node *) xcalloc(sizeof(struct leaf_node), 1); - hashcpy(l->key_sha1, commit_sha1); + hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, entry.sha1); if (len < 20) { if (!S_ISDIR(entry.mode)) @@ -343,12 +342,12 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, static void initialize_notes(const char *notes_ref_name) { - unsigned char sha1[20], commit_sha1[20]; + unsigned char sha1[20], object_sha1[20]; unsigned mode; struct leaf_node root_tree; - if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) || - get_tree_entry(commit_sha1, "", sha1, &mode)) + if (!notes_ref_name || read_ref(notes_ref_name, object_sha1) || + get_tree_entry(object_sha1, "", sha1, &mode)) return; hashclr(root_tree.key_sha1); @@ -356,9 +355,9 @@ static void initialize_notes(const char *notes_ref_name) load_subtree(&root_tree, &root_node, 0); } -static unsigned char *lookup_notes(const unsigned char *commit_sha1) +static unsigned char *lookup_notes(const unsigned char *object_sha1) { - struct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1); + struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1); if (found) return found->val_sha1; return NULL; @@ -371,7 +370,7 @@ void free_notes(void) initialized = 0; } -void get_commit_notes(const struct commit *commit, struct strbuf *sb, +void format_note(const unsigned char *object_sha1, struct strbuf *sb, const char *output_encoding, int flags) { static const char utf8[] = "utf-8"; @@ -390,7 +389,7 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb, initialized = 1; } - sha1 = lookup_notes(commit->object.sha1); + sha1 = lookup_notes(object_sha1); if (!sha1) return; -- cgit v1.2.1 From 709f79b0894859a6624e99b3a0c4714dd4ece494 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:12 +0100 Subject: Notes API: init_notes(): Initialize the notes tree from the given notes ref Created by a simple refactoring of initialize_notes(). Also add a new 'flags' parameter, which is a bitwise combination of notes initialization flags. For now, there is only one flag - NOTES_INIT_EMPTY - which indicates that the notes tree should not auto-load the contents of the given (or default) notes ref, but rather should leave the notes tree initialized to an empty state. This will become useful in the future when manipulating the notes tree through the notes API. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 4ee4fec233..3f4ae35340 100644 --- a/notes.c +++ b/notes.c @@ -340,15 +340,28 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, free(buf); } -static void initialize_notes(const char *notes_ref_name) +void init_notes(const char *notes_ref, int flags) { unsigned char sha1[20], object_sha1[20]; unsigned mode; struct leaf_node root_tree; - if (!notes_ref_name || read_ref(notes_ref_name, object_sha1) || - get_tree_entry(object_sha1, "", sha1, &mode)) + assert(!initialized); + initialized = 1; + + if (!notes_ref) + notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT); + if (!notes_ref) + notes_ref = notes_ref_name; /* value of core.notesRef config */ + if (!notes_ref) + notes_ref = GIT_NOTES_DEFAULT_REF; + + if (flags & NOTES_INIT_EMPTY || !notes_ref || + read_ref(notes_ref, object_sha1)) return; + if (get_tree_entry(object_sha1, "", sha1, &mode)) + die("Failed to read notes tree referenced by %s (%s)", + notes_ref, object_sha1); hashclr(root_tree.key_sha1); hashcpy(root_tree.val_sha1, sha1); @@ -379,15 +392,8 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb, unsigned long linelen, msglen; enum object_type type; - if (!initialized) { - const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT); - if (env) - notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT); - else if (!notes_ref_name) - notes_ref_name = GIT_NOTES_DEFAULT_REF; - initialize_notes(notes_ref_name); - initialized = 1; - } + if (!initialized) + init_notes(NULL, 0); sha1 = lookup_notes(object_sha1); if (!sha1) -- cgit v1.2.1 From 2626b5367047d8b8d5c4555ec6b579ed37a6625d Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:13 +0100 Subject: Notes API: add_note(): Add note objects to the internal notes tree structure Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 3f4ae35340..2c0d14ed8f 100644 --- a/notes.c +++ b/notes.c @@ -368,6 +368,17 @@ void init_notes(const char *notes_ref, int flags) load_subtree(&root_tree, &root_node, 0); } +void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1) +{ + struct leaf_node *l; + + assert(initialized); + l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); + hashcpy(l->key_sha1, object_sha1); + hashcpy(l->val_sha1, note_sha1); + note_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE); +} + static unsigned char *lookup_notes(const unsigned char *object_sha1) { struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1); -- cgit v1.2.1 From 1ec666b092d1df5691cd4d38f20aaeadd5049aa2 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:14 +0100 Subject: Notes API: remove_note(): Remove note objects from the notes tree structure This includes adding internal functions for maintaining a healthy notes tree structure after removing individual notes. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 2c0d14ed8f..2e82d71987 100644 --- a/notes.c +++ b/notes.c @@ -44,7 +44,7 @@ struct leaf_node { #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3)) #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type))) -#define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f) +#define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f) #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ (memcmp(key_sha1, subtree_sha1, subtree_sha1[19])) @@ -249,6 +249,79 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, note_tree_insert(new_node, n + 1, entry, type); } +/* + * How to consolidate an int_node: + * If there are > 1 non-NULL entries, give up and return non-zero. + * Otherwise replace the int_node at the given index in the given parent node + * with the only entry (or a NULL entry if no entries) from the given tree, + * and return 0. + */ +static int note_tree_consolidate(struct int_node *tree, + struct int_node *parent, unsigned char index) +{ + unsigned int i; + void *p = NULL; + + assert(tree && parent); + assert(CLR_PTR_TYPE(parent->a[index]) == tree); + + for (i = 0; i < 16; i++) { + if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) { + if (p) /* more than one entry */ + return -2; + p = tree->a[i]; + } + } + + /* replace tree with p in parent[index] */ + parent->a[index] = p; + free(tree); + return 0; +} + +/* + * 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. + * - 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 int_node *tree, unsigned char n, + struct leaf_node *entry) +{ + struct leaf_node *l; + struct int_node *parent_stack[20]; + unsigned char i, j; + void **p = note_tree_search(&tree, &n, entry->key_sha1); + + assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ + if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE) + return; /* type mismatch, nothing to remove */ + l = (struct leaf_node *) CLR_PTR_TYPE(*p); + if (hashcmp(l->key_sha1, entry->key_sha1)) + return; /* key mismatch, nothing to remove */ + + /* we have found a matching entry */ + free(l); + *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL); + + /* consolidate this tree level, and parent levels, if possible */ + if (!n) + return; /* cannot consolidate top level */ + /* first, build stack of ancestors between root and current node */ + parent_stack[0] = &root_node; + for (i = 0; i < n; i++) { + j = GET_NIBBLE(i, entry->key_sha1); + parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]); + } + assert(i == n && parent_stack[i] == tree); + /* next, unwind stack until note_tree_consolidate() is done */ + while (i > 0 && + !note_tree_consolidate(parent_stack[i], parent_stack[i - 1], + GET_NIBBLE(i - 1, entry->key_sha1))) + i--; +} + /* Free the entire notes data contained in the given tree */ static void note_tree_free(struct int_node *tree) { @@ -379,6 +452,16 @@ void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1) note_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE); } +void remove_note(const unsigned char *object_sha1) +{ + struct leaf_node l; + + assert(initialized); + hashcpy(l.key_sha1, object_sha1); + hashclr(l.val_sha1); + return note_tree_remove(&root_node, 0, &l); +} + static unsigned char *lookup_notes(const unsigned char *object_sha1) { struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1); -- cgit v1.2.1 From 9b391f218a5b732a5a8abae87d3165e97fe2f6f6 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:15 +0100 Subject: Notes API: get_note(): Return the note annotating the given object Created by a simple cleanup and rename of lookup_notes(). Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 2e82d71987..a0a85b4daf 100644 --- a/notes.c +++ b/notes.c @@ -462,12 +462,13 @@ void remove_note(const unsigned char *object_sha1) return note_tree_remove(&root_node, 0, &l); } -static unsigned char *lookup_notes(const unsigned char *object_sha1) +const unsigned char *get_note(const unsigned char *object_sha1) { - struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1); - if (found) - return found->val_sha1; - return NULL; + struct leaf_node *found; + + assert(initialized); + found = note_tree_find(&root_node, 0, object_sha1); + return found ? found->val_sha1 : NULL; } void free_notes(void) @@ -481,7 +482,7 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb, const char *output_encoding, int flags) { static const char utf8[] = "utf-8"; - unsigned char *sha1; + const unsigned char *sha1; char *msg, *msg_p; unsigned long linelen, msglen; enum object_type type; @@ -489,7 +490,7 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb, if (!initialized) init_notes(NULL, 0); - sha1 = lookup_notes(object_sha1); + sha1 = get_note(object_sha1); if (!sha1) return; -- cgit v1.2.1 From 73f77b909f87fcaece42ec50d8d0c1c35efbf947 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:16 +0100 Subject: Notes API: for_each_note(): Traverse the entire notes tree with a callback This includes a first attempt at creating an optimal fanout scheme (which is calculated on-the-fly, while traversing). Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index a0a85b4daf..eabd6f30cd 100644 --- a/notes.c +++ b/notes.c @@ -413,6 +413,133 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, free(buf); } +/* + * Determine optimal on-disk fanout for this part of the notes tree + * + * Given a (sub)tree and the level in the internal tree structure, determine + * whether or not the given existing fanout should be expanded for this + * (sub)tree. + * + * Values of the 'fanout' variable: + * - 0: No fanout (all notes are stored directly in the root notes tree) + * - 1: 2/38 fanout + * - 2: 2/2/36 fanout + * - 3: 2/2/2/34 fanout + * etc. + */ +static unsigned char determine_fanout(struct int_node *tree, unsigned char n, + unsigned char fanout) +{ + /* + * The following is a simple heuristic that works well in practice: + * For each even-numbered 16-tree level (remember that each on-disk + * fanout level corresponds to _two_ 16-tree levels), peek at all 16 + * entries at that tree level. If all of them are either int_nodes or + * subtree entries, then there are likely plenty of notes below this + * level, so we return an incremented fanout. + */ + unsigned int i; + if ((n % 2) || (n > 2 * fanout)) + return fanout; + for (i = 0; i < 16; i++) { + switch (GET_PTR_TYPE(tree->a[i])) { + case PTR_TYPE_SUBTREE: + case PTR_TYPE_INTERNAL: + continue; + default: + return fanout; + } + } + return fanout + 1; +} + +static void construct_path_with_fanout(const unsigned char *sha1, + unsigned char fanout, char *path) +{ + unsigned int i = 0, j = 0; + const char *hex_sha1 = sha1_to_hex(sha1); + assert(fanout < 20); + while (fanout) { + path[i++] = hex_sha1[j++]; + path[i++] = hex_sha1[j++]; + path[i++] = '/'; + fanout--; + } + strcpy(path + i, hex_sha1 + j); +} + +static int for_each_note_helper(struct int_node *tree, unsigned char n, + unsigned char fanout, int flags, each_note_fn fn, + void *cb_data) +{ + unsigned int i; + void *p; + int ret = 0; + struct leaf_node *l; + static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */ + + fanout = determine_fanout(tree, n, fanout); + for (i = 0; i < 16; i++) { +redo: + p = tree->a[i]; + switch (GET_PTR_TYPE(p)) { + case PTR_TYPE_INTERNAL: + /* recurse into int_node */ + ret = for_each_note_helper(CLR_PTR_TYPE(p), n + 1, + fanout, flags, fn, cb_data); + break; + case PTR_TYPE_SUBTREE: + l = (struct leaf_node *) CLR_PTR_TYPE(p); + /* + * Subtree entries in the note tree represent parts of + * the note tree that have not yet been explored. There + * is a direct relationship between subtree entries at + * level 'n' in the tree, and the 'fanout' variable: + * Subtree entries at level 'n <= 2 * fanout' should be + * preserved, since they correspond exactly to a fanout + * directory in the on-disk structure. However, subtree + * entries at level 'n > 2 * fanout' should NOT be + * preserved, but rather consolidated into the above + * notes tree level. We achieve this by unconditionally + * unpacking subtree entries that exist below the + * threshold level at 'n = 2 * fanout'. + */ + if (n <= 2 * fanout && + flags & FOR_EACH_NOTE_YIELD_SUBTREES) { + /* invoke callback with subtree */ + unsigned int path_len = + l->key_sha1[19] * 2 + fanout; + assert(path_len < 40 + 19); + construct_path_with_fanout(l->key_sha1, fanout, + path); + /* Create trailing slash, if needed */ + if (path[path_len - 1] != '/') + path[path_len++] = '/'; + path[path_len] = '\0'; + ret = fn(l->key_sha1, l->val_sha1, path, + cb_data); + } + if (n > fanout * 2 || + !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) { + /* unpack subtree and resume traversal */ + tree->a[i] = NULL; + load_subtree(l, tree, n); + free(l); + goto redo; + } + break; + case PTR_TYPE_NOTE: + l = (struct leaf_node *) CLR_PTR_TYPE(p); + construct_path_with_fanout(l->key_sha1, fanout, path); + ret = fn(l->key_sha1, l->val_sha1, path, cb_data); + break; + } + if (ret) + return ret; + } + return 0; +} + void init_notes(const char *notes_ref, int flags) { unsigned char sha1[20], object_sha1[20]; @@ -471,6 +598,12 @@ const unsigned char *get_note(const unsigned char *object_sha1) return found ? found->val_sha1 : NULL; } +int for_each_note(int flags, each_note_fn fn, void *cb_data) +{ + assert(initialized); + return for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data); +} + void free_notes(void) { note_tree_free(&root_node); -- cgit v1.2.1 From 61a7cca0c6504aee7bae7837582230561bdb81d4 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:17 +0100 Subject: Notes API: write_notes_tree(): Store the notes tree in the database Uses for_each_note() to traverse the notes tree, and produces tree objects on the fly representing the "on-disk" version of the notes tree with appropriate fanout. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index eabd6f30cd..b576f7e624 100644 --- a/notes.c +++ b/notes.c @@ -1,5 +1,6 @@ #include "cache.h" #include "notes.h" +#include "tree.h" #include "utf8.h" #include "strbuf.h" #include "tree-walk.h" @@ -540,6 +541,126 @@ redo: return 0; } +struct tree_write_stack { + struct tree_write_stack *next; + struct strbuf buf; + char path[2]; /* path to subtree in next, if any */ +}; + +static inline int matches_tree_write_stack(struct tree_write_stack *tws, + const char *full_path) +{ + return full_path[0] == tws->path[0] && + full_path[1] == tws->path[1] && + full_path[2] == '/'; +} + +static void write_tree_entry(struct strbuf *buf, unsigned int mode, + const char *path, unsigned int path_len, const + unsigned char *sha1) +{ + strbuf_addf(buf, "%06o %.*s%c", mode, path_len, path, '\0'); + strbuf_add(buf, sha1, 20); +} + +static void tree_write_stack_init_subtree(struct tree_write_stack *tws, + const char *path) +{ + struct tree_write_stack *n; + assert(!tws->next); + assert(tws->path[0] == '\0' && tws->path[1] == '\0'); + n = (struct tree_write_stack *) + xmalloc(sizeof(struct tree_write_stack)); + n->next = NULL; + strbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */ + n->path[0] = n->path[1] = '\0'; + tws->next = n; + tws->path[0] = path[0]; + tws->path[1] = path[1]; +} + +static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) +{ + int ret; + struct tree_write_stack *n = tws->next; + unsigned char s[20]; + if (n) { + ret = tree_write_stack_finish_subtree(n); + if (ret) + return ret; + ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s); + if (ret) + return ret; + strbuf_release(&n->buf); + free(n); + tws->next = NULL; + write_tree_entry(&tws->buf, 040000, tws->path, 2, s); + tws->path[0] = tws->path[1] = '\0'; + } + return 0; +} + +static int write_each_note_helper(struct tree_write_stack *tws, + const char *path, unsigned int mode, + const unsigned char *sha1) +{ + size_t path_len = strlen(path); + unsigned int n = 0; + int ret; + + /* Determine common part of tree write stack */ + while (tws && 3 * n < path_len && + matches_tree_write_stack(tws, path + 3 * n)) { + n++; + tws = tws->next; + } + + /* tws point to last matching tree_write_stack entry */ + ret = tree_write_stack_finish_subtree(tws); + if (ret) + return ret; + + /* Start subtrees needed to satisfy path */ + while (3 * n + 2 < path_len && path[3 * n + 2] == '/') { + tree_write_stack_init_subtree(tws, path + 3 * n); + n++; + tws = tws->next; + } + + /* There should be no more directory components in the given path */ + assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL); + + /* Finally add given entry to the current tree object */ + write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n), + sha1); + + return 0; +} + +struct write_each_note_data { + struct tree_write_stack *root; +}; + +static int write_each_note(const unsigned char *object_sha1, + const unsigned char *note_sha1, char *note_path, + void *cb_data) +{ + struct write_each_note_data *d = + (struct write_each_note_data *) cb_data; + size_t note_path_len = strlen(note_path); + unsigned int mode = 0100644; + + if (note_path[note_path_len - 1] == '/') { + /* subtree entry */ + note_path_len--; + note_path[note_path_len] = '\0'; + mode = 040000; + } + assert(note_path_len <= 40 + 19); + + return write_each_note_helper(d->root, note_path, mode, note_sha1); +} + void init_notes(const char *notes_ref, int flags) { unsigned char sha1[20], object_sha1[20]; @@ -604,6 +725,30 @@ int for_each_note(int flags, each_note_fn fn, void *cb_data) return for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data); } +int write_notes_tree(unsigned char *result) +{ + struct tree_write_stack root; + struct write_each_note_data cb_data; + int ret; + + assert(initialized); + + /* Prepare for traversal of current notes tree */ + root.next = NULL; /* last forward entry in list is grounded */ + strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */ + root.path[0] = root.path[1] = '\0'; + cb_data.root = &root; + + /* Write tree objects representing current notes tree */ + ret = for_each_note(FOR_EACH_NOTE_DONT_UNPACK_SUBTREES | + FOR_EACH_NOTE_YIELD_SUBTREES, + write_each_note, &cb_data) || + tree_write_stack_finish_subtree(&root) || + write_sha1_file(root.buf.buf, root.buf.len, tree_type, result); + strbuf_release(&root.buf); + return ret; +} + void free_notes(void) { note_tree_free(&root_node); -- cgit v1.2.1 From cd30539214bb09881b84c796a50d30e409dee3fa Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:18 +0100 Subject: Notes API: Allow multiple concurrent notes trees with new struct notes_tree The new struct notes_tree encapsulates access to a specific notes tree. It is provided to allow callers to make use of several different notes trees simultaneously. A struct notes_tree * parameter is added to every function in the notes API. In all cases, NULL can be passed, in which case the fallback "default" notes tree (default_notes_tree) is used. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 90 ++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 34 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index b576f7e624..08a369af82 100644 --- a/notes.c +++ b/notes.c @@ -50,9 +50,7 @@ struct leaf_node { #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ (memcmp(key_sha1, subtree_sha1, subtree_sha1[19])) -static struct int_node root_node; - -static int initialized; +struct notes_tree default_notes_tree; static void load_subtree(struct leaf_node *subtree, struct int_node *node, unsigned int n); @@ -287,8 +285,8 @@ static int note_tree_consolidate(struct int_node *tree, * - 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 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]; @@ -310,7 +308,7 @@ static void note_tree_remove(struct int_node *tree, unsigned char n, if (!n) return; /* cannot consolidate top level */ /* first, build stack of ancestors between root and current node */ - parent_stack[0] = &root_node; + parent_stack[0] = t->root; for (i = 0; i < n; i++) { j = GET_NIBBLE(i, entry->key_sha1); parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]); @@ -661,14 +659,15 @@ static int write_each_note(const unsigned char *object_sha1, return write_each_note_helper(d->root, note_path, mode, note_sha1); } -void init_notes(const char *notes_ref, int flags) +void init_notes(struct notes_tree *t, const char *notes_ref, int flags) { unsigned char sha1[20], object_sha1[20]; unsigned mode; struct leaf_node root_tree; - assert(!initialized); - initialized = 1; + if (!t) + t = &default_notes_tree; + assert(!t->initialized); if (!notes_ref) notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT); @@ -677,6 +676,10 @@ void init_notes(const char *notes_ref, int flags) if (!notes_ref) notes_ref = GIT_NOTES_DEFAULT_REF; + t->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1); + t->ref = notes_ref ? xstrdup(notes_ref) : NULL; + t->initialized = 1; + if (flags & NOTES_INIT_EMPTY || !notes_ref || read_ref(notes_ref, object_sha1)) return; @@ -686,52 +689,65 @@ void init_notes(const char *notes_ref, int flags) hashclr(root_tree.key_sha1); hashcpy(root_tree.val_sha1, sha1); - load_subtree(&root_tree, &root_node, 0); + load_subtree(&root_tree, t->root, 0); } -void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1) +void add_note(struct notes_tree *t, const unsigned char *object_sha1, + const unsigned char *note_sha1) { struct leaf_node *l; - assert(initialized); + if (!t) + t = &default_notes_tree; + assert(t->initialized); l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, note_sha1); - note_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE); + note_tree_insert(t->root, 0, l, PTR_TYPE_NOTE); } -void remove_note(const unsigned char *object_sha1) +void remove_note(struct notes_tree *t, const unsigned char *object_sha1) { struct leaf_node l; - assert(initialized); + if (!t) + t = &default_notes_tree; + assert(t->initialized); hashcpy(l.key_sha1, object_sha1); hashclr(l.val_sha1); - return note_tree_remove(&root_node, 0, &l); + return note_tree_remove(t, t->root, 0, &l); } -const unsigned char *get_note(const unsigned char *object_sha1) +const unsigned char *get_note(struct notes_tree *t, + const unsigned char *object_sha1) { struct leaf_node *found; - assert(initialized); - found = note_tree_find(&root_node, 0, object_sha1); + if (!t) + t = &default_notes_tree; + assert(t->initialized); + found = note_tree_find(t->root, 0, object_sha1); return found ? found->val_sha1 : NULL; } -int for_each_note(int flags, each_note_fn fn, void *cb_data) +int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, + void *cb_data) { - assert(initialized); - return for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data); + if (!t) + t = &default_notes_tree; + assert(t->initialized); + return for_each_note_helper(t->root, 0, 0, flags, fn, cb_data); } -int write_notes_tree(unsigned char *result) +int write_notes_tree(struct notes_tree *t, unsigned char *result) { struct tree_write_stack root; struct write_each_note_data cb_data; int ret; - assert(initialized); + if (!t) + t = &default_notes_tree; + assert(t->initialized); /* Prepare for traversal of current notes tree */ root.next = NULL; /* last forward entry in list is grounded */ @@ -740,7 +756,7 @@ int write_notes_tree(unsigned char *result) cb_data.root = &root; /* Write tree objects representing current notes tree */ - ret = for_each_note(FOR_EACH_NOTE_DONT_UNPACK_SUBTREES | + ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES | FOR_EACH_NOTE_YIELD_SUBTREES, write_each_note, &cb_data) || tree_write_stack_finish_subtree(&root) || @@ -749,15 +765,19 @@ int write_notes_tree(unsigned char *result) return ret; } -void free_notes(void) +void free_notes(struct notes_tree *t) { - note_tree_free(&root_node); - memset(&root_node, 0, sizeof(struct int_node)); - initialized = 0; + if (!t) + t = &default_notes_tree; + if (t->root) + note_tree_free(t->root); + free(t->root); + free(t->ref); + memset(t, 0, sizeof(struct notes_tree)); } -void format_note(const unsigned char *object_sha1, struct strbuf *sb, - const char *output_encoding, int flags) +void format_note(struct notes_tree *t, const unsigned char *object_sha1, + struct strbuf *sb, const char *output_encoding, int flags) { static const char utf8[] = "utf-8"; const unsigned char *sha1; @@ -765,10 +785,12 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb, unsigned long linelen, msglen; enum object_type type; - if (!initialized) - init_notes(NULL, 0); + if (!t) + t = &default_notes_tree; + if (!t->initialized) + init_notes(t, NULL, 0); - sha1 = get_note(object_sha1); + sha1 = get_note(t, object_sha1); if (!sha1) return; -- cgit v1.2.1 From 73f464b5f3fe4dd5109b9fb9e58c1fe55393902d Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:19 +0100 Subject: Refactor notes concatenation into a flexible interface for combining notes When adding a note to an object that already has an existing note, the current solution is to concatenate the contents of the two notes. However, the caller may instead wish to _overwrite_ the existing note with the new note, or maybe even _ignore_ the new note, and keep the existing one. There might also be other ways of combining notes that are only known to the caller. Therefore, instead of unconditionally concatenating notes, we let the caller specify how to combine notes, by passing in a pointer to a function for combining notes. The caller may choose to implement its own function for notes combining, but normally one of the following three conveniently supplied notes combination functions will be sufficient: - combine_notes_concatenate() combines the two notes by appending the contents of the new note to the contents of the existing note. - combine_notes_overwrite() replaces the existing note with the new note. - combine_notes_ignore() keeps the existing note, and ignores the new note. A combine_notes function can be passed to init_notes() to choose a default combine_notes function for that notes tree. If NULL is given, the notes tree falls back to combine_notes_concatenate() as the ultimate default. A combine_notes function can also be passed directly to add_note(), to control the notes combining behaviour for a note addition in particular. If NULL is passed, the combine_notes function registered for the given notes tree is used. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 138 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 80 insertions(+), 58 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 08a369af82..dc4e4f619f 100644 --- a/notes.c +++ b/notes.c @@ -1,5 +1,6 @@ #include "cache.h" #include "notes.h" +#include "blob.h" #include "tree.h" #include "utf8.h" #include "strbuf.h" @@ -127,55 +128,12 @@ static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, return NULL; } -/* Create a new blob object by concatenating the two given blob objects */ -static int concatenate_notes(unsigned char *cur_sha1, - const unsigned char *new_sha1) -{ - char *cur_msg, *new_msg, *buf; - unsigned long cur_len, new_len, buf_len; - enum object_type cur_type, new_type; - int ret; - - /* read in both note blob objects */ - new_msg = read_sha1_file(new_sha1, &new_type, &new_len); - if (!new_msg || !new_len || new_type != OBJ_BLOB) { - free(new_msg); - return 0; - } - cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len); - if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) { - free(cur_msg); - free(new_msg); - hashcpy(cur_sha1, new_sha1); - return 0; - } - - /* we will separate the notes by a newline anyway */ - if (cur_msg[cur_len - 1] == '\n') - cur_len--; - - /* concatenate cur_msg and new_msg into buf */ - buf_len = cur_len + 1 + new_len; - buf = (char *) xmalloc(buf_len); - memcpy(buf, cur_msg, cur_len); - buf[cur_len] = '\n'; - memcpy(buf + cur_len + 1, new_msg, new_len); - - free(cur_msg); - free(new_msg); - - /* create a new blob object from buf */ - ret = write_sha1_file(buf, buf_len, "blob", cur_sha1); - free(buf); - return ret; -} - /* * To insert a leaf_node: * Search to the tree location appropriate for the given leaf_node's key: * - If location is unused (NULL), store the tweaked pointer directly there * - If location holds a note entry that matches the note-to-be-inserted, then - * concatenate the two notes. + * combine the two notes (by calling the given combine_notes function). * - If location holds a note entry that matches the subtree-to-be-inserted, * then unpack the subtree-to-be-inserted into the location. * - If location holds a matching subtree entry, unpack the subtree at that @@ -184,7 +142,8 @@ static int concatenate_notes(unsigned char *cur_sha1, * node-to-be-inserted, and store the new int_node into the location. */ static void note_tree_insert(struct int_node *tree, unsigned char n, - struct leaf_node *entry, unsigned char type) + struct leaf_node *entry, unsigned char type, + combine_notes_fn combine_notes) { struct int_node *new_node; struct leaf_node *l; @@ -205,12 +164,11 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, if (!hashcmp(l->val_sha1, entry->val_sha1)) return; - if (concatenate_notes(l->val_sha1, - entry->val_sha1)) - die("failed to concatenate note %s " - "into note %s for object %s", - sha1_to_hex(entry->val_sha1), + if (combine_notes(l->val_sha1, entry->val_sha1)) + die("failed to combine notes %s and %s" + " for object %s", sha1_to_hex(l->val_sha1), + sha1_to_hex(entry->val_sha1), sha1_to_hex(l->key_sha1)); free(entry); return; @@ -233,7 +191,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, *p = NULL; load_subtree(l, tree, n); free(l); - note_tree_insert(tree, n, entry, type); + note_tree_insert(tree, n, entry, type, combine_notes); return; } break; @@ -243,9 +201,9 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE || GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1); - note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p)); + note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes); *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); - note_tree_insert(new_node, n + 1, entry, type); + note_tree_insert(new_node, n + 1, entry, type, combine_notes); } /* @@ -406,7 +364,8 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, l->key_sha1[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } - note_tree_insert(node, n, l, type); + note_tree_insert(node, n, l, type, + combine_notes_concatenate); } } free(buf); @@ -659,7 +618,64 @@ static int write_each_note(const unsigned char *object_sha1, return write_each_note_helper(d->root, note_path, mode, note_sha1); } -void init_notes(struct notes_tree *t, const char *notes_ref, int flags) +int combine_notes_concatenate(unsigned char *cur_sha1, + const unsigned char *new_sha1) +{ + char *cur_msg = NULL, *new_msg = NULL, *buf; + unsigned long cur_len, new_len, buf_len; + enum object_type cur_type, new_type; + int ret; + + /* read in both note blob objects */ + if (!is_null_sha1(new_sha1)) + new_msg = read_sha1_file(new_sha1, &new_type, &new_len); + if (!new_msg || !new_len || new_type != OBJ_BLOB) { + free(new_msg); + return 0; + } + if (!is_null_sha1(cur_sha1)) + cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len); + if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) { + free(cur_msg); + free(new_msg); + hashcpy(cur_sha1, new_sha1); + return 0; + } + + /* we will separate the notes by a newline anyway */ + if (cur_msg[cur_len - 1] == '\n') + cur_len--; + + /* concatenate cur_msg and new_msg into buf */ + buf_len = cur_len + 1 + new_len; + buf = (char *) xmalloc(buf_len); + memcpy(buf, cur_msg, cur_len); + buf[cur_len] = '\n'; + memcpy(buf + cur_len + 1, new_msg, new_len); + free(cur_msg); + free(new_msg); + + /* create a new blob object from buf */ + ret = write_sha1_file(buf, buf_len, blob_type, cur_sha1); + free(buf); + return ret; +} + +int combine_notes_overwrite(unsigned char *cur_sha1, + const unsigned char *new_sha1) +{ + hashcpy(cur_sha1, new_sha1); + return 0; +} + +int combine_notes_ignore(unsigned char *cur_sha1, + const unsigned char *new_sha1) +{ + return 0; +} + +void init_notes(struct notes_tree *t, const char *notes_ref, + combine_notes_fn combine_notes, int flags) { unsigned char sha1[20], object_sha1[20]; unsigned mode; @@ -676,8 +692,12 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags) if (!notes_ref) notes_ref = GIT_NOTES_DEFAULT_REF; + if (!combine_notes) + combine_notes = combine_notes_concatenate; + t->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1); t->ref = notes_ref ? xstrdup(notes_ref) : NULL; + t->combine_notes = combine_notes; t->initialized = 1; if (flags & NOTES_INIT_EMPTY || !notes_ref || @@ -693,17 +713,19 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags) } void add_note(struct notes_tree *t, const unsigned char *object_sha1, - const unsigned char *note_sha1) + const unsigned char *note_sha1, combine_notes_fn combine_notes) { struct leaf_node *l; if (!t) t = &default_notes_tree; assert(t->initialized); + if (!combine_notes) + combine_notes = t->combine_notes; l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, note_sha1); - note_tree_insert(t->root, 0, l, PTR_TYPE_NOTE); + note_tree_insert(t->root, 0, l, PTR_TYPE_NOTE, combine_notes); } void remove_note(struct notes_tree *t, const unsigned char *object_sha1) @@ -788,7 +810,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1, if (!t) t = &default_notes_tree; if (!t->initialized) - init_notes(t, NULL, 0); + init_notes(t, NULL, NULL, 0); sha1 = get_note(t, object_sha1); if (!sha1) -- cgit v1.2.1 From 851c2b3791f24e319c23331887d4b8150ca4d9ba Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:23 +0100 Subject: Teach notes code to properly preserve non-notes in the notes tree The note tree structure allows for non-note entries to coexist with note entries in a notes tree. Although we certainly expect there to be very few non-notes in a notes tree, we should still support them to a certain degree. This patch teaches the notes code to preserve non-notes when updating the notes tree with write_notes_tree(). Non-notes are not affected by fanout restructuring. For non-notes to be handled correctly, we can no longer allow subtree entries that do not match the fanout structure produced by the notes code itself. This means that fanouts like 4/36, 6/34, 8/32, 4/4/32, etc. are no longer recognized as note subtrees; only 2-based fanouts are allowed (2/38, 2/2/36, 2/2/2/34, etc.). Since the notes code has never at any point _produced_ non-2-based fanouts, it is highly unlikely that this change will cause problems for anyone. The patch also adds some tests verifying the correct handling of non-notes in a notes tree. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 180 insertions(+), 39 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index dc4e4f619f..d432517587 100644 --- a/notes.c +++ b/notes.c @@ -37,6 +37,21 @@ struct leaf_node { unsigned char val_sha1[20]; }; +/* + * A notes tree may contain entries that are not notes, and that do not follow + * the naming conventions of notes. There are typically none/few of these, but + * we still need to keep track of them. Keep a simple linked list sorted alpha- + * betically on the non-note path. The list is populated when parsing tree + * objects in load_subtree(), and the non-notes are correctly written back into + * the tree objects produced by write_notes_tree(). + */ +struct non_note { + struct non_note *next; /* grounded (last->next == NULL) */ + char *path; + unsigned int mode; + unsigned char sha1[20]; +}; + #define PTR_TYPE_NULL 0 #define PTR_TYPE_INTERNAL 1 #define PTR_TYPE_NOTE 2 @@ -53,8 +68,8 @@ struct leaf_node { struct notes_tree default_notes_tree; -static void load_subtree(struct leaf_node *subtree, struct int_node *node, - unsigned int n); +static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, + struct int_node *node, unsigned int n); /* * Search the tree until the appropriate location for the given key is found: @@ -71,7 +86,7 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, * - an unused leaf node (NULL) * In any case, set *tree and *n, and return pointer to the tree location. */ -static void **note_tree_search(struct int_node **tree, +static void **note_tree_search(struct notes_tree *t, struct int_node **tree, unsigned char *n, const unsigned char *key_sha1) { struct leaf_node *l; @@ -83,9 +98,9 @@ static void **note_tree_search(struct int_node **tree, if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { /* unpack tree and resume search */ (*tree)->a[0] = NULL; - load_subtree(l, *tree, *n); + load_subtree(t, l, *tree, *n); free(l); - return note_tree_search(tree, n, key_sha1); + return note_tree_search(t, tree, n, key_sha1); } } @@ -95,15 +110,15 @@ static void **note_tree_search(struct int_node **tree, case PTR_TYPE_INTERNAL: *tree = CLR_PTR_TYPE(p); (*n)++; - return note_tree_search(tree, n, key_sha1); + return note_tree_search(t, tree, n, key_sha1); case PTR_TYPE_SUBTREE: l = (struct leaf_node *) CLR_PTR_TYPE(p); if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) { /* unpack tree and resume search */ (*tree)->a[i] = NULL; - load_subtree(l, *tree, *n); + load_subtree(t, l, *tree, *n); free(l); - return note_tree_search(tree, n, key_sha1); + return note_tree_search(t, tree, n, key_sha1); } /* fall through */ default: @@ -116,10 +131,11 @@ static void **note_tree_search(struct int_node **tree, * Search to the tree location appropriate for the given key: * If a note entry with matching key, return the note entry, else return NULL. */ -static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, +static struct leaf_node *note_tree_find(struct notes_tree *t, + struct int_node *tree, unsigned char n, const unsigned char *key_sha1) { - void **p = note_tree_search(&tree, &n, key_sha1); + void **p = note_tree_search(t, &tree, &n, key_sha1); if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) { struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p); if (!hashcmp(key_sha1, l->key_sha1)) @@ -141,13 +157,13 @@ static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n, * - Else, create a new int_node, holding both the node-at-location and the * node-to-be-inserted, and store the new int_node into the location. */ -static void note_tree_insert(struct int_node *tree, unsigned char n, - struct leaf_node *entry, unsigned char type, +static void note_tree_insert(struct notes_tree *t, struct int_node *tree, + unsigned char n, struct leaf_node *entry, unsigned char type, combine_notes_fn combine_notes) { struct int_node *new_node; struct leaf_node *l; - void **p = note_tree_search(&tree, &n, entry->key_sha1); + void **p = note_tree_search(t, &tree, &n, entry->key_sha1); assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ l = (struct leaf_node *) CLR_PTR_TYPE(*p); @@ -178,7 +194,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1, entry->key_sha1)) { /* unpack 'entry' */ - load_subtree(entry, tree, n); + load_subtree(t, entry, tree, n); free(entry); return; } @@ -189,9 +205,10 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) { /* unpack 'l' and restart insert */ *p = NULL; - load_subtree(l, tree, n); + load_subtree(t, l, tree, n); free(l); - note_tree_insert(tree, n, entry, type, combine_notes); + note_tree_insert(t, tree, n, entry, type, + combine_notes); return; } break; @@ -201,9 +218,10 @@ static void note_tree_insert(struct int_node *tree, unsigned char n, assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE || GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1); - note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes); + note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p), + combine_notes); *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); - note_tree_insert(new_node, n + 1, entry, type, combine_notes); + note_tree_insert(t, new_node, n + 1, entry, type, combine_notes); } /* @@ -249,7 +267,7 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree, struct leaf_node *l; struct int_node *parent_stack[20]; unsigned char i, j; - void **p = note_tree_search(&tree, &n, entry->key_sha1); + void **p = note_tree_search(t, &tree, &n, entry->key_sha1); assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE) @@ -324,14 +342,67 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len, return len; } -static void load_subtree(struct leaf_node *subtree, struct int_node *node, - unsigned int n) +static int non_note_cmp(const struct non_note *a, const struct non_note *b) +{ + return strcmp(a->path, b->path); +} + +static void add_non_note(struct notes_tree *t, const char *path, + unsigned int mode, const unsigned char *sha1) +{ + struct non_note *p = t->prev_non_note, *n; + n = (struct non_note *) xmalloc(sizeof(struct non_note)); + n->next = NULL; + n->path = xstrdup(path); + n->mode = mode; + hashcpy(n->sha1, sha1); + t->prev_non_note = n; + + if (!t->first_non_note) { + t->first_non_note = n; + return; + } + + if (non_note_cmp(p, n) < 0) + ; /* do nothing */ + else if (non_note_cmp(t->first_non_note, n) <= 0) + p = t->first_non_note; + else { + /* n sorts before t->first_non_note */ + n->next = t->first_non_note; + t->first_non_note = n; + return; + } + + /* n sorts equal or after p */ + while (p->next && non_note_cmp(p->next, n) <= 0) + p = p->next; + + if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */ + assert(strcmp(p->path, n->path) == 0); + p->mode = n->mode; + hashcpy(p->sha1, n->sha1); + free(n); + t->prev_non_note = p; + return; + } + + /* n sorts between p and p->next */ + n->next = p->next; + p->next = n; +} + +static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, + struct int_node *node, unsigned int n) { unsigned char object_sha1[20]; unsigned int prefix_len; void *buf; struct tree_desc desc; struct name_entry entry; + int len, path_len; + unsigned char type; + struct leaf_node *l; buf = fill_tree_descriptor(&desc, subtree->val_sha1); if (!buf) @@ -342,31 +413,68 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node, assert(prefix_len * 2 >= n); memcpy(object_sha1, subtree->key_sha1, prefix_len); while (tree_entry(&desc, &entry)) { - int len = get_sha1_hex_segment(entry.path, strlen(entry.path), + path_len = strlen(entry.path); + len = get_sha1_hex_segment(entry.path, path_len, object_sha1 + prefix_len, 20 - prefix_len); if (len < 0) - continue; /* entry.path is not a SHA1 sum. Skip */ + goto handle_non_note; /* entry.path is not a SHA1 */ len += prefix_len; /* * If object SHA1 is complete (len == 20), assume note object - * If object SHA1 is incomplete (len < 20), assume note subtree + * If object SHA1 is incomplete (len < 20), and current + * component consists of 2 hex chars, assume note subtree */ if (len <= 20) { - unsigned char type = PTR_TYPE_NOTE; - struct leaf_node *l = (struct leaf_node *) + type = PTR_TYPE_NOTE; + l = (struct leaf_node *) xcalloc(sizeof(struct leaf_node), 1); hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, entry.sha1); if (len < 20) { - if (!S_ISDIR(entry.mode)) - continue; /* entry cannot be subtree */ + if (!S_ISDIR(entry.mode) || path_len != 2) + goto handle_non_note; /* not subtree */ l->key_sha1[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } - note_tree_insert(node, n, l, type, + note_tree_insert(t, node, n, l, type, combine_notes_concatenate); } + continue; + +handle_non_note: + /* + * Determine full path for this non-note entry: + * The filename is already found in entry.path, but the + * directory part of the path must be deduced from the subtree + * containing this entry. We assume here that the overall notes + * tree follows a strict byte-based progressive fanout + * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not + * e.g. 4/36 fanout). This means that if a non-note is found at + * path "dead/beef", the following code will register it as + * being found on "de/ad/beef". + * On the other hand, if you use such non-obvious non-note + * paths in the middle of a notes tree, you deserve what's + * coming to you ;). Note that for non-notes that are not + * SHA1-like at the top level, there will be no problems. + * + * To conclude, it is strongly advised to make sure non-notes + * have at least one non-hex character in the top-level path + * component. + */ + { + char non_note_path[PATH_MAX]; + char *p = non_note_path; + const char *q = sha1_to_hex(subtree->key_sha1); + int i; + for (i = 0; i < prefix_len; i++) { + *p++ = *q++; + *p++ = *q++; + *p++ = '/'; + } + strcpy(p, entry.path); + add_non_note(t, non_note_path, entry.mode, entry.sha1); + } } free(buf); } @@ -426,9 +534,9 @@ static void construct_path_with_fanout(const unsigned char *sha1, strcpy(path + i, hex_sha1 + j); } -static int for_each_note_helper(struct int_node *tree, unsigned char n, - unsigned char fanout, int flags, each_note_fn fn, - void *cb_data) +static int for_each_note_helper(struct notes_tree *t, struct int_node *tree, + unsigned char n, unsigned char fanout, int flags, + each_note_fn fn, void *cb_data) { unsigned int i; void *p; @@ -443,7 +551,7 @@ redo: switch (GET_PTR_TYPE(p)) { case PTR_TYPE_INTERNAL: /* recurse into int_node */ - ret = for_each_note_helper(CLR_PTR_TYPE(p), n + 1, + ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1, fanout, flags, fn, cb_data); break; case PTR_TYPE_SUBTREE: @@ -481,7 +589,7 @@ redo: !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) { /* unpack subtree and resume traversal */ tree->a[i] = NULL; - load_subtree(l, tree, n); + load_subtree(t, l, tree, n); free(l); goto redo; } @@ -596,8 +704,29 @@ static int write_each_note_helper(struct tree_write_stack *tws, struct write_each_note_data { struct tree_write_stack *root; + struct non_note *next_non_note; }; +static int write_each_non_note_until(const char *note_path, + struct write_each_note_data *d) +{ + struct non_note *n = d->next_non_note; + int cmp, ret; + while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) { + if (note_path && cmp == 0) + ; /* do nothing, prefer note to non-note */ + else { + ret = write_each_note_helper(d->root, n->path, n->mode, + n->sha1); + if (ret) + return ret; + } + n = n->next; + } + d->next_non_note = n; + return 0; +} + static int write_each_note(const unsigned char *object_sha1, const unsigned char *note_sha1, char *note_path, void *cb_data) @@ -615,7 +744,9 @@ static int write_each_note(const unsigned char *object_sha1, } assert(note_path_len <= 40 + 19); - return write_each_note_helper(d->root, note_path, mode, note_sha1); + /* Weave non-note entries into note entries */ + return write_each_non_note_until(note_path, d) || + write_each_note_helper(d->root, note_path, mode, note_sha1); } int combine_notes_concatenate(unsigned char *cur_sha1, @@ -696,6 +827,8 @@ void init_notes(struct notes_tree *t, const char *notes_ref, combine_notes = combine_notes_concatenate; t->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1); + t->first_non_note = NULL; + t->prev_non_note = NULL; t->ref = notes_ref ? xstrdup(notes_ref) : NULL; t->combine_notes = combine_notes; t->initialized = 1; @@ -709,7 +842,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, hashclr(root_tree.key_sha1); hashcpy(root_tree.val_sha1, sha1); - load_subtree(&root_tree, t->root, 0); + load_subtree(t, &root_tree, t->root, 0); } void add_note(struct notes_tree *t, const unsigned char *object_sha1, @@ -725,7 +858,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1, l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, note_sha1); - note_tree_insert(t->root, 0, l, PTR_TYPE_NOTE, combine_notes); + 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) @@ -748,7 +881,7 @@ const unsigned char *get_note(struct notes_tree *t, if (!t) t = &default_notes_tree; assert(t->initialized); - found = note_tree_find(t->root, 0, object_sha1); + found = note_tree_find(t, t->root, 0, object_sha1); return found ? found->val_sha1 : NULL; } @@ -758,7 +891,7 @@ int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, if (!t) t = &default_notes_tree; assert(t->initialized); - return for_each_note_helper(t->root, 0, 0, flags, fn, cb_data); + return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data); } int write_notes_tree(struct notes_tree *t, unsigned char *result) @@ -776,11 +909,13 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result) strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */ root.path[0] = root.path[1] = '\0'; cb_data.root = &root; + cb_data.next_non_note = t->first_non_note; /* Write tree objects representing current notes tree */ ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES | FOR_EACH_NOTE_YIELD_SUBTREES, write_each_note, &cb_data) || + write_each_non_note_until(NULL, &cb_data) || tree_write_stack_finish_subtree(&root) || write_sha1_file(root.buf.buf, root.buf.len, tree_type, result); strbuf_release(&root.buf); @@ -794,6 +929,12 @@ void free_notes(struct notes_tree *t) if (t->root) note_tree_free(t->root); free(t->root); + while (t->first_non_note) { + t->prev_non_note = t->first_non_note->next; + free(t->first_non_note->path); + free(t->first_non_note); + t->first_non_note = t->prev_non_note; + } free(t->ref); memset(t, 0, sizeof(struct notes_tree)); } -- cgit v1.2.1 From 00fbe63627b72c807e558643f0634e435137122f Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:27 +0100 Subject: Notes API: prune_notes(): Prune notes that belong to non-existing objects When an object is made unreachable by Git, any notes that annotate that object are not automagically made unreachable, since all notes are always trivially reachable from a notes ref. In order to remove notes for non-existing objects, we therefore need to add functionality for traversing the notes tree and explicitly removing references to notes that annotate non-reachable objects. Thus the notes objects themselves also become unreachable, and are removed by a later garbage collect. prune_notes() performs this traversal (by using for_each_note() internally), and removes the notes in question from the notes tree. Note that the effect of prune_notes() is not persistent unless a subsequent call to write_notes_tree() is made. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index d432517587..3ba3e6de17 100644 --- a/notes.c +++ b/notes.c @@ -749,6 +749,29 @@ static int write_each_note(const unsigned char *object_sha1, write_each_note_helper(d->root, note_path, mode, note_sha1); } +struct note_delete_list { + struct note_delete_list *next; + const unsigned char *sha1; +}; + +static int prune_notes_helper(const unsigned char *object_sha1, + const unsigned char *note_sha1, char *note_path, + void *cb_data) +{ + struct note_delete_list **l = (struct note_delete_list **) cb_data; + struct note_delete_list *n; + + if (has_sha1_file(object_sha1)) + return 0; /* nothing to do for this note */ + + /* failed to find object => prune this note */ + n = (struct note_delete_list *) xmalloc(sizeof(*n)); + n->next = *l; + n->sha1 = object_sha1; + *l = n; + return 0; +} + int combine_notes_concatenate(unsigned char *cur_sha1, const unsigned char *new_sha1) { @@ -922,6 +945,22 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result) return ret; } +void prune_notes(struct notes_tree *t) +{ + struct note_delete_list *l = NULL; + + if (!t) + t = &default_notes_tree; + assert(t->initialized); + + for_each_note(t, 0, prune_notes_helper, &l); + + while (l) { + remove_note(t, l->sha1); + l = l->next; + } +} + void free_notes(struct notes_tree *t) { if (!t) -- cgit v1.2.1 From c88f0cc78e2bd387c9a2a47973a3c0a3b6328fec Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 24 Feb 2010 21:39:06 -0800 Subject: notes: fix malformed tree entry The mode bits for entries in a tree object should be an octal number with minimum number of digits. Do not pad it with 0 to the left. Signed-off-by: Junio C Hamano --- notes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 3ba3e6de17..a4f9926d4d 100644 --- a/notes.c +++ b/notes.c @@ -624,8 +624,8 @@ static void write_tree_entry(struct strbuf *buf, unsigned int mode, const char *path, unsigned int path_len, const unsigned char *sha1) { - strbuf_addf(buf, "%06o %.*s%c", mode, path_len, path, '\0'); - strbuf_add(buf, sha1, 20); + strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0'); + strbuf_add(buf, sha1, 20); } static void tree_write_stack_init_subtree(struct tree_write_stack *tws, -- cgit v1.2.1 From 894a9d333e9e2015cad00d95250b7c5d3acea8b6 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Fri, 12 Mar 2010 18:04:26 +0100 Subject: Support showing notes from more than one notes tree With this patch, you can set notes.displayRef to a glob that points at your favourite notes refs, e.g., [notes] displayRef = refs/notes/* Then git-log and friends will show notes from all trees. Thanks to Junio C Hamano for lots of feedback, which greatly influenced the design of the entire series and this commit in particular. Signed-off-by: Thomas Rast Acked-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 162 insertions(+), 7 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 3ba3e6de17..225a16608a 100644 --- a/notes.c +++ b/notes.c @@ -5,6 +5,8 @@ #include "utf8.h" #include "strbuf.h" #include "tree-walk.h" +#include "string-list.h" +#include "refs.h" /* * Use a non-balancing simple 16-tree structure with struct int_node as @@ -68,6 +70,9 @@ struct non_note { struct notes_tree default_notes_tree; +static struct string_list display_notes_refs; +static struct notes_tree **display_notes_trees; + static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, struct int_node *node, unsigned int n); @@ -828,6 +833,83 @@ int combine_notes_ignore(unsigned char *cur_sha1, return 0; } +static int string_list_add_one_ref(const char *path, const unsigned char *sha1, + int flag, void *cb) +{ + struct string_list *refs = cb; + if (!unsorted_string_list_has_string(refs, path)) + string_list_append(path, refs); + return 0; +} + +void string_list_add_refs_by_glob(struct string_list *list, const char *glob) +{ + if (has_glob_specials(glob)) { + for_each_glob_ref(string_list_add_one_ref, glob, list); + } else { + unsigned char sha1[20]; + 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); + } +} + +void string_list_add_refs_from_colon_sep(struct string_list *list, + const char *globs) +{ + struct strbuf globbuf = STRBUF_INIT; + struct strbuf **split; + int i; + + strbuf_addstr(&globbuf, globs); + split = strbuf_split(&globbuf, ':'); + + for (i = 0; split[i]; i++) { + if (!split[i]->len) + continue; + if (split[i]->buf[split[i]->len-1] == ':') + strbuf_setlen(split[i], split[i]->len-1); + string_list_add_refs_by_glob(list, split[i]->buf); + } + + strbuf_list_free(split); + 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; + + if (*load_refs && !strcmp(k, "notes.displayref")) { + if (!v) + config_error_nonbool(k); + string_list_add_refs_by_glob(&display_notes_refs, v); + } + + return 0; +} + +static const char *default_notes_ref(void) +{ + const char *notes_ref = NULL; + if (!notes_ref) + notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT); + if (!notes_ref) + notes_ref = notes_ref_name; /* value of core.notesRef config */ + if (!notes_ref) + notes_ref = GIT_NOTES_DEFAULT_REF; + return notes_ref; +} + void init_notes(struct notes_tree *t, const char *notes_ref, combine_notes_fn combine_notes, int flags) { @@ -840,11 +922,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, assert(!t->initialized); if (!notes_ref) - notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT); - if (!notes_ref) - notes_ref = notes_ref_name; /* value of core.notesRef config */ - if (!notes_ref) - notes_ref = GIT_NOTES_DEFAULT_REF; + notes_ref = default_notes_ref(); if (!combine_notes) combine_notes = combine_notes_concatenate; @@ -868,6 +946,63 @@ 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 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; + return trees; +} + +void init_display_notes(struct display_notes_opt *opt) +{ + char *display_ref_env; + int load_config_refs = 0; + display_notes_refs.strdup_strings = 1; + + assert(!display_notes_trees); + + if (!opt || !opt->suppress_default_notes) { + string_list_append(default_notes_ref(), &display_notes_refs); + display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT); + if (display_ref_env) { + string_list_add_refs_from_colon_sep(&display_notes_refs, + display_ref_env); + load_config_refs = 0; + } else + load_config_refs = 1; + } + + 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); + + display_notes_trees = load_notes_trees(&display_notes_refs); + string_list_clear(&display_notes_refs, 0); +} + void add_note(struct notes_tree *t, const unsigned char *object_sha1, const unsigned char *note_sha1, combine_notes_fn combine_notes) { @@ -1016,8 +1151,18 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1, if (msglen && msg[msglen - 1] == '\n') msglen--; - if (flags & NOTES_SHOW_HEADER) - strbuf_addstr(sb, "\nNotes:\n"); + if (flags & NOTES_SHOW_HEADER) { + const char *ref = t->ref; + if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) { + strbuf_addstr(sb, "\nNotes:\n"); + } else { + if (!prefixcmp(ref, "refs/")) + ref += 5; + if (!prefixcmp(ref, "notes/")) + ref += 6; + strbuf_addf(sb, "\nNotes (%s):\n", ref); + } + } for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) { linelen = strchrnul(msg_p, '\n') - msg_p; @@ -1030,3 +1175,13 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1, free(msg); } + +void format_display_notes(const unsigned char *object_sha1, + struct strbuf *sb, const char *output_encoding, int flags) +{ + int i; + assert(display_notes_trees); + for (i = 0; display_notes_trees[i]; i++) + format_note(display_notes_trees[i], object_sha1, sb, + output_encoding, flags); +} -- cgit v1.2.1 From 160baa0d9cbdfcdb6251aa5ede77c59c0d53edfd Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Fri, 12 Mar 2010 18:04:31 +0100 Subject: notes: implement 'git notes copy --stdin' This implements a mass-copy command that takes a sequence of lines in the format SP [ SP ] LF on stdin, and copies each 's notes to the . The is ignored. The intent, of course, is that this can read the same input that the 'post-rewrite' hook gets. The copy_note() function is exposed for everyone's and in particular the next commit's use. Signed-off-by: Thomas Rast Acked-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 225a16608a..2feeb7bb06 100644 --- a/notes.c +++ b/notes.c @@ -1185,3 +1185,21 @@ void format_display_notes(const unsigned char *object_sha1, format_note(display_notes_trees[i], object_sha1, sb, output_encoding, flags); } + +int copy_note(struct notes_tree *t, + const unsigned char *from_obj, const unsigned char *to_obj, + int force, combine_notes_fn combine_fn) +{ + const unsigned char *note = get_note(t, from_obj); + const unsigned char *existing_note = get_note(t, to_obj); + + if (!force && existing_note) + return 1; + + if (note) + add_note(t, to_obj, note, combine_fn); + else if (existing_note) + add_note(t, to_obj, null_sha1, combine_fn); + + return 0; +} -- cgit v1.2.1 From 7f710ea98262c7d81006c16c727796d9e6aeaa81 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Fri, 12 Mar 2010 18:04:36 +0100 Subject: notes: track whether notes_trees were changed at all Currently, the notes copying is a bit wasteful since it always creates new trees, even if no notes were copied at all. Teach add_note() and remove_note() to flag the affected notes tree as changed ('dirty'). Then teach builtin/notes.c to use this knowledge and avoid committing trees that weren't changed. Signed-off-by: Thomas Rast Acked-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 2feeb7bb06..0261e7898a 100644 --- a/notes.c +++ b/notes.c @@ -933,6 +933,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, t->ref = notes_ref ? xstrdup(notes_ref) : NULL; t->combine_notes = combine_notes; t->initialized = 1; + t->dirty = 0; if (flags & NOTES_INIT_EMPTY || !notes_ref || read_ref(notes_ref, object_sha1)) @@ -1011,6 +1012,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1, if (!t) t = &default_notes_tree; assert(t->initialized); + t->dirty = 1; if (!combine_notes) combine_notes = t->combine_notes; l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); @@ -1026,6 +1028,7 @@ void remove_note(struct notes_tree *t, const unsigned char *object_sha1) if (!t) t = &default_notes_tree; assert(t->initialized); + t->dirty = 1; hashcpy(l.key_sha1, object_sha1); hashclr(l.val_sha1); return note_tree_remove(t, t->root, 0, &l); -- cgit v1.2.1 From a502ab93339adeef014e1d95cb8f2520379a8651 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Thu, 18 Mar 2010 10:03:43 -0500 Subject: notes.c: remove inappropriate call to return Signed-off-by: Brandon Casey Acked-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index a4f9926d4d..07941b7235 100644 --- a/notes.c +++ b/notes.c @@ -893,7 +893,7 @@ void remove_note(struct notes_tree *t, const unsigned char *object_sha1) assert(t->initialized); hashcpy(l.key_sha1, object_sha1); hashclr(l.val_sha1); - return note_tree_remove(t, t->root, 0, &l); + note_tree_remove(t, t->root, 0, &l); } const unsigned char *get_note(struct notes_tree *t, -- cgit v1.2.1 From a9f2adff802308481f2e638bae0c5b6e205251a3 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Fri, 14 May 2010 23:42:07 +0200 Subject: notes: dry-run and verbose options for prune Introduce -n and -v options for "git notes prune" in complete analogy to "git prune" so that one can check for dangling notes easily. The output is a list of names of objects whose notes would be resp. are removed so that one can check the object ("git show sha1") as well as the note ("git notes show sha1"). Signed-off-by: Michael J Gruber Acked-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index e425e19827..6ee04e79e9 100644 --- a/notes.c +++ b/notes.c @@ -1083,7 +1083,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 +1094,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; } } -- cgit v1.2.1 From b684e977363ee5cb53d83c69f2298d7898c5f89a Mon Sep 17 00:00:00 2001 From: Julian Phillips Date: Sat, 26 Jun 2010 00:41:34 +0100 Subject: string_list: Fix argument order for for_each_string_list Update the definition and callers of for_each_string_list to use the string_list as the first argument. This helps make the string_list API easier to use by being more consistent. Signed-off-by: Julian Phillips Signed-off-by: Junio C Hamano --- notes.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index e425e19827..70170db918 100644 --- a/notes.c +++ b/notes.c @@ -969,7 +969,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs) 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); + for_each_string_list(refs, load_one_display_note_ref, &cb_data); trees[cb_data.counter] = NULL; return trees; } @@ -996,8 +996,8 @@ 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, + for_each_string_list(opt->extra_notes_refs, + string_list_add_refs_from_list, &display_notes_refs); display_notes_trees = load_notes_trees(&display_notes_refs); -- cgit v1.2.1 From 1d2f80fa79cdc6f7f4fa1cefb47d7d19be3bc5ee Mon Sep 17 00:00:00 2001 From: Julian Phillips Date: Sat, 26 Jun 2010 00:41:38 +0100 Subject: string_list: Fix argument order for string_list_append Update the definition and callers of string_list_append to use the string_list as the first argument. This helps make the string_list API easier to use by being more consistent. Signed-off-by: Julian Phillips Signed-off-by: Junio C Hamano --- notes.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 70170db918..4a541e4371 100644 --- a/notes.c +++ b/notes.c @@ -838,7 +838,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 +851,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); } } @@ -983,7 +983,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, -- cgit v1.2.1 From 89fe121d5fd808391ee38b7f39b88cb3f912776f Mon Sep 17 00:00:00 2001 From: Ramsay Jones Date: Mon, 21 Jun 2010 19:52:29 +0100 Subject: notes: Initialise variable to appease gcc gcc version 3.4.4 thinks that the 'cmp' variable could be used while uninitialised and complains thus: notes.c: In function `write_each_non_note_until': notes.c:719: warning: 'cmp' might be used uninitialized in \ this function Note that gcc versions 4.1.2 and 4.4.0 do not issue this warning. Signed-off-by: Ramsay Jones Signed-off-by: Junio C Hamano --- notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index e425e19827..cc92cf351a 100644 --- a/notes.c +++ b/notes.c @@ -716,7 +716,7 @@ static int write_each_non_note_until(const char *note_path, struct write_each_note_data *d) { struct non_note *n = d->next_non_note; - int cmp, ret; + int cmp = 0, ret; while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) { if (note_path && cmp == 0) ; /* do nothing, prefer note to non-note */ -- cgit v1.2.1 From 8a57c6e9437eeebf849f0def03389078a510312e Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Sat, 3 Jul 2010 14:41:54 +0200 Subject: Convert the users of for_each_string_list to for_each_string_list_item macro The rule for selecting the candidates for conversion is: if the callback function returns only 0 (the condition for for_each_string_list to exit early), than it can be safely converted to the macro. A notable exception are the callers in builtin/remote.c. If converted, the readability in the file will suffer greately. Besides, the code is not very performance critical (at the moment, at least): it does output formatting of the list of remotes. Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- notes.c | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 1978244398..7fd203560a 100644 --- a/notes.c +++ b/notes.c @@ -877,14 +877,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 +939,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 +975,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); -- cgit v1.2.1 From 1ee1e43df37e53b0bc50a0eda57dd1772dc220f5 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 31 Aug 2010 17:56:50 +0200 Subject: notes: Don't create (empty) commit when removing non-existing notes Extend remove_note() in the notes API to return whether or not a note was actually removed. Use this in 'git notes remove' to skip the creation of a notes commit when no notes were actually removed. Also add a test illustrating the change in behavior. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 7fd203560a..70d00135eb 100644 --- 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); @@ -1003,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, -- cgit v1.2.1 From 327a89dca11ac78aee711e4ab440d2194b9914f8 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 9 Nov 2010 22:49:37 +0100 Subject: notes.c: Hexify SHA1 in die() message from init_notes() Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 1978244398..bb03eb0364 100644 --- a/notes.c +++ b/notes.c @@ -940,7 +940,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, return; if (get_tree_entry(object_sha1, "", sha1, &mode)) die("Failed to read notes tree referenced by %s (%s)", - notes_ref, object_sha1); + notes_ref, sha1_to_hex(object_sha1)); hashclr(root_tree.key_sha1); hashcpy(root_tree.val_sha1, sha1); -- cgit v1.2.1 From 4a9cf1cefc0fd05e0eb46f862e398f1e4ac1c9a7 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 9 Nov 2010 22:49:39 +0100 Subject: notes.h: Make default_notes_ref() available in notes API Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index bb03eb0364..d71c0a30bf 100644 --- a/notes.c +++ b/notes.c @@ -898,7 +898,7 @@ static int notes_display_config(const char *k, const char *v, void *cb) return 0; } -static const char *default_notes_ref(void) +const char *default_notes_ref(void) { const char *notes_ref = NULL; if (!notes_ref) -- cgit v1.2.1 From a5cdebea55d53406e117d9a1fd4cc316ef036553 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 9 Nov 2010 22:49:40 +0100 Subject: notes.c: Reorder functions in preparation for next commit This patch introduces no functional change. It consists solely of reordering functions in notes.c to avoid use-before-declaration errors after applying the next commit in this series. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 146 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 73 insertions(+), 73 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index d71c0a30bf..bfb3ea5b53 100644 --- a/notes.c +++ b/notes.c @@ -149,6 +149,79 @@ static struct leaf_node *note_tree_find(struct notes_tree *t, return NULL; } +/* + * How to consolidate an int_node: + * If there are > 1 non-NULL entries, give up and return non-zero. + * Otherwise replace the int_node at the given index in the given parent node + * with the only entry (or a NULL entry if no entries) from the given tree, + * and return 0. + */ +static int note_tree_consolidate(struct int_node *tree, + struct int_node *parent, unsigned char index) +{ + unsigned int i; + void *p = NULL; + + assert(tree && parent); + assert(CLR_PTR_TYPE(parent->a[index]) == tree); + + for (i = 0; i < 16; i++) { + if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) { + if (p) /* more than one entry */ + return -2; + p = tree->a[i]; + } + } + + /* replace tree with p in parent[index] */ + parent->a[index] = p; + free(tree); + return 0; +} + +/* + * 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. + * - 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) +{ + struct leaf_node *l; + struct int_node *parent_stack[20]; + unsigned char i, j; + void **p = note_tree_search(t, &tree, &n, entry->key_sha1); + + assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ + if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE) + return; /* type mismatch, nothing to remove */ + l = (struct leaf_node *) CLR_PTR_TYPE(*p); + if (hashcmp(l->key_sha1, entry->key_sha1)) + return; /* key mismatch, nothing to remove */ + + /* we have found a matching entry */ + free(l); + *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL); + + /* consolidate this tree level, and parent levels, if possible */ + if (!n) + return; /* cannot consolidate top level */ + /* first, build stack of ancestors between root and current node */ + parent_stack[0] = t->root; + for (i = 0; i < n; i++) { + j = GET_NIBBLE(i, entry->key_sha1); + parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]); + } + assert(i == n && parent_stack[i] == tree); + /* next, unwind stack until note_tree_consolidate() is done */ + while (i > 0 && + !note_tree_consolidate(parent_stack[i], parent_stack[i - 1], + GET_NIBBLE(i - 1, entry->key_sha1))) + i--; +} + /* * To insert a leaf_node: * Search to the tree location appropriate for the given leaf_node's key: @@ -229,79 +302,6 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, note_tree_insert(t, new_node, n + 1, entry, type, combine_notes); } -/* - * How to consolidate an int_node: - * If there are > 1 non-NULL entries, give up and return non-zero. - * Otherwise replace the int_node at the given index in the given parent node - * with the only entry (or a NULL entry if no entries) from the given tree, - * and return 0. - */ -static int note_tree_consolidate(struct int_node *tree, - struct int_node *parent, unsigned char index) -{ - unsigned int i; - void *p = NULL; - - assert(tree && parent); - assert(CLR_PTR_TYPE(parent->a[index]) == tree); - - for (i = 0; i < 16; i++) { - if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) { - if (p) /* more than one entry */ - return -2; - p = tree->a[i]; - } - } - - /* replace tree with p in parent[index] */ - parent->a[index] = p; - free(tree); - return 0; -} - -/* - * 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. - * - 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) -{ - struct leaf_node *l; - struct int_node *parent_stack[20]; - unsigned char i, j; - void **p = note_tree_search(t, &tree, &n, entry->key_sha1); - - assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ - if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE) - return; /* type mismatch, nothing to remove */ - l = (struct leaf_node *) CLR_PTR_TYPE(*p); - if (hashcmp(l->key_sha1, entry->key_sha1)) - return; /* key mismatch, nothing to remove */ - - /* we have found a matching entry */ - free(l); - *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL); - - /* consolidate this tree level, and parent levels, if possible */ - if (!n) - return; /* cannot consolidate top level */ - /* first, build stack of ancestors between root and current node */ - parent_stack[0] = t->root; - for (i = 0; i < n; i++) { - j = GET_NIBBLE(i, entry->key_sha1); - parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]); - } - assert(i == n && parent_stack[i] == tree); - /* next, unwind stack until note_tree_consolidate() is done */ - while (i > 0 && - !note_tree_consolidate(parent_stack[i], parent_stack[i - 1], - GET_NIBBLE(i - 1, entry->key_sha1))) - i--; -} - /* Free the entire notes data contained in the given tree */ static void note_tree_free(struct int_node *tree) { -- cgit v1.2.1 From e2656c82fd836a3d410230c98f6a725368f15642 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 9 Nov 2010 22:49:41 +0100 Subject: notes.h/c: Allow combine_notes functions to remove notes Allow combine_notes functions to request that a note be removed, by setting the resulting note SHA1 to null_sha1 (0000000...). For consistency, also teach note_tree_insert() to skip insertion of an empty note (a note with entry->val_sha1 equal to null_sha1) when there is no note to combine it with. In general, an empty note (null_sha1) is treated identically to no note at all, but when adding an empty note where there already exists a non-empty note, we allow the combine_notes function to potentially record a new/changed note. Document this behaviour, and clearly specify how combine_notes functions are expected to handle null_sha1 in input. Before this patch, storing null_sha1s in the notes tree were silently allowed, causing an invalid notes tree (referring to blobs with null_sha1) to be produced by write_notes_tree(). Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index bfb3ea5b53..0c13a36eb7 100644 --- a/notes.c +++ b/notes.c @@ -248,7 +248,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, switch (GET_PTR_TYPE(*p)) { case PTR_TYPE_NULL: assert(!*p); - *p = SET_PTR_TYPE(entry, type); + if (is_null_sha1(entry->val_sha1)) + free(entry); + else + *p = SET_PTR_TYPE(entry, type); return; case PTR_TYPE_NOTE: switch (type) { @@ -264,6 +267,9 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, sha1_to_hex(l->val_sha1), sha1_to_hex(entry->val_sha1), sha1_to_hex(l->key_sha1)); + + if (is_null_sha1(l->val_sha1)) + note_tree_remove(t, tree, n, entry); free(entry); return; } @@ -295,6 +301,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, /* non-matching leaf_node */ assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE || GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); + if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */ + free(entry); + return; + } new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1); note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes); -- cgit v1.2.1 From 180619a58572b17de0ebeb96989e0aa832765186 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Mon, 15 Nov 2010 00:52:26 +0100 Subject: notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond The combine_notes_fn functions uses a non-zero return value to indicate failure. However, this return value was converted to a call to die() in note_tree_insert(). Instead, propagate this return value out to add_note(), and return it from there to enable the caller to handle errors appropriately. Existing add_note() callers are updated to die() upon failure, thus preserving the current behaviour. The only exceptions are copy_note() and notes_cache_put() where we are able to propagate the add_note() return value instead. This patch has been improved by the following contributions: - Jonathan Nieder: Future-proof by always checking add_note() return value - Jonathan Nieder: Improve clarity of final if-condition in note_tree_insert() Thanks-to: Jonathan Nieder Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 0c13a36eb7..1674391d38 100644 --- a/notes.c +++ b/notes.c @@ -235,13 +235,14 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree, * - Else, create a new int_node, holding both the node-at-location and the * node-to-be-inserted, and store the new int_node into the location. */ -static void note_tree_insert(struct notes_tree *t, struct int_node *tree, +static int note_tree_insert(struct notes_tree *t, struct int_node *tree, unsigned char n, struct leaf_node *entry, unsigned char type, combine_notes_fn combine_notes) { struct int_node *new_node; struct leaf_node *l; void **p = note_tree_search(t, &tree, &n, entry->key_sha1); + int ret = 0; assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ l = (struct leaf_node *) CLR_PTR_TYPE(*p); @@ -252,26 +253,21 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, free(entry); else *p = SET_PTR_TYPE(entry, type); - return; + return 0; case PTR_TYPE_NOTE: switch (type) { case PTR_TYPE_NOTE: if (!hashcmp(l->key_sha1, entry->key_sha1)) { /* skip concatenation if l == entry */ if (!hashcmp(l->val_sha1, entry->val_sha1)) - return; + return 0; - if (combine_notes(l->val_sha1, entry->val_sha1)) - die("failed to combine notes %s and %s" - " for object %s", - sha1_to_hex(l->val_sha1), - sha1_to_hex(entry->val_sha1), - sha1_to_hex(l->key_sha1)); - - if (is_null_sha1(l->val_sha1)) + ret = combine_notes(l->val_sha1, + entry->val_sha1); + if (!ret && is_null_sha1(l->val_sha1)) note_tree_remove(t, tree, n, entry); free(entry); - return; + return ret; } break; case PTR_TYPE_SUBTREE: @@ -280,7 +276,7 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, /* unpack 'entry' */ load_subtree(t, entry, tree, n); free(entry); - return; + return 0; } break; } @@ -291,9 +287,8 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, *p = NULL; load_subtree(t, l, tree, n); free(l); - note_tree_insert(t, tree, n, entry, type, - combine_notes); - return; + return note_tree_insert(t, tree, n, entry, type, + combine_notes); } break; } @@ -303,13 +298,15 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree, GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */ free(entry); - return; + return 0; } new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1); - note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p), - combine_notes); + ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p), + combine_notes); + if (ret) + return ret; *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); - note_tree_insert(t, new_node, n + 1, entry, type, combine_notes); + return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes); } /* Free the entire notes data contained in the given tree */ @@ -452,8 +449,12 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, l->key_sha1[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } - note_tree_insert(t, node, n, l, type, - combine_notes_concatenate); + if (note_tree_insert(t, node, n, l, type, + combine_notes_concatenate)) + die("Failed to load %s %s into notes tree " + "from %s", + type == PTR_TYPE_NOTE ? "note" : "subtree", + sha1_to_hex(l->key_sha1), t->ref); } continue; @@ -1014,7 +1015,7 @@ void init_display_notes(struct display_notes_opt *opt) string_list_clear(&display_notes_refs, 0); } -void add_note(struct notes_tree *t, const unsigned char *object_sha1, +int add_note(struct notes_tree *t, const unsigned char *object_sha1, const unsigned char *note_sha1, combine_notes_fn combine_notes) { struct leaf_node *l; @@ -1028,7 +1029,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1, l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); hashcpy(l->key_sha1, object_sha1); hashcpy(l->val_sha1, note_sha1); - note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes); + return 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) @@ -1204,7 +1205,7 @@ void format_display_notes(const unsigned char *object_sha1, int copy_note(struct notes_tree *t, const unsigned char *from_obj, const unsigned char *to_obj, - int force, combine_notes_fn combine_fn) + int force, combine_notes_fn combine_notes) { const unsigned char *note = get_note(t, from_obj); const unsigned char *existing_note = get_note(t, to_obj); @@ -1213,9 +1214,9 @@ int copy_note(struct notes_tree *t, return 1; if (note) - add_note(t, to_obj, note, combine_fn); + return add_note(t, to_obj, note, combine_notes); else if (existing_note) - add_note(t, to_obj, null_sha1, combine_fn); + return add_note(t, to_obj, null_sha1, combine_notes); return 0; } -- cgit v1.2.1 From d4990c4b2f5f7066853fea4df775b3f506c79431 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Tue, 9 Nov 2010 22:49:44 +0100 Subject: notes.c: Use two newlines (instead of one) when concatenating notes When using combine_notes_concatenate() to concatenate notes, it currently ensures exactly one newline character between the given notes. However, when using builtin/notes.c:create_note() to concatenate notes (e.g. by 'git notes append'), it adds a newline character to the trailing newline of the preceding notes object, thus resulting in _two_ newlines (aka. a blank line) separating contents of the two notes. This patch brings combine_notes_concatenate() into consistency with builtin/notes.c:create_note(), by ensuring exactly _two_ newline characters between concatenated notes. The patch also changes a few notes-related selftests accordingly. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 1674391d38..09a93abca1 100644 --- a/notes.c +++ b/notes.c @@ -812,16 +812,17 @@ int combine_notes_concatenate(unsigned char *cur_sha1, return 0; } - /* we will separate the notes by a newline anyway */ + /* we will separate the notes by two newlines anyway */ if (cur_msg[cur_len - 1] == '\n') cur_len--; /* concatenate cur_msg and new_msg into buf */ - buf_len = cur_len + 1 + new_len; + buf_len = cur_len + 2 + new_len; buf = (char *) xmalloc(buf_len); memcpy(buf, cur_msg, cur_len); buf[cur_len] = '\n'; - memcpy(buf + cur_len + 1, new_msg, new_len); + buf[cur_len + 1] = '\n'; + memcpy(buf + cur_len + 2, new_msg, new_len); free(cur_msg); free(new_msg); -- cgit v1.2.1 From a6a09095a08339afc8468d053ff978ed4662a1d5 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Mon, 15 Nov 2010 00:57:17 +0100 Subject: git notes merge: Add another auto-resolving strategy: "cat_sort_uniq" This new strategy is similar to "concatenate", but in addition to concatenating the two note candidates, this strategy sorts the resulting lines, and removes duplicate lines from the result. This is equivalent to applying the "cat | sort | uniq" shell pipeline to the two note candidates. This strategy is useful if the notes follow a line-based format where one wants to avoid duplicate lines in the merge result. Note that if either of the note candidates contain duplicate lines _prior_ to the merge, these will also be removed by this merge strategy. The patch also contains tests and documentation for the new strategy. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 09a93abca1..96cde42134 100644 --- a/notes.c +++ b/notes.c @@ -845,6 +845,82 @@ int combine_notes_ignore(unsigned char *cur_sha1, return 0; } +static int string_list_add_note_lines(struct string_list *sort_uniq_list, + const unsigned char *sha1) +{ + char *data; + unsigned long len; + enum object_type t; + struct strbuf buf = STRBUF_INIT; + struct strbuf **lines = NULL; + int i, list_index; + + if (is_null_sha1(sha1)) + return 0; + + /* read_sha1_file NUL-terminates */ + data = read_sha1_file(sha1, &t, &len); + if (t != OBJ_BLOB || !data || !len) { + free(data); + return t != OBJ_BLOB || !data; + } + + strbuf_attach(&buf, data, len, len + 1); + lines = strbuf_split(&buf, '\n'); + + for (i = 0; lines[i]; i++) { + if (lines[i]->buf[lines[i]->len - 1] == '\n') + strbuf_setlen(lines[i], lines[i]->len - 1); + if (!lines[i]->len) + continue; /* skip empty lines */ + list_index = string_list_find_insert_index(sort_uniq_list, + lines[i]->buf, 0); + if (list_index < 0) + continue; /* skip duplicate lines */ + string_list_insert_at_index(sort_uniq_list, list_index, + lines[i]->buf); + } + + strbuf_list_free(lines); + strbuf_release(&buf); + return 0; +} + +static int string_list_join_lines_helper(struct string_list_item *item, + void *cb_data) +{ + struct strbuf *buf = cb_data; + strbuf_addstr(buf, item->string); + strbuf_addch(buf, '\n'); + return 0; +} + +int combine_notes_cat_sort_uniq(unsigned char *cur_sha1, + const unsigned char *new_sha1) +{ + struct string_list sort_uniq_list = { NULL, 0, 0, 1 }; + struct strbuf buf = STRBUF_INIT; + int ret = 1; + + /* read both note blob objects into unique_lines */ + if (string_list_add_note_lines(&sort_uniq_list, cur_sha1)) + goto out; + if (string_list_add_note_lines(&sort_uniq_list, new_sha1)) + goto out; + + /* create a new blob object from sort_uniq_list */ + if (for_each_string_list(&sort_uniq_list, + string_list_join_lines_helper, &buf)) + goto out; + + ret = write_sha1_file(buf.buf, buf.len, blob_type, cur_sha1); + +out: + strbuf_release(&buf); + string_list_clear(&sort_uniq_list, 0); + return ret; +} + static int string_list_add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb) { -- cgit v1.2.1