Skip to content

Commit a916cb5

Browse files
committed
Merge branch 'bc/object-id'
Identify parts of the code that knows that we use SHA-1 hash to name our objects too much, and use (1) symbolic constants instead of hardcoded 20 as byte count and/or (2) use struct object_id instead of unsigned char [20] for object names. * bc/object-id: apply: convert threeway_stage to object_id patch-id: convert to use struct object_id commit: convert parts to struct object_id diff: convert struct combine_diff_path to object_id bulk-checkin.c: convert to use struct object_id zip: use GIT_SHA1_HEXSZ for trailers archive.c: convert to use struct object_id bisect.c: convert leaf functions to use struct object_id define utility functions for object IDs define a structure for object IDs
2 parents 3d4a3ff + d07d4ab commit a916cb5

18 files changed

+192
-145
lines changed

archive-zip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,12 @@ static void write_zip_trailer(const unsigned char *sha1)
448448
copy_le16(trailer.entries, zip_dir_entries);
449449
copy_le32(trailer.size, zip_dir_offset);
450450
copy_le32(trailer.offset, zip_offset);
451-
copy_le16(trailer.comment_length, sha1 ? 40 : 0);
451+
copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0);
452452

453453
write_or_die(1, zip_dir, zip_dir_offset);
454454
write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
455455
if (sha1)
456-
write_or_die(1, sha1_to_hex(sha1), 40);
456+
write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ);
457457
}
458458

459459
static void dos_time(time_t *time, int *dos_date, int *dos_time)

archive.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void setup_archive_check(struct git_attr_check *check)
101101

102102
struct directory {
103103
struct directory *up;
104-
unsigned char sha1[20];
104+
struct object_id oid;
105105
int baselen, len;
106106
unsigned mode;
107107
int stage;
@@ -177,7 +177,7 @@ static void queue_directory(const unsigned char *sha1,
177177
d->stage = stage;
178178
c->bottom = d;
179179
d->len = sprintf(d->path, "%.*s%s/", (int)base->len, base->buf, filename);
180-
hashcpy(d->sha1, sha1);
180+
hashcpy(d->oid.hash, sha1);
181181
}
182182

183183
static int write_directory(struct archiver_context *c)
@@ -191,7 +191,7 @@ static int write_directory(struct archiver_context *c)
191191
d->path[d->len - 1] = '\0'; /* no trailing slash */
192192
ret =
193193
write_directory(c) ||
194-
write_archive_entry(d->sha1, d->path, d->baselen,
194+
write_archive_entry(d->oid.hash, d->path, d->baselen,
195195
d->path + d->baselen, d->mode,
196196
d->stage, c) != READ_TREE_RECURSIVE;
197197
free(d);
@@ -354,23 +354,23 @@ static void parse_treeish_arg(const char **argv,
354354
time_t archive_time;
355355
struct tree *tree;
356356
const struct commit *commit;
357-
unsigned char sha1[20];
357+
struct object_id oid;
358358

359359
/* Remotes are only allowed to fetch actual refs */
360360
if (remote && !remote_allow_unreachable) {
361361
char *ref = NULL;
362362
const char *colon = strchrnul(name, ':');
363363
int refnamelen = colon - name;
364364

365-
if (!dwim_ref(name, refnamelen, sha1, &ref))
365+
if (!dwim_ref(name, refnamelen, oid.hash, &ref))
366366
die("no such ref: %.*s", refnamelen, name);
367367
free(ref);
368368
}
369369

370-
if (get_sha1(name, sha1))
370+
if (get_sha1(name, oid.hash))
371371
die("Not a valid object name");
372372

373-
commit = lookup_commit_reference_gently(sha1, 1);
373+
commit = lookup_commit_reference_gently(oid.hash, 1);
374374
if (commit) {
375375
commit_sha1 = commit->object.sha1;
376376
archive_time = commit->date;
@@ -379,21 +379,21 @@ static void parse_treeish_arg(const char **argv,
379379
archive_time = time(NULL);
380380
}
381381

382-
tree = parse_tree_indirect(sha1);
382+
tree = parse_tree_indirect(oid.hash);
383383
if (tree == NULL)
384384
die("not a tree object");
385385

386386
if (prefix) {
387-
unsigned char tree_sha1[20];
387+
struct object_id tree_oid;
388388
unsigned int mode;
389389
int err;
390390

391391
err = get_tree_entry(tree->object.sha1, prefix,
392-
tree_sha1, &mode);
392+
tree_oid.hash, &mode);
393393
if (err || !S_ISDIR(mode))
394394
die("current working directory is untracked");
395395

396-
tree = parse_tree_indirect(tree_sha1);
396+
tree = parse_tree_indirect(tree_oid.hash);
397397
}
398398
ar_args->tree = tree;
399399
ar_args->commit_sha1 = commit_sha1;

bisect.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static struct sha1_array good_revs;
1616
static struct sha1_array skipped_revs;
1717

18-
static unsigned char *current_bad_sha1;
18+
static struct object_id *current_bad_oid;
1919

2020
static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
2121
static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
@@ -404,8 +404,8 @@ static int register_ref(const char *refname, const unsigned char *sha1,
404404
int flags, void *cb_data)
405405
{
406406
if (!strcmp(refname, "bad")) {
407-
current_bad_sha1 = xmalloc(20);
408-
hashcpy(current_bad_sha1, sha1);
407+
current_bad_oid = xmalloc(sizeof(*current_bad_oid));
408+
hashcpy(current_bad_oid->hash, sha1);
409409
} else if (starts_with(refname, "good-")) {
410410
sha1_array_append(&good_revs, sha1);
411411
} else if (starts_with(refname, "skip-")) {
@@ -564,7 +564,7 @@ static struct commit_list *skip_away(struct commit_list *list, int count)
564564

565565
for (i = 0; cur; cur = cur->next, i++) {
566566
if (i == index) {
567-
if (hashcmp(cur->item->object.sha1, current_bad_sha1))
567+
if (hashcmp(cur->item->object.sha1, current_bad_oid->hash))
568568
return cur;
569569
if (previous)
570570
return previous;
@@ -607,7 +607,7 @@ static void bisect_rev_setup(struct rev_info *revs, const char *prefix,
607607

608608
/* rev_argv.argv[0] will be ignored by setup_revisions */
609609
argv_array_push(&rev_argv, "bisect_rev_setup");
610-
argv_array_pushf(&rev_argv, bad_format, sha1_to_hex(current_bad_sha1));
610+
argv_array_pushf(&rev_argv, bad_format, oid_to_hex(current_bad_oid));
611611
for (i = 0; i < good_revs.nr; i++)
612612
argv_array_pushf(&rev_argv, good_format,
613613
sha1_to_hex(good_revs.sha1[i]));
@@ -628,7 +628,7 @@ static void bisect_common(struct rev_info *revs)
628628
}
629629

630630
static void exit_if_skipped_commits(struct commit_list *tried,
631-
const unsigned char *bad)
631+
const struct object_id *bad)
632632
{
633633
if (!tried)
634634
return;
@@ -637,12 +637,12 @@ static void exit_if_skipped_commits(struct commit_list *tried,
637637
"The first bad commit could be any of:\n");
638638
print_commit_list(tried, "%s\n", "%s\n");
639639
if (bad)
640-
printf("%s\n", sha1_to_hex(bad));
640+
printf("%s\n", oid_to_hex(bad));
641641
printf("We cannot bisect more!\n");
642642
exit(2);
643643
}
644644

645-
static int is_expected_rev(const unsigned char *sha1)
645+
static int is_expected_rev(const struct object_id *oid)
646646
{
647647
const char *filename = git_path("BISECT_EXPECTED_REV");
648648
struct stat st;
@@ -658,7 +658,7 @@ static int is_expected_rev(const unsigned char *sha1)
658658
return 0;
659659

660660
if (strbuf_getline(&str, fp, '\n') != EOF)
661-
res = !strcmp(str.buf, sha1_to_hex(sha1));
661+
res = !strcmp(str.buf, oid_to_hex(oid));
662662

663663
strbuf_release(&str);
664664
fclose(fp);
@@ -719,7 +719,7 @@ static struct commit **get_bad_and_good_commits(int *rev_nr)
719719
struct commit **rev = xmalloc(len * sizeof(*rev));
720720
int i, n = 0;
721721

722-
rev[n++] = get_commit_reference(current_bad_sha1);
722+
rev[n++] = get_commit_reference(current_bad_oid->hash);
723723
for (i = 0; i < good_revs.nr; i++)
724724
rev[n++] = get_commit_reference(good_revs.sha1[i]);
725725
*rev_nr = n;
@@ -729,8 +729,8 @@ static struct commit **get_bad_and_good_commits(int *rev_nr)
729729

730730
static void handle_bad_merge_base(void)
731731
{
732-
if (is_expected_rev(current_bad_sha1)) {
733-
char *bad_hex = sha1_to_hex(current_bad_sha1);
732+
if (is_expected_rev(current_bad_oid)) {
733+
char *bad_hex = oid_to_hex(current_bad_oid);
734734
char *good_hex = join_sha1_array_hex(&good_revs, ' ');
735735

736736
fprintf(stderr, "The merge base %s is bad.\n"
@@ -750,7 +750,7 @@ static void handle_bad_merge_base(void)
750750
static void handle_skipped_merge_base(const unsigned char *mb)
751751
{
752752
char *mb_hex = sha1_to_hex(mb);
753-
char *bad_hex = sha1_to_hex(current_bad_sha1);
753+
char *bad_hex = sha1_to_hex(current_bad_oid->hash);
754754
char *good_hex = join_sha1_array_hex(&good_revs, ' ');
755755

756756
warning("the merge base between %s and [%s] "
@@ -781,7 +781,7 @@ static void check_merge_bases(int no_checkout)
781781

782782
for (; result; result = result->next) {
783783
const unsigned char *mb = result->item->object.sha1;
784-
if (!hashcmp(mb, current_bad_sha1)) {
784+
if (!hashcmp(mb, current_bad_oid->hash)) {
785785
handle_bad_merge_base();
786786
} else if (0 <= sha1_array_lookup(&good_revs, mb)) {
787787
continue;
@@ -838,7 +838,7 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
838838
struct stat st;
839839
int fd;
840840

841-
if (!current_bad_sha1)
841+
if (!current_bad_oid)
842842
die("a bad revision is needed");
843843

844844
/* Check if file BISECT_ANCESTORS_OK exists. */
@@ -903,7 +903,7 @@ int bisect_next_all(const char *prefix, int no_checkout)
903903
struct commit_list *tried;
904904
int reaches = 0, all = 0, nr, steps;
905905
const unsigned char *bisect_rev;
906-
char bisect_rev_hex[41];
906+
char bisect_rev_hex[GIT_SHA1_HEXSZ + 1];
907907

908908
if (read_bisect_refs())
909909
die("reading bisect refs failed");
@@ -927,7 +927,7 @@ int bisect_next_all(const char *prefix, int no_checkout)
927927
exit_if_skipped_commits(tried, NULL);
928928

929929
printf("%s was both good and bad\n",
930-
sha1_to_hex(current_bad_sha1));
930+
oid_to_hex(current_bad_oid));
931931
exit(1);
932932
}
933933

@@ -938,10 +938,10 @@ int bisect_next_all(const char *prefix, int no_checkout)
938938
}
939939

940940
bisect_rev = revs.commits->item->object.sha1;
941-
memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
941+
memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
942942

943-
if (!hashcmp(bisect_rev, current_bad_sha1)) {
944-
exit_if_skipped_commits(tried, current_bad_sha1);
943+
if (!hashcmp(bisect_rev, current_bad_oid->hash)) {
944+
exit_if_skipped_commits(tried, current_bad_oid);
945945
printf("%s is the first bad commit\n", bisect_rev_hex);
946946
show_diff_tree(prefix, revs.commits->item);
947947
/* This means the bisection process succeeded. */

builtin/apply.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ struct patch {
208208
struct patch *next;
209209

210210
/* three-way fallback result */
211-
unsigned char threeway_stage[3][20];
211+
struct object_id threeway_stage[3];
212212
};
213213

214214
static void free_fragment_list(struct fragment *list)
@@ -3426,11 +3426,11 @@ static int try_threeway(struct image *image, struct patch *patch,
34263426
if (status) {
34273427
patch->conflicted_threeway = 1;
34283428
if (patch->is_new)
3429-
hashclr(patch->threeway_stage[0]);
3429+
oidclr(&patch->threeway_stage[0]);
34303430
else
3431-
hashcpy(patch->threeway_stage[0], pre_sha1);
3432-
hashcpy(patch->threeway_stage[1], our_sha1);
3433-
hashcpy(patch->threeway_stage[2], post_sha1);
3431+
hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3432+
hashcpy(patch->threeway_stage[1].hash, our_sha1);
3433+
hashcpy(patch->threeway_stage[2].hash, post_sha1);
34343434
fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
34353435
} else {
34363436
fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
@@ -4186,14 +4186,14 @@ static void add_conflicted_stages_file(struct patch *patch)
41864186

41874187
remove_file_from_cache(patch->new_name);
41884188
for (stage = 1; stage < 4; stage++) {
4189-
if (is_null_sha1(patch->threeway_stage[stage - 1]))
4189+
if (is_null_oid(&patch->threeway_stage[stage - 1]))
41904190
continue;
41914191
ce = xcalloc(1, ce_size);
41924192
memcpy(ce->name, patch->new_name, namelen);
41934193
ce->ce_mode = create_ce_mode(mode);
41944194
ce->ce_flags = create_ce_flags(stage);
41954195
ce->ce_namelen = namelen;
4196-
hashcpy(ce->sha1, patch->threeway_stage[stage - 1]);
4196+
hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
41974197
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
41984198
die(_("unable to add cache entry for %s"), patch->new_name);
41994199
}

builtin/patch-id.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "builtin.h"
22

3-
static void flush_current_id(int patchlen, unsigned char *id, unsigned char *result)
3+
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
44
{
55
char name[50];
66

77
if (!patchlen)
88
return;
99

10-
memcpy(name, sha1_to_hex(id), 41);
11-
printf("%s %s\n", sha1_to_hex(result), name);
10+
memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
11+
printf("%s %s\n", oid_to_hex(result), name);
1212
}
1313

1414
static int remove_space(char *line)
@@ -53,31 +53,31 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
5353
return 1;
5454
}
5555

56-
static void flush_one_hunk(unsigned char *result, git_SHA_CTX *ctx)
56+
static void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
5757
{
58-
unsigned char hash[20];
58+
unsigned char hash[GIT_SHA1_RAWSZ];
5959
unsigned short carry = 0;
6060
int i;
6161

6262
git_SHA1_Final(hash, ctx);
6363
git_SHA1_Init(ctx);
6464
/* 20-byte sum, with carry */
65-
for (i = 0; i < 20; ++i) {
66-
carry += result[i] + hash[i];
67-
result[i] = carry;
65+
for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
66+
carry += result->hash[i] + hash[i];
67+
result->hash[i] = carry;
6868
carry >>= 8;
6969
}
7070
}
7171

72-
static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
72+
static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
7373
struct strbuf *line_buf, int stable)
7474
{
7575
int patchlen = 0, found_next = 0;
7676
int before = -1, after = -1;
7777
git_SHA_CTX ctx;
7878

7979
git_SHA1_Init(&ctx);
80-
hashclr(result);
80+
oidclr(result);
8181

8282
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
8383
char *line = line_buf->buf;
@@ -93,7 +93,7 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
9393
else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
9494
continue;
9595

96-
if (!get_sha1_hex(p, next_sha1)) {
96+
if (!get_oid_hex(p, next_oid)) {
9797
found_next = 1;
9898
break;
9999
}
@@ -143,7 +143,7 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
143143
}
144144

145145
if (!found_next)
146-
hashclr(next_sha1);
146+
oidclr(next_oid);
147147

148148
flush_one_hunk(result, &ctx);
149149

@@ -152,15 +152,15 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
152152

153153
static void generate_id_list(int stable)
154154
{
155-
unsigned char sha1[20], n[20], result[20];
155+
struct object_id oid, n, result;
156156
int patchlen;
157157
struct strbuf line_buf = STRBUF_INIT;
158158

159-
hashclr(sha1);
159+
oidclr(&oid);
160160
while (!feof(stdin)) {
161-
patchlen = get_one_patchid(n, result, &line_buf, stable);
162-
flush_current_id(patchlen, sha1, result);
163-
hashcpy(sha1, n);
161+
patchlen = get_one_patchid(&n, &result, &line_buf, stable);
162+
flush_current_id(patchlen, &oid, &result);
163+
oidcpy(&oid, &n);
164164
}
165165
strbuf_release(&line_buf);
166166
}

0 commit comments

Comments
 (0)