Skip to content

Commit 7683e2e

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1ff57c1 commit 7683e2e

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-35
lines changed

commit.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ struct commit *lookup_commit(const unsigned char *sha1)
5555

5656
struct commit *lookup_commit_reference_by_name(const char *name)
5757
{
58-
unsigned char sha1[20];
58+
struct object_id oid;
5959
struct commit *commit;
6060

61-
if (get_sha1_committish(name, sha1))
61+
if (get_sha1_committish(name, oid.hash))
6262
return NULL;
63-
commit = lookup_commit_reference(sha1);
63+
commit = lookup_commit_reference(oid.hash);
6464
if (parse_commit(commit))
6565
return NULL;
6666
return commit;
@@ -99,7 +99,7 @@ static int commit_graft_alloc, commit_graft_nr;
9999
static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
100100
{
101101
struct commit_graft **commit_graft_table = table;
102-
return commit_graft_table[index]->sha1;
102+
return commit_graft_table[index]->oid.hash;
103103
}
104104

105105
static int commit_graft_pos(const unsigned char *sha1)
@@ -110,7 +110,7 @@ static int commit_graft_pos(const unsigned char *sha1)
110110

111111
int register_commit_graft(struct commit_graft *graft, int ignore_dups)
112112
{
113-
int pos = commit_graft_pos(graft->sha1);
113+
int pos = commit_graft_pos(graft->oid.hash);
114114

115115
if (0 <= pos) {
116116
if (ignore_dups)
@@ -138,22 +138,23 @@ struct commit_graft *read_graft_line(char *buf, int len)
138138
/* The format is just "Commit Parent1 Parent2 ...\n" */
139139
int i;
140140
struct commit_graft *graft = NULL;
141+
const int entry_size = GIT_SHA1_HEXSZ + 1;
141142

142143
while (len && isspace(buf[len-1]))
143144
buf[--len] = '\0';
144145
if (buf[0] == '#' || buf[0] == '\0')
145146
return NULL;
146-
if ((len + 1) % 41)
147+
if ((len + 1) % entry_size)
147148
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);
150151
graft->nr_parent = i;
151-
if (get_sha1_hex(buf, graft->sha1))
152+
if (get_oid_hex(buf, &graft->oid))
152153
goto bad_graft_data;
153-
for (i = 40; i < len; i += 41) {
154+
for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
154155
if (buf[i] != ' ')
155156
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))
157158
goto bad_graft_data;
158159
}
159160
return graft;
@@ -302,47 +303,50 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
302303
{
303304
const char *tail = buffer;
304305
const char *bufptr = buffer;
305-
unsigned char parent[20];
306+
struct object_id parent;
306307
struct commit_list **pptr;
307308
struct commit_graft *graft;
309+
const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
310+
const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
308311

309312
if (item->object.parsed)
310313
return 0;
311314
item->object.parsed = 1;
312315
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')
314318
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)
316320
return error("bad tree pointer in commit %s",
317321
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" */
320324
pptr = &item->parents;
321325

322326
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)) {
324328
struct commit *new_parent;
325329

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')
329333
return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
330-
bufptr += 48;
334+
bufptr += parent_entry_len + 1;
331335
/*
332336
* The clone is shallow if nr_parent < 0, and we must
333337
* not traverse its real parents even when we unhide them.
334338
*/
335339
if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
336340
continue;
337-
new_parent = lookup_commit(parent);
341+
new_parent = lookup_commit(parent.hash);
338342
if (new_parent)
339343
pptr = &commit_list_insert(new_parent, pptr)->next;
340344
}
341345
if (graft) {
342346
int i;
343347
struct commit *new_parent;
344348
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);
346350
if (!new_parent)
347351
continue;
348352
pptr = &commit_list_insert(new_parent, pptr)->next;
@@ -1580,10 +1584,10 @@ struct commit *get_merge_parent(const char *name)
15801584
{
15811585
struct object *obj;
15821586
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))
15851589
return NULL;
1586-
obj = parse_object(sha1);
1590+
obj = parse_object(oid.hash);
15871591
commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
15881592
if (commit && !commit->util) {
15891593
struct merge_remote_desc *desc;

commit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ enum rev_sort_order {
226226
void sort_in_topological_order(struct commit_list **, enum rev_sort_order);
227227

228228
struct commit_graft {
229-
unsigned char sha1[20];
229+
struct object_id oid;
230230
int nr_parent; /* < 0 if shallow commit */
231-
unsigned char parent[FLEX_ARRAY][20]; /* more */
231+
struct object_id parent[FLEX_ARRAY]; /* more */
232232
};
233233
typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
234234

log-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
137137

138138
static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
139139
{
140-
struct commit *commit = lookup_commit(graft->sha1);
140+
struct commit *commit = lookup_commit(graft->oid.hash);
141141
if (!commit)
142142
return 0;
143143
add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);

send-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c
182182
{
183183
struct strbuf *sb = cb;
184184
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));
186186
return 0;
187187
}
188188

shallow.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int register_shallow(const unsigned char *sha1)
3131
xmalloc(sizeof(struct commit_graft));
3232
struct commit *commit = lookup_commit(sha1);
3333

34-
hashcpy(graft->sha1, sha1);
34+
hashcpy(graft->oid.hash, sha1);
3535
graft->nr_parent = -1;
3636
if (commit && commit->object.parsed)
3737
commit->parents = NULL;
@@ -159,11 +159,11 @@ struct write_shallow_data {
159159
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
160160
{
161161
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);
163163
if (graft->nr_parent != -1)
164164
return 0;
165165
if (data->flags & SEEN_ONLY) {
166-
struct commit *c = lookup_commit(graft->sha1);
166+
struct commit *c = lookup_commit(graft->oid.hash);
167167
if (!c || !(c->object.flags & SEEN)) {
168168
if (data->flags & VERBOSE)
169169
printf("Removing %s from .git/shallow\n",
@@ -282,7 +282,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c
282282
{
283283
int fd = *(int *)cb;
284284
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));
286286
return 0;
287287
}
288288

upload-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
7474
{
7575
FILE *fp = cb_data;
7676
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));
7878
return 0;
7979
}
8080

0 commit comments

Comments
 (0)