From 5dcc969e79ebb1e9c1996ffd972bb76467d3bc84 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:37 -0700 Subject: notes: convert internal structures to struct object_id Convert the internal structures using unsigned char [20] to take struct object_id using the following semantic patch and the standard object_id transforms: @@ struct leaf_node E1; @@ - E1.key_sha1 + E1.key_oid.hash @@ struct leaf_node *E1; @@ - E1->key_sha1 + E1->key_oid.hash @@ struct leaf_node E1; @@ - E1.key_sha1 + E1.key_oid.hash @@ struct leaf_node *E1; @@ - E1->key_sha1 + E1->key_oid.hash @@ struct non_note E1; @@ - E1.sha1 + E1.oid.hash @@ struct non_note *E1; @@ - E1->sha1 + E1->oid.hash Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- notes.c | 98 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 51 insertions(+), 47 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 542563b280..251cf11c95 100644 --- a/notes.c +++ b/notes.c @@ -35,8 +35,8 @@ struct int_node { * subtree. */ struct leaf_node { - unsigned char key_sha1[20]; - unsigned char val_sha1[20]; + struct object_id key_oid; + struct object_id val_oid; }; /* @@ -51,7 +51,7 @@ struct non_note { struct non_note *next; /* grounded (last->next == NULL) */ char *path; unsigned int mode; - unsigned char sha1[20]; + struct object_id oid; }; #define PTR_TYPE_NULL 0 @@ -100,7 +100,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree, 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)) { + if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) { /* unpack tree and resume search */ (*tree)->a[0] = NULL; load_subtree(t, l, *tree, *n); @@ -118,7 +118,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree, 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)) { + if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) { /* unpack tree and resume search */ (*tree)->a[i] = NULL; load_subtree(t, l, *tree, *n); @@ -143,7 +143,7 @@ static struct leaf_node *note_tree_find(struct notes_tree *t, 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)) + if (!hashcmp(key_sha1, l->key_oid.hash)) return l; } return NULL; @@ -196,17 +196,17 @@ static void note_tree_remove(struct notes_tree *t, 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); + void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash); 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)) + if (oidcmp(&l->key_oid, &entry->key_oid)) return; /* key mismatch, nothing to remove */ /* we have found a matching entry */ - hashcpy(entry->val_sha1, l->val_sha1); + oidcpy(&entry->val_oid, &l->val_oid); free(l); *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL); @@ -216,14 +216,14 @@ static void note_tree_remove(struct notes_tree *t, /* 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); + j = GET_NIBBLE(i, entry->key_oid.hash); 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))) + GET_NIBBLE(i - 1, entry->key_oid.hash))) i--; } @@ -246,7 +246,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree, { struct int_node *new_node; struct leaf_node *l; - void **p = note_tree_search(t, &tree, &n, entry->key_sha1); + void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash); int ret = 0; assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ @@ -254,7 +254,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree, switch (GET_PTR_TYPE(*p)) { case PTR_TYPE_NULL: assert(!*p); - if (is_null_sha1(entry->val_sha1)) + if (is_null_oid(&entry->val_oid)) free(entry); else *p = SET_PTR_TYPE(entry, type); @@ -262,22 +262,22 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree, case PTR_TYPE_NOTE: switch (type) { case PTR_TYPE_NOTE: - if (!hashcmp(l->key_sha1, entry->key_sha1)) { + if (!oidcmp(&l->key_oid, &entry->key_oid)) { /* skip concatenation if l == entry */ - if (!hashcmp(l->val_sha1, entry->val_sha1)) + if (!oidcmp(&l->val_oid, &entry->val_oid)) return 0; - ret = combine_notes(l->val_sha1, - entry->val_sha1); - if (!ret && is_null_sha1(l->val_sha1)) + ret = combine_notes(l->val_oid.hash, + entry->val_oid.hash); + if (!ret && is_null_oid(&l->val_oid)) note_tree_remove(t, tree, n, entry); free(entry); return ret; } break; case PTR_TYPE_SUBTREE: - if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1, - entry->key_sha1)) { + if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash, + entry->key_oid.hash)) { /* unpack 'entry' */ load_subtree(t, entry, tree, n); free(entry); @@ -287,7 +287,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree, } break; case PTR_TYPE_SUBTREE: - if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) { + if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) { /* unpack 'l' and restart insert */ *p = NULL; load_subtree(t, l, tree, n); @@ -301,7 +301,7 @@ static int 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 */ + if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */ free(entry); return 0; } @@ -373,7 +373,7 @@ static void add_non_note(struct notes_tree *t, char *path, n->next = NULL; n->path = path; n->mode = mode; - hashcpy(n->sha1, sha1); + hashcpy(n->oid.hash, sha1); t->prev_non_note = n; if (!t->first_non_note) { @@ -399,7 +399,7 @@ static void add_non_note(struct notes_tree *t, char *path, 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); + oidcpy(&p->oid, &n->oid); free(n); t->prev_non_note = p; return; @@ -422,14 +422,14 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, unsigned char type; struct leaf_node *l; - buf = fill_tree_descriptor(&desc, subtree->val_sha1); + buf = fill_tree_descriptor(&desc, subtree->val_oid.hash); if (!buf) die("Could not read %s for notes-index", - sha1_to_hex(subtree->val_sha1)); + oid_to_hex(&subtree->val_oid)); - prefix_len = subtree->key_sha1[19]; + prefix_len = subtree->key_oid.hash[19]; assert(prefix_len * 2 >= n); - memcpy(object_sha1, subtree->key_sha1, prefix_len); + memcpy(object_sha1, subtree->key_oid.hash, prefix_len); while (tree_entry(&desc, &entry)) { path_len = strlen(entry.path); len = get_sha1_hex_segment(entry.path, path_len, @@ -447,12 +447,12 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, type = PTR_TYPE_NOTE; l = (struct leaf_node *) xcalloc(1, sizeof(struct leaf_node)); - hashcpy(l->key_sha1, object_sha1); - hashcpy(l->val_sha1, entry.oid->hash); + hashcpy(l->key_oid.hash, object_sha1); + oidcpy(&l->val_oid, entry.oid); if (len < 20) { if (!S_ISDIR(entry.mode) || path_len != 2) goto handle_non_note; /* not subtree */ - l->key_sha1[19] = (unsigned char) len; + l->key_oid.hash[19] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } if (note_tree_insert(t, node, n, l, type, @@ -460,7 +460,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, 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); + oid_to_hex(&l->key_oid), t->ref); } continue; @@ -486,7 +486,7 @@ handle_non_note: */ { struct strbuf non_note_path = STRBUF_INIT; - const char *q = sha1_to_hex(subtree->key_sha1); + const char *q = oid_to_hex(&subtree->key_oid); int i; for (i = 0; i < prefix_len; i++) { strbuf_addch(&non_note_path, *q++); @@ -599,15 +599,17 @@ redo: flags & FOR_EACH_NOTE_YIELD_SUBTREES) { /* invoke callback with subtree */ unsigned int path_len = - l->key_sha1[19] * 2 + fanout; + l->key_oid.hash[19] * 2 + fanout; assert(path_len < FANOUT_PATH_MAX - 1); - construct_path_with_fanout(l->key_sha1, fanout, + construct_path_with_fanout(l->key_oid.hash, + 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, + ret = fn(l->key_oid.hash, l->val_oid.hash, + path, cb_data); } if (n > fanout * 2 || @@ -621,8 +623,10 @@ 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); + construct_path_with_fanout(l->key_oid.hash, fanout, + path); + ret = fn(l->key_oid.hash, l->val_oid.hash, path, + cb_data); break; } if (ret) @@ -742,7 +746,7 @@ static int write_each_non_note_until(const char *note_path, ; /* do nothing, prefer note to non-note */ else { ret = write_each_note_helper(d->root, n->path, n->mode, - n->sha1); + n->oid.hash); if (ret) return ret; } @@ -1027,8 +1031,8 @@ void init_notes(struct notes_tree *t, const char *notes_ref, die("Failed to read notes tree referenced by %s (%s)", notes_ref, oid_to_hex(&object_oid)); - hashclr(root_tree.key_sha1); - hashcpy(root_tree.val_sha1, oid.hash); + oidclr(&root_tree.key_oid); + oidcpy(&root_tree.val_oid, &oid); load_subtree(t, &root_tree, t->root, 0); } @@ -1092,8 +1096,8 @@ int add_note(struct notes_tree *t, const unsigned char *object_sha1, 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); + hashcpy(l->key_oid.hash, object_sha1); + hashcpy(l->val_oid.hash, note_sha1); return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes); } @@ -1104,10 +1108,10 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1) if (!t) t = &default_notes_tree; assert(t->initialized); - hashcpy(l.key_sha1, object_sha1); - hashclr(l.val_sha1); + hashcpy(l.key_oid.hash, object_sha1); + oidclr(&l.val_oid); note_tree_remove(t, t->root, 0, &l); - if (is_null_sha1(l.val_sha1)) /* no note was removed */ + if (is_null_oid(&l.val_oid)) /* no note was removed */ return 1; t->dirty = 1; return 0; @@ -1122,7 +1126,7 @@ const unsigned char *get_note(struct notes_tree *t, t = &default_notes_tree; assert(t->initialized); found = note_tree_find(t, t->root, 0, object_sha1); - return found ? found->val_sha1 : NULL; + return found ? found->val_oid.hash : NULL; } int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, -- cgit v1.2.3 From 89c149f55b93e052e46e46ebc4d7e76b0b25a90c Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:38 -0700 Subject: notes: convert internal parts to struct object_id Convert several portions of the internals of the code to struct object_id. Introduce two macros to denote the different constants in the code: KEY_INDEX for the last byte of the object ID, and FANOUT_PATH_SEPARATORS for the number of possible path separators (on Unix, "/"). While these constants are both 19 (one less than the number of bytes in the hash), distinguish them to make the code more understandable, and define them logically based on their intended purpose. Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- notes.c | 64 +++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 251cf11c95..babe0c0ebf 100644 --- a/notes.c +++ b/notes.c @@ -65,8 +65,10 @@ struct non_note { #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f) +#define KEY_INDEX (GIT_SHA1_RAWSZ - 1) +#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1) #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ - (memcmp(key_sha1, subtree_sha1, subtree_sha1[19])) + (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX])) struct notes_tree default_notes_tree; @@ -194,7 +196,7 @@ static void note_tree_remove(struct notes_tree *t, struct leaf_node *entry) { struct leaf_node *l; - struct int_node *parent_stack[20]; + struct int_node *parent_stack[GIT_SHA1_RAWSZ]; unsigned char i, j; void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash); @@ -341,21 +343,21 @@ static void note_tree_free(struct int_node *tree) * 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) +static int get_oid_hex_segment(const char *hex, unsigned int hex_len, + unsigned char *oid, unsigned int oid_len) { unsigned int i, len = hex_len >> 1; - if (hex_len % 2 != 0 || len > sha1_len) + if (hex_len % 2 != 0 || len > oid_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; + *oid++ = val; hex += 2; } - for (; i < sha1_len; i++) - *sha1++ = 0; + for (; i < oid_len; i++) + *oid++ = 0; return len; } @@ -413,7 +415,7 @@ static void add_non_note(struct notes_tree *t, char *path, static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, struct int_node *node, unsigned int n) { - unsigned char object_sha1[20]; + struct object_id object_oid; unsigned int prefix_len; void *buf; struct tree_desc desc; @@ -427,13 +429,13 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, die("Could not read %s for notes-index", oid_to_hex(&subtree->val_oid)); - prefix_len = subtree->key_oid.hash[19]; + prefix_len = subtree->key_oid.hash[KEY_INDEX]; assert(prefix_len * 2 >= n); - memcpy(object_sha1, subtree->key_oid.hash, prefix_len); + memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len); while (tree_entry(&desc, &entry)) { path_len = strlen(entry.path); - len = get_sha1_hex_segment(entry.path, path_len, - object_sha1 + prefix_len, 20 - prefix_len); + len = get_oid_hex_segment(entry.path, path_len, + object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len); if (len < 0) goto handle_non_note; /* entry.path is not a SHA1 */ len += prefix_len; @@ -443,16 +445,16 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, * If object SHA1 is incomplete (len < 20), and current * component consists of 2 hex chars, assume note subtree */ - if (len <= 20) { + if (len <= GIT_SHA1_RAWSZ) { type = PTR_TYPE_NOTE; l = (struct leaf_node *) xcalloc(1, sizeof(struct leaf_node)); - hashcpy(l->key_oid.hash, object_sha1); + oidcpy(&l->key_oid, &object_oid); oidcpy(&l->val_oid, entry.oid); - if (len < 20) { + if (len < GIT_SHA1_RAWSZ) { if (!S_ISDIR(entry.mode) || path_len != 2) goto handle_non_note; /* not subtree */ - l->key_oid.hash[19] = (unsigned char) len; + l->key_oid.hash[KEY_INDEX] = (unsigned char) len; type = PTR_TYPE_SUBTREE; } if (note_tree_insert(t, node, n, l, type, @@ -542,14 +544,14 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n, } /* hex SHA1 + 19 * '/' + NUL */ -#define FANOUT_PATH_MAX 40 + 19 + 1 +#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 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); + assert(fanout < GIT_SHA1_RAWSZ); while (fanout) { path[i++] = hex_sha1[j++]; path[i++] = hex_sha1[j++]; @@ -599,7 +601,7 @@ redo: flags & FOR_EACH_NOTE_YIELD_SUBTREES) { /* invoke callback with subtree */ unsigned int path_len = - l->key_oid.hash[19] * 2 + fanout; + l->key_oid.hash[KEY_INDEX] * 2 + fanout; assert(path_len < FANOUT_PATH_MAX - 1); construct_path_with_fanout(l->key_oid.hash, fanout, @@ -654,7 +656,7 @@ static void write_tree_entry(struct strbuf *buf, unsigned int mode, unsigned char *sha1) { strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0'); - strbuf_add(buf, sha1, 20); + strbuf_add(buf, sha1, GIT_SHA1_RAWSZ); } static void tree_write_stack_init_subtree(struct tree_write_stack *tws, @@ -666,7 +668,7 @@ static void tree_write_stack_init_subtree(struct tree_write_stack *tws, 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 */ + strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */ n->path[0] = n->path[1] = '\0'; tws->next = n; tws->path[0] = path[0]; @@ -677,18 +679,18 @@ 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]; + struct object_id s; 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); + ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash); if (ret) return ret; strbuf_release(&n->buf); free(n); tws->next = NULL; - write_tree_entry(&tws->buf, 040000, tws->path, 2, s); + write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash); tws->path[0] = tws->path[1] = '\0'; } return 0; @@ -771,7 +773,7 @@ static int write_each_note(const unsigned char *object_sha1, note_path[note_path_len] = '\0'; mode = 040000; } - assert(note_path_len <= 40 + 19); + assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS); /* Weave non-note entries into note entries */ return write_each_non_note_until(note_path, d) || @@ -946,8 +948,8 @@ 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)) + struct object_id oid; + if (get_oid(glob, &oid)) warning("notes ref %s is invalid", glob); if (!unsorted_string_list_has_string(list, glob)) string_list_append(list, glob); @@ -1150,7 +1152,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result) /* 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 */ + strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */ root.path[0] = root.path[1] = '\0'; cb_data.root = &root; cb_data.next_non_note = t->first_non_note; @@ -1315,9 +1317,9 @@ void expand_notes_ref(struct strbuf *sb) void expand_loose_notes_ref(struct strbuf *sb) { - unsigned char object[20]; + struct object_id object; - if (get_sha1(sb->buf, object)) { + if (get_oid(sb->buf, &object)) { /* fallback to expand_notes_ref */ expand_notes_ref(sb); } -- cgit v1.2.3 From 490bc83a01acfefa11e98f8852b1f4a9dd962331 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:39 -0700 Subject: notes: convert for_each_note to struct object_id Convert for_each_note and each of the callbacks to use struct object_id. Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/notes.c | 6 +++--- notes.c | 24 ++++++++++++------------ notes.h | 4 ++-- remote-testsvn.c | 8 ++++---- 4 files changed, 21 insertions(+), 21 deletions(-) (limited to 'notes.c') diff --git a/builtin/notes.c b/builtin/notes.c index f2847c41e0..53fe6d34d4 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -109,11 +109,11 @@ static void free_note_data(struct note_data *d) strbuf_release(&d->buf); } -static int list_each_note(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +static int list_each_note(const struct object_id *object_oid, + const struct object_id *note_oid, char *note_path, void *cb_data) { - printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1)); + printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid)); return 0; } diff --git a/notes.c b/notes.c index babe0c0ebf..e881c10ee9 100644 --- a/notes.c +++ b/notes.c @@ -610,7 +610,7 @@ redo: if (path[path_len - 1] != '/') path[path_len++] = '/'; path[path_len] = '\0'; - ret = fn(l->key_oid.hash, l->val_oid.hash, + ret = fn(&l->key_oid, &l->val_oid, path, cb_data); } @@ -627,7 +627,7 @@ redo: l = (struct leaf_node *) CLR_PTR_TYPE(p); construct_path_with_fanout(l->key_oid.hash, fanout, path); - ret = fn(l->key_oid.hash, l->val_oid.hash, path, + ret = fn(&l->key_oid, &l->val_oid, path, cb_data); break; } @@ -698,7 +698,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) static int write_each_note_helper(struct tree_write_stack *tws, const char *path, unsigned int mode, - const unsigned char *sha1) + const struct object_id *oid) { size_t path_len = strlen(path); unsigned int n = 0; @@ -728,7 +728,7 @@ static int write_each_note_helper(struct tree_write_stack *tws, /* Finally add given entry to the current tree object */ write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n), - sha1); + oid->hash); return 0; } @@ -748,7 +748,7 @@ static int write_each_non_note_until(const char *note_path, ; /* do nothing, prefer note to non-note */ else { ret = write_each_note_helper(d->root, n->path, n->mode, - n->oid.hash); + &n->oid); if (ret) return ret; } @@ -758,8 +758,8 @@ static int write_each_non_note_until(const char *note_path, return 0; } -static int write_each_note(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +static int write_each_note(const struct object_id *object_oid, + const struct object_id *note_oid, char *note_path, void *cb_data) { struct write_each_note_data *d = @@ -777,7 +777,7 @@ static int write_each_note(const unsigned char *object_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); + write_each_note_helper(d->root, note_path, mode, note_oid); } struct note_delete_list { @@ -785,20 +785,20 @@ struct note_delete_list { const unsigned char *sha1; }; -static int prune_notes_helper(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +static int prune_notes_helper(const struct object_id *object_oid, + const struct object_id *note_oid, 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)) + if (has_object_file(object_oid)) 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; + n->sha1 = object_oid->hash; *l = n; return 0; } diff --git a/notes.h b/notes.h index 5345642cfd..6651673ae1 100644 --- a/notes.h +++ b/notes.h @@ -202,8 +202,8 @@ int copy_note(struct notes_tree *t, * - copy_note() * - free_notes() */ -typedef int each_note_fn(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +typedef int each_note_fn(const struct object_id *object_oid, + const struct object_id *note_oid, char *note_path, void *cb_data); int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, void *cb_data); diff --git a/remote-testsvn.c b/remote-testsvn.c index f87bf851ba..793c4ad1d9 100644 --- a/remote-testsvn.c +++ b/remote-testsvn.c @@ -99,8 +99,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res) return -1; } -static int note2mark_cb(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +static int note2mark_cb(const struct object_id *object_oid, + const struct object_id *note_oid, char *note_path, void *cb_data) { FILE *file = (FILE *)cb_data; @@ -109,14 +109,14 @@ static int note2mark_cb(const unsigned char *object_sha1, enum object_type type; struct rev_note note; - if (!(msg = read_sha1_file(note_sha1, &type, &msglen)) || + if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) || !msglen || type != OBJ_BLOB) { free(msg); return 1; } if (parse_rev_note(msg, ¬e)) return 2; - if (fprintf(file, ":%d %s\n", note.rev_nr, sha1_to_hex(object_sha1)) < 1) + if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1) return 3; return 0; } -- cgit v1.2.3 From 9ef7223058a44990dc4650ecb1209c97ceb636b3 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:40 -0700 Subject: notes: make get_note return pointer to struct object_id Make get_note return a pointer to a const struct object_id. Add a defensive check to ensure we don't accidentally dereference a NULL pointer. Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/notes.c | 22 +++++++++++----------- notes-cache.c | 8 ++++---- notes.c | 18 +++++++++--------- notes.h | 2 +- remote-testsvn.c | 6 +++--- 5 files changed, 28 insertions(+), 28 deletions(-) (limited to 'notes.c') diff --git a/builtin/notes.c b/builtin/notes.c index 53fe6d34d4..3d9005b8f4 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -351,7 +351,7 @@ static int list(int argc, const char **argv, const char *prefix) { struct notes_tree *t; unsigned char object[20]; - const unsigned char *note; + const struct object_id *note; int retval = -1; struct option options[] = { OPT_END() @@ -372,7 +372,7 @@ static int list(int argc, const char **argv, const char *prefix) die(_("failed to resolve '%s' as a valid ref."), argv[0]); note = get_note(t, object); if (note) { - puts(sha1_to_hex(note)); + puts(oid_to_hex(note)); retval = 0; } else retval = error(_("no note found for object %s."), @@ -392,7 +392,7 @@ static int add(int argc, const char **argv, const char *prefix) const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; - const unsigned char *note; + const struct object_id *note; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), @@ -453,7 +453,7 @@ static int add(int argc, const char **argv, const char *prefix) sha1_to_hex(object)); } - prepare_note_data(object, &d, note); + prepare_note_data(object, &d, note->hash); if (d.buf.len || allow_empty) { write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) @@ -474,7 +474,7 @@ static int add(int argc, const char **argv, const char *prefix) static int copy(int argc, const char **argv, const char *prefix) { int retval = 0, force = 0, from_stdin = 0; - const unsigned char *from_note, *note; + const struct object_id *from_note, *note; const char *object_ref; unsigned char object[20], from_obj[20]; struct notes_tree *t; @@ -539,7 +539,7 @@ static int copy(int argc, const char **argv, const char *prefix) goto out; } - if (add_note(t, object, from_note, combine_notes_overwrite)) + if (add_note(t, object, from_note->hash, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes copy'"); out: @@ -553,7 +553,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; - const unsigned char *note; + const struct object_id *note; char *logmsg; const char * const *usage; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; @@ -598,13 +598,13 @@ static int append_edit(int argc, const char **argv, const char *prefix) t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); note = get_note(t, object); - prepare_note_data(object, &d, edit ? note : NULL); + prepare_note_data(object, &d, edit && note ? note->hash : NULL); if (note && !edit) { /* Append buf to previous note contents */ unsigned long size; enum object_type type; - char *prev_buf = read_sha1_file(note, &type, &size); + char *prev_buf = read_sha1_file(note->hash, &type, &size); strbuf_grow(&d.buf, size + 1); if (d.buf.len && prev_buf && size) @@ -638,7 +638,7 @@ static int show(int argc, const char **argv, const char *prefix) const char *object_ref; struct notes_tree *t; unsigned char object[20]; - const unsigned char *note; + const struct object_id *note; int retval; struct option options[] = { OPT_END() @@ -664,7 +664,7 @@ static int show(int argc, const char **argv, const char *prefix) retval = error(_("no note found for object %s."), sha1_to_hex(object)); else { - const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; + const char *show_args[3] = {"show", oid_to_hex(note), NULL}; retval = execv_git_cmd(show_args); } free_notes(t); diff --git a/notes-cache.c b/notes-cache.c index 2843e98576..6e84a748f0 100644 --- a/notes-cache.c +++ b/notes-cache.c @@ -69,15 +69,15 @@ int notes_cache_write(struct notes_cache *c) char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid, size_t *outsize) { - const unsigned char *value_sha1; + const struct object_id *value_oid; enum object_type type; char *value; unsigned long size; - value_sha1 = get_note(&c->tree, key_oid->hash); - if (!value_sha1) + value_oid = get_note(&c->tree, key_oid->hash); + if (!value_oid) return NULL; - value = read_sha1_file(value_sha1, &type, &size); + value = read_sha1_file(value_oid->hash, &type, &size); *outsize = size; return value; diff --git a/notes.c b/notes.c index e881c10ee9..fe4db2c1ec 100644 --- a/notes.c +++ b/notes.c @@ -1119,7 +1119,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1) return 0; } -const unsigned char *get_note(struct notes_tree *t, +const struct object_id *get_note(struct notes_tree *t, const unsigned char *object_sha1) { struct leaf_node *found; @@ -1128,7 +1128,7 @@ const unsigned char *get_note(struct notes_tree *t, t = &default_notes_tree; assert(t->initialized); found = note_tree_find(t, t->root, 0, object_sha1); - return found ? found->val_oid.hash : NULL; + return found ? &found->val_oid : NULL; } int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, @@ -1219,7 +1219,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1, struct strbuf *sb, const char *output_encoding, int raw) { static const char utf8[] = "utf-8"; - const unsigned char *sha1; + const struct object_id *oid; char *msg, *msg_p; unsigned long linelen, msglen; enum object_type type; @@ -1229,11 +1229,11 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1, if (!t->initialized) init_notes(t, NULL, NULL, 0); - sha1 = get_note(t, object_sha1); - if (!sha1) + oid = get_note(t, object_sha1); + if (!oid) return; - if (!(msg = read_sha1_file(sha1, &type, &msglen)) || type != OBJ_BLOB) { + if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) { free(msg); return; } @@ -1291,14 +1291,14 @@ int copy_note(struct notes_tree *t, const unsigned char *from_obj, const unsigned char *to_obj, 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); + const struct object_id *note = get_note(t, from_obj); + const struct object_id *existing_note = get_note(t, to_obj); if (!force && existing_note) return 1; if (note) - return add_note(t, to_obj, note, combine_notes); + return add_note(t, to_obj, note->hash, combine_notes); else if (existing_note) return add_note(t, to_obj, null_sha1, combine_notes); diff --git a/notes.h b/notes.h index 6651673ae1..c72bb97106 100644 --- a/notes.h +++ b/notes.h @@ -140,7 +140,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1); * * Return NULL if the given object has no notes. */ -const unsigned char *get_note(struct notes_tree *t, +const struct object_id *get_note(struct notes_tree *t, const unsigned char *object_sha1); /* diff --git a/remote-testsvn.c b/remote-testsvn.c index 793c4ad1d9..017af1bd5f 100644 --- a/remote-testsvn.c +++ b/remote-testsvn.c @@ -53,15 +53,15 @@ static void terminate_batch(void) /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */ static char *read_ref_note(const unsigned char sha1[20]) { - const unsigned char *note_sha1; + const struct object_id *note_oid; char *msg = NULL; unsigned long msglen; enum object_type type; init_notes(NULL, notes_ref, NULL, 0); - if (!(note_sha1 = get_note(NULL, sha1))) + if (!(note_oid = get_note(NULL, sha1))) return NULL; /* note tree not found */ - if (!(msg = read_sha1_file(note_sha1, &type, &msglen))) + if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen))) error("Empty notes tree. %s", notes_ref); else if (!msglen || type != OBJ_BLOB) { error("Note contains unusable content. " -- cgit v1.2.3 From fb61e4d3abe2349eb0fabe0be8981626b4074fa7 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:41 -0700 Subject: notes: convert format_display_notes to struct object_id Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- log-tree.c | 2 +- notes.c | 8 ++++---- notes.h | 2 +- revision.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'notes.c') diff --git a/log-tree.c b/log-tree.c index a4ec11c2bf..9c0c64a2d8 100644 --- a/log-tree.c +++ b/log-tree.c @@ -655,7 +655,7 @@ void show_log(struct rev_info *opt) struct strbuf notebuf = STRBUF_INIT; raw = (opt->commit_format == CMIT_FMT_USERFORMAT); - format_display_notes(commit->object.oid.hash, ¬ebuf, + format_display_notes(&commit->object.oid, ¬ebuf, get_log_output_encoding(), raw); ctx.notes_message = notebuf.len ? strbuf_detach(¬ebuf, NULL) diff --git a/notes.c b/notes.c index fe4db2c1ec..b5cabafde6 100644 --- a/notes.c +++ b/notes.c @@ -1215,7 +1215,7 @@ void free_notes(struct notes_tree *t) * (raw != 0) gives the %N userformat; otherwise, the note message is given * for human consumption. */ -static void format_note(struct notes_tree *t, const unsigned char *object_sha1, +static void format_note(struct notes_tree *t, const struct object_id *object_oid, struct strbuf *sb, const char *output_encoding, int raw) { static const char utf8[] = "utf-8"; @@ -1229,7 +1229,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1, if (!t->initialized) init_notes(t, NULL, NULL, 0); - oid = get_note(t, object_sha1); + oid = get_note(t, object_oid->hash); if (!oid) return; @@ -1277,13 +1277,13 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1, free(msg); } -void format_display_notes(const unsigned char *object_sha1, +void format_display_notes(const struct object_id *object_oid, struct strbuf *sb, const char *output_encoding, int raw) { int i; assert(display_notes_trees); for (i = 0; display_notes_trees[i]; i++) - format_note(display_notes_trees[i], object_sha1, sb, + format_note(display_notes_trees[i], object_oid, sb, output_encoding, raw); } diff --git a/notes.h b/notes.h index c72bb97106..a66532103c 100644 --- a/notes.h +++ b/notes.h @@ -277,7 +277,7 @@ void init_display_notes(struct display_notes_opt *opt); * * You *must* call init_display_notes() before using this function. */ -void format_display_notes(const unsigned char *object_sha1, +void format_display_notes(const struct object_id *object_oid, struct strbuf *sb, const char *output_encoding, int raw); /* diff --git a/revision.c b/revision.c index b023945309..475d5b2dc1 100644 --- a/revision.c +++ b/revision.c @@ -2908,7 +2908,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt) if (opt->show_notes) { if (!buf.len) strbuf_addstr(&buf, message); - format_display_notes(commit->object.oid.hash, &buf, encoding, 1); + format_display_notes(&commit->object.oid, &buf, encoding, 1); } /* -- cgit v1.2.3 From 5ee8a954e0191be2a144afdda6e37ef776730246 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 30 May 2017 10:30:43 -0700 Subject: notes: convert some accessor functions to struct object_id Convert add_note, get_note, and copy_note to take struct object_id. Signed-off-by: brian m. carlson Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/notes.c | 20 ++++++++++---------- notes-cache.c | 4 ++-- notes-merge.c | 18 +++++++++--------- notes-utils.c | 2 +- notes.c | 20 ++++++++++---------- notes.h | 8 ++++---- remote-testsvn.c | 10 +++++----- 7 files changed, 41 insertions(+), 41 deletions(-) (limited to 'notes.c') diff --git a/builtin/notes.c b/builtin/notes.c index 7947a16ede..b13fc87894 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -309,7 +309,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd) if (rewrite_cmd) err = copy_note_for_rewrite(c, &from_obj, &to_obj); else - err = copy_note(t, from_obj.hash, to_obj.hash, force, + err = copy_note(t, &from_obj, &to_obj, force, combine_notes_overwrite); if (err) { @@ -370,7 +370,7 @@ static int list(int argc, const char **argv, const char *prefix) if (argc) { if (get_oid(argv[0], &object)) die(_("failed to resolve '%s' as a valid ref."), argv[0]); - note = get_note(t, object.hash); + note = get_note(t, &object); if (note) { puts(oid_to_hex(note)); retval = 0; @@ -427,7 +427,7 @@ static int add(int argc, const char **argv, const char *prefix) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("add", NOTES_INIT_WRITABLE); - note = get_note(t, object.hash); + note = get_note(t, &object); if (note) { if (!force) { @@ -456,7 +456,7 @@ static int add(int argc, const char **argv, const char *prefix) prepare_note_data(&object, &d, note->hash); if (d.buf.len || allow_empty) { write_note_data(&d, new_note.hash); - if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite)) + if (add_note(t, &object, &new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes add'"); } else { @@ -518,7 +518,7 @@ static int copy(int argc, const char **argv, const char *prefix) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("copy", NOTES_INIT_WRITABLE); - note = get_note(t, object.hash); + note = get_note(t, &object); if (note) { if (!force) { @@ -532,14 +532,14 @@ static int copy(int argc, const char **argv, const char *prefix) oid_to_hex(&object)); } - from_note = get_note(t, from_obj.hash); + from_note = get_note(t, &from_obj); if (!from_note) { retval = error(_("missing notes on source object %s. Cannot " "copy."), oid_to_hex(&from_obj)); goto out; } - if (add_note(t, object.hash, from_note->hash, combine_notes_overwrite)) + if (add_note(t, &object, from_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes copy'"); out: @@ -596,7 +596,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); - note = get_note(t, object.hash); + note = get_note(t, &object); prepare_note_data(&object, &d, edit && note ? note->hash : NULL); @@ -616,7 +616,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) if (d.buf.len || allow_empty) { write_note_data(&d, new_note.hash); - if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite)) + if (add_note(t, &object, &new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); } else { @@ -658,7 +658,7 @@ static int show(int argc, const char **argv, const char *prefix) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("show", 0); - note = get_note(t, object.hash); + note = get_note(t, &object); if (!note) retval = error(_("no note found for object %s."), diff --git a/notes-cache.c b/notes-cache.c index 6e84a748f0..29b4cede5f 100644 --- a/notes-cache.c +++ b/notes-cache.c @@ -74,7 +74,7 @@ char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid, char *value; unsigned long size; - value_oid = get_note(&c->tree, key_oid->hash); + value_oid = get_note(&c->tree, key_oid); if (!value_oid) return NULL; value = read_sha1_file(value_oid->hash, &type, &size); @@ -90,5 +90,5 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid, if (write_sha1_file(data, size, "blob", value_oid.hash) < 0) return -1; - return add_note(&c->tree, key_oid->hash, value_oid.hash, NULL); + return add_note(&c->tree, key_oid, &value_oid, NULL); } diff --git a/notes-merge.c b/notes-merge.c index 6244f6af9c..9a1a49506e 100644 --- a/notes-merge.c +++ b/notes-merge.c @@ -444,14 +444,14 @@ static int merge_one_change(struct notes_merge_options *o, if (o->verbosity >= 2) printf("Using remote notes for %s\n", oid_to_hex(&p->obj)); - if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_overwrite)) + if (add_note(t, &p->obj, &p->remote, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); return 0; case NOTES_MERGE_RESOLVE_UNION: if (o->verbosity >= 2) printf("Concatenating local and remote notes for %s\n", oid_to_hex(&p->obj)); - if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_concatenate)) + if (add_note(t, &p->obj, &p->remote, combine_notes_concatenate)) die("failed to concatenate notes " "(combine_notes_concatenate)"); return 0; @@ -459,7 +459,7 @@ static int merge_one_change(struct notes_merge_options *o, if (o->verbosity >= 2) printf("Concatenating unique lines in local and remote " "notes for %s\n", oid_to_hex(&p->obj)); - if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_cat_sort_uniq)) + if (add_note(t, &p->obj, &p->remote, combine_notes_cat_sort_uniq)) die("failed to concatenate notes " "(combine_notes_cat_sort_uniq)"); return 0; @@ -491,7 +491,7 @@ static int merge_changes(struct notes_merge_options *o, !oidcmp(&p->local, &p->base)) { /* no local change; adopt remote change */ trace_printf("\t\t\tno local change, adopted remote\n"); - if (add_note(t, p->obj.hash, p->remote.hash, + if (add_note(t, &p->obj, &p->remote, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); } else { @@ -693,12 +693,12 @@ int notes_merge_commit(struct notes_merge_options *o, baselen = path.len; while ((e = readdir(dir)) != NULL) { struct stat st; - unsigned char obj_sha1[20], blob_sha1[20]; + struct object_id obj_oid, blob_oid; if (is_dot_or_dotdot(e->d_name)) continue; - if (strlen(e->d_name) != 40 || get_sha1_hex(e->d_name, obj_sha1)) { + if (get_oid_hex(e->d_name, &obj_oid)) { if (o->verbosity >= 3) printf("Skipping non-SHA1 entry '%s%s'\n", path.buf, e->d_name); @@ -709,14 +709,14 @@ int notes_merge_commit(struct notes_merge_options *o, /* write file as blob, and add to partial_tree */ if (stat(path.buf, &st)) die_errno("Failed to stat '%s'", path.buf); - if (index_path(blob_sha1, path.buf, &st, HASH_WRITE_OBJECT)) + if (index_path(blob_oid.hash, path.buf, &st, HASH_WRITE_OBJECT)) die("Failed to write blob object from '%s'", path.buf); - if (add_note(partial_tree, obj_sha1, blob_sha1, NULL)) + if (add_note(partial_tree, &obj_oid, &blob_oid, NULL)) die("Failed to add resolved note '%s' to notes tree", path.buf); if (o->verbosity >= 4) printf("Added resolved note for object %s: %s\n", - sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1)); + oid_to_hex(&obj_oid), oid_to_hex(&blob_oid)); strbuf_setlen(&path, baselen); } diff --git a/notes-utils.c b/notes-utils.c index 7d7c22b435..b2aada90a2 100644 --- a/notes-utils.c +++ b/notes-utils.c @@ -160,7 +160,7 @@ int copy_note_for_rewrite(struct notes_rewrite_cfg *c, int ret = 0; int i; for (i = 0; c->trees[i]; i++) - ret = copy_note(c->trees[i], from_obj->hash, to_obj->hash, 1, c->combine) || ret; + ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; return ret; } diff --git a/notes.c b/notes.c index b5cabafde6..4b3a1adda5 100644 --- a/notes.c +++ b/notes.c @@ -1086,8 +1086,8 @@ void init_display_notes(struct display_notes_opt *opt) string_list_clear(&display_notes_refs, 0); } -int add_note(struct notes_tree *t, const unsigned char *object_sha1, - const unsigned char *note_sha1, combine_notes_fn combine_notes) +int add_note(struct notes_tree *t, const struct object_id *object_oid, + const struct object_id *note_oid, combine_notes_fn combine_notes) { struct leaf_node *l; @@ -1098,8 +1098,8 @@ int add_note(struct notes_tree *t, const unsigned char *object_sha1, if (!combine_notes) combine_notes = t->combine_notes; l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); - hashcpy(l->key_oid.hash, object_sha1); - hashcpy(l->val_oid.hash, note_sha1); + oidcpy(&l->key_oid, object_oid); + oidcpy(&l->val_oid, note_oid); return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes); } @@ -1120,14 +1120,14 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1) } const struct object_id *get_note(struct notes_tree *t, - const unsigned char *object_sha1) + const struct object_id *oid) { struct leaf_node *found; if (!t) t = &default_notes_tree; assert(t->initialized); - found = note_tree_find(t, t->root, 0, object_sha1); + found = note_tree_find(t, t->root, 0, oid->hash); return found ? &found->val_oid : NULL; } @@ -1229,7 +1229,7 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid if (!t->initialized) init_notes(t, NULL, NULL, 0); - oid = get_note(t, object_oid->hash); + oid = get_note(t, object_oid); if (!oid) return; @@ -1288,7 +1288,7 @@ void format_display_notes(const struct object_id *object_oid, } int copy_note(struct notes_tree *t, - const unsigned char *from_obj, const unsigned char *to_obj, + const struct object_id *from_obj, const struct object_id *to_obj, int force, combine_notes_fn combine_notes) { const struct object_id *note = get_note(t, from_obj); @@ -1298,9 +1298,9 @@ int copy_note(struct notes_tree *t, return 1; if (note) - return add_note(t, to_obj, note->hash, combine_notes); + return add_note(t, to_obj, note, combine_notes); else if (existing_note) - return add_note(t, to_obj, null_sha1, combine_notes); + return add_note(t, to_obj, &null_oid, combine_notes); return 0; } diff --git a/notes.h b/notes.h index a66532103c..3848c2fb3f 100644 --- a/notes.h +++ b/notes.h @@ -121,8 +121,8 @@ void init_notes(struct notes_tree *t, const char *notes_ref, * are not persistent until a subsequent call to write_notes_tree() returns * zero. */ -int add_note(struct notes_tree *t, const unsigned char *object_sha1, - const unsigned char *note_sha1, combine_notes_fn combine_notes); +int add_note(struct notes_tree *t, const struct object_id *object_oid, + const struct object_id *note_oid, combine_notes_fn combine_notes); /* * Remove the given note object from the given notes_tree structure @@ -141,7 +141,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1); * Return NULL if the given object has no notes. */ const struct object_id *get_note(struct notes_tree *t, - const unsigned char *object_sha1); + const struct object_id *object_oid); /* * Copy a note from one object to another in the given notes_tree. @@ -156,7 +156,7 @@ const struct object_id *get_note(struct notes_tree *t, * zero. */ int copy_note(struct notes_tree *t, - const unsigned char *from_obj, const unsigned char *to_obj, + const struct object_id *from_obj, const struct object_id *to_obj, int force, combine_notes_fn combine_notes); /* diff --git a/remote-testsvn.c b/remote-testsvn.c index 017af1bd5f..8e8d5c7947 100644 --- a/remote-testsvn.c +++ b/remote-testsvn.c @@ -51,7 +51,7 @@ static void terminate_batch(void) } /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */ -static char *read_ref_note(const unsigned char sha1[20]) +static char *read_ref_note(const struct object_id *oid) { const struct object_id *note_oid; char *msg = NULL; @@ -59,7 +59,7 @@ static char *read_ref_note(const unsigned char sha1[20]) enum object_type type; init_notes(NULL, notes_ref, NULL, 0); - if (!(note_oid = get_note(NULL, sha1))) + if (!(note_oid = get_note(NULL, oid))) return NULL; /* note tree not found */ if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen))) error("Empty notes tree. %s", notes_ref); @@ -174,15 +174,15 @@ static int cmd_import(const char *line) int code; int dumpin_fd; char *note_msg; - unsigned char head_sha1[20]; + struct object_id head_oid; unsigned int startrev; struct child_process svndump_proc = CHILD_PROCESS_INIT; const char *command = "svnrdump"; - if (read_ref(private_ref, head_sha1)) + if (read_ref(private_ref, head_oid.hash)) startrev = 0; else { - note_msg = read_ref_note(head_sha1); + note_msg = read_ref_note(&head_oid); if(note_msg == NULL) { warning("No note found for %s.", private_ref); startrev = 0; -- cgit v1.2.3