commit: convert parts to struct object_id

Convert struct commit_graft and necessary local parts of commit.c. Also, convert several constants based on the hex length of an SHA-1 to use GIT_SHA1_HEXSZ, and move several magic constants into variables for readability. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Mar 13, 2015 at 23:39 UTC 7683e2e6e301835236539a33b2fba936dd1a0a2b
6 files changed +39 -35
commit.c
+30 -26
@@ -55,12 +55,12 @@ struct commit *lookup_commit(const unsigned char *sha1)
55
56 struct commit *lookup_commit_reference_by_name(const char *name)
57 {
58 - unsigned char sha1[20];
58 + struct object_id oid;
59 struct commit *commit;
60
61 - if (get_sha1_committish(name, sha1))
61 + if (get_sha1_committish(name, oid.hash))
62 return NULL;
63 - commit = lookup_commit_reference(sha1);
63 + commit = lookup_commit_reference(oid.hash);
64 if (parse_commit(commit))
65 return NULL;
66 return commit;
@@ -99,7 +99,7 @@ static int commit_graft_alloc, commit_graft_nr;
99 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
100 {
101 struct commit_graft **commit_graft_table = table;
102 - return commit_graft_table[index]->sha1;
102 + return commit_graft_table[index]->oid.hash;
103 }
104
105 static int commit_graft_pos(const unsigned char *sha1)
@@ -110,7 +110,7 @@ static int commit_graft_pos(const unsigned char *sha1)
110
111 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
112 {
113 - int pos = commit_graft_pos(graft->sha1);
113 + int pos = commit_graft_pos(graft->oid.hash);
114
115 if (0 <= pos) {
116 if (ignore_dups)
@@ -138,22 +138,23 @@ struct commit_graft *read_graft_line(char *buf, int len)
138 /* The format is just "Commit Parent1 Parent2 ...\n" */
139 int i;
140 struct commit_graft *graft = NULL;
141 + const int entry_size = GIT_SHA1_HEXSZ + 1;
142
143 while (len && isspace(buf[len-1]))
144 buf[--len] = '\0';
145 if (buf[0] == '#' || buf[0] == '\0')
146 return NULL;
146 - if ((len + 1) % 41)
147 + if ((len + 1) % entry_size)
148 goto bad_graft_data;
148 - i = (len + 1) / 41 - 1;
149 - graft = xmalloc(sizeof(*graft) + 20 * i);
149 + i = (len + 1) / entry_size - 1;
150 + graft = xmalloc(sizeof(*graft) + GIT_SHA1_RAWSZ * i);
151 graft->nr_parent = i;
151 - if (get_sha1_hex(buf, graft->sha1))
152 + if (get_oid_hex(buf, &graft->oid))
153 goto bad_graft_data;
153 - for (i = 40; i < len; i += 41) {
154 + for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
155 if (buf[i] != ' ')
156 goto bad_graft_data;
156 - if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
157 + if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash))
158 goto bad_graft_data;
159 }
160 return graft;
@@ -302,39 +303,42 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
303 {
304 const char *tail = buffer;
305 const char *bufptr = buffer;
305 - unsigned char parent[20];
306 + struct object_id parent;
307 struct commit_list **pptr;
308 struct commit_graft *graft;
309 + const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
310 + const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
311
312 if (item->object.parsed)
313 return 0;
314 item->object.parsed = 1;
315 tail += size;
313 - if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
316 + if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
317 + bufptr[tree_entry_len] != '\n')
318 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
315 - if (get_sha1_hex(bufptr + 5, parent) < 0)
319 + if (get_sha1_hex(bufptr + 5, parent.hash) < 0)
320 return error("bad tree pointer in commit %s",
321 sha1_to_hex(item->object.sha1));
318 - item->tree = lookup_tree(parent);
319 - bufptr += 46; /* "tree " + "hex sha1" + "\n" */
322 + item->tree = lookup_tree(parent.hash);
323 + bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
324 pptr = &item->parents;
325
326 graft = lookup_commit_graft(item->object.sha1);
323 - while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
327 + while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
328 struct commit *new_parent;
329
326 - if (tail <= bufptr + 48 ||
327 - get_sha1_hex(bufptr + 7, parent) ||
328 - bufptr[47] != '\n')
330 + if (tail <= bufptr + parent_entry_len + 1 ||
331 + get_sha1_hex(bufptr + 7, parent.hash) ||
332 + bufptr[parent_entry_len] != '\n')
333 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
330 - bufptr += 48;
334 + bufptr += parent_entry_len + 1;
335 /*
336 * The clone is shallow if nr_parent < 0, and we must
337 * not traverse its real parents even when we unhide them.
338 */
339 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
340 continue;
337 - new_parent = lookup_commit(parent);
341 + new_parent = lookup_commit(parent.hash);
342 if (new_parent)
343 pptr = &commit_list_insert(new_parent, pptr)->next;
344 }
@@ -342,7 +346,7 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
346 int i;
347 struct commit *new_parent;
348 for (i = 0; i < graft->nr_parent; i++) {
345 - new_parent = lookup_commit(graft->parent[i]);
349 + new_parent = lookup_commit(graft->parent[i].hash);
350 if (!new_parent)
351 continue;
352 pptr = &commit_list_insert(new_parent, pptr)->next;
@@ -1580,10 +1584,10 @@ struct commit *get_merge_parent(const char *name)
1584 {
1585 struct object *obj;
1586 struct commit *commit;
1583 - unsigned char sha1[20];
1584 - if (get_sha1(name, sha1))
1587 + struct object_id oid;
1588 + if (get_sha1(name, oid.hash))
1589 return NULL;
1586 - obj = parse_object(sha1);
1590 + obj = parse_object(oid.hash);
1591 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1592 if (commit && !commit->util) {
1593 struct merge_remote_desc *desc;
commit.h
+2 -2
@@ -226,9 +226,9 @@ enum rev_sort_order {
226 void sort_in_topological_order(struct commit_list **, enum rev_sort_order);
227
228 struct commit_graft {
229 - unsigned char sha1[20];
229 + struct object_id oid;
230 int nr_parent; /* < 0 if shallow commit */
231 - unsigned char parent[FLEX_ARRAY][20]; /* more */
231 + struct object_id parent[FLEX_ARRAY]; /* more */
232 };
233 typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
234
log-tree.c
+1 -1
@@ -137,7 +137,7 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
137
138 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
139 {
140 - struct commit *commit = lookup_commit(graft->sha1);
140 + struct commit *commit = lookup_commit(graft->oid.hash);
141 if (!commit)
142 return 0;
143 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
send-pack.c
+1 -1
@@ -182,7 +182,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c
182 {
183 struct strbuf *sb = cb;
184 if (graft->nr_parent == -1)
185 - packet_buf_write(sb, "shallow %s\n", sha1_to_hex(graft->sha1));
185 + packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
186 return 0;
187 }
188
shallow.c
+4 -4
@@ -31,7 +31,7 @@ int register_shallow(const unsigned char *sha1)
31 xmalloc(sizeof(struct commit_graft));
32 struct commit *commit = lookup_commit(sha1);
33
34 - hashcpy(graft->sha1, sha1);
34 + hashcpy(graft->oid.hash, sha1);
35 graft->nr_parent = -1;
36 if (commit && commit->object.parsed)
37 commit->parents = NULL;
@@ -159,11 +159,11 @@ struct write_shallow_data {
159 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
160 {
161 struct write_shallow_data *data = cb_data;
162 - const char *hex = sha1_to_hex(graft->sha1);
162 + const char *hex = oid_to_hex(&graft->oid);
163 if (graft->nr_parent != -1)
164 return 0;
165 if (data->flags & SEEN_ONLY) {
166 - struct commit *c = lookup_commit(graft->sha1);
166 + struct commit *c = lookup_commit(graft->oid.hash);
167 if (!c || !(c->object.flags & SEEN)) {
168 if (data->flags & VERBOSE)
169 printf("Removing %s from .git/shallow\n",
@@ -282,7 +282,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c
282 {
283 int fd = *(int *)cb;
284 if (graft->nr_parent == -1)
285 - packet_write(fd, "shallow %s\n", sha1_to_hex(graft->sha1));
285 + packet_write(fd, "shallow %s\n", oid_to_hex(&graft->oid));
286 return 0;
287 }
288
upload-pack.c
+1 -1
@@ -74,7 +74,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
74 {
75 FILE *fp = cb_data;
76 if (graft->nr_parent == -1)
77 - fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
77 + fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
78 return 0;
79 }
80