builtin/fmt-merge-message: convert to struct object_id
Convert most of the code to use struct object_id, including struct origin_data and struct merge_parents. Convert several instances of hardcoded numbers into references to GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 21, 2017 at 23:47 UTC
175ccdcf2ae99d9892b97e59d4b75d7502ab42e2
1 file changed
+35
-35
builtin/fmt-merge-msg.c
+35
-35
@@ -41,7 +41,7 @@ struct src_data {
41
};
42
43
struct origin_data {
44
- unsigned char sha1[20];
44
+ struct object_id oid;
45
unsigned is_local_branch:1;
46
};
47
@@ -59,8 +59,8 @@ static struct string_list origins = STRING_LIST_INIT_DUP;
59
struct merge_parents {
60
int alloc, nr;
61
struct merge_parent {
62
- unsigned char given[20];
63
- unsigned char commit[20];
62
+ struct object_id given;
63
+ struct object_id commit;
64
unsigned char used;
65
} *item;
66
};
@@ -70,14 +70,14 @@ struct merge_parents {
70
* hundreds of heads at a time anyway.
71
*/
72
static struct merge_parent *find_merge_parent(struct merge_parents *table,
73
- unsigned char *given,
74
- unsigned char *commit)
73
+ struct object_id *given,
74
+ struct object_id *commit)
75
{
76
int i;
77
for (i = 0; i < table->nr; i++) {
78
- if (given && hashcmp(table->item[i].given, given))
78
+ if (given && oidcmp(&table->item[i].given, given))
79
continue;
80
- if (commit && hashcmp(table->item[i].commit, commit))
80
+ if (commit && oidcmp(&table->item[i].commit, commit))
81
continue;
82
return &table->item[i];
83
}
@@ -85,14 +85,14 @@ static struct merge_parent *find_merge_parent(struct merge_parents *table,
85
}
86
87
static void add_merge_parent(struct merge_parents *table,
88
- unsigned char *given,
89
- unsigned char *commit)
88
+ struct object_id *given,
89
+ struct object_id *commit)
90
{
91
if (table->nr && find_merge_parent(table, given, commit))
92
return;
93
ALLOC_GROW(table->item, table->nr + 1, table->alloc);
94
- hashcpy(table->item[table->nr].given, given);
95
- hashcpy(table->item[table->nr].commit, commit);
94
+ oidcpy(&table->item[table->nr].given, given);
95
+ oidcpy(&table->item[table->nr].commit, commit);
96
table->item[table->nr].used = 0;
97
table->nr++;
98
}
@@ -106,30 +106,30 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
106
struct src_data *src_data;
107
struct string_list_item *item;
108
int pulling_head = 0;
109
- unsigned char sha1[20];
109
+ struct object_id oid;
110
111
- if (len < 43 || line[40] != '\t')
111
+ if (len < GIT_SHA1_HEXSZ + 3 || line[GIT_SHA1_HEXSZ] != '\t')
112
return 1;
113
114
- if (starts_with(line + 41, "not-for-merge"))
114
+ if (starts_with(line + GIT_SHA1_HEXSZ + 1, "not-for-merge"))
115
return 0;
116
117
- if (line[41] != '\t')
117
+ if (line[GIT_SHA1_HEXSZ + 1] != '\t')
118
return 2;
119
120
- i = get_sha1_hex(line, sha1);
120
+ i = get_oid_hex(line, &oid);
121
if (i)
122
return 3;
123
124
- if (!find_merge_parent(merge_parents, sha1, NULL))
124
+ if (!find_merge_parent(merge_parents, &oid, NULL))
125
return 0; /* subsumed by other parents */
126
127
origin_data = xcalloc(1, sizeof(struct origin_data));
128
- hashcpy(origin_data->sha1, sha1);
128
+ oidcpy(&origin_data->oid, &oid);
129
130
if (line[len - 1] == '\n')
131
line[len - 1] = 0;
132
- line += 42;
132
+ line += GIT_SHA1_HEXSZ + 2;
133
134
/*
135
* At this point, line points at the beginning of comment e.g.
@@ -338,10 +338,10 @@ static void shortlog(const char *name,
338
struct string_list committers = STRING_LIST_INIT_DUP;
339
int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
340
struct strbuf sb = STRBUF_INIT;
341
- const unsigned char *sha1 = origin_data->sha1;
341
+ const struct object_id *oid = &origin_data->oid;
342
int limit = opts->shortlog_len;
343
344
- branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
344
+ branch = deref_tag(parse_object(oid->hash), oid_to_hex(oid), GIT_SHA1_HEXSZ);
345
if (!branch || branch->type != OBJ_COMMIT)
346
return;
347
@@ -531,7 +531,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
531
}
532
533
static void find_merge_parents(struct merge_parents *result,
534
- struct strbuf *in, unsigned char *head)
534
+ struct strbuf *in, struct object_id *head)
535
{
536
struct commit_list *parents;
537
struct commit *head_commit;
@@ -542,31 +542,31 @@ static void find_merge_parents(struct merge_parents *result,
542
int len;
543
char *p = in->buf + pos;
544
char *newline = strchr(p, '\n');
545
- unsigned char sha1[20];
545
+ struct object_id oid;
546
struct commit *parent;
547
struct object *obj;
548
549
len = newline ? newline - p : strlen(p);
550
pos += len + !!newline;
551
552
- if (len < 43 ||
553
- get_sha1_hex(p, sha1) ||
554
- p[40] != '\t' ||
555
- p[41] != '\t')
552
+ if (len < GIT_SHA1_HEXSZ + 3 ||
553
+ get_oid_hex(p, &oid) ||
554
+ p[GIT_SHA1_HEXSZ] != '\t' ||
555
+ p[GIT_SHA1_HEXSZ + 1] != '\t')
556
continue; /* skip not-for-merge */
557
/*
558
* Do not use get_merge_parent() here; we do not have
559
* "name" here and we do not want to contaminate its
560
* util field yet.
561
*/
562
- obj = parse_object(sha1);
562
+ obj = parse_object(oid.hash);
563
parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);
564
if (!parent)
565
continue;
566
commit_list_insert(parent, &parents);
567
- add_merge_parent(result, obj->oid.hash, parent->object.oid.hash);
567
+ add_merge_parent(result, &obj->oid, &parent->object.oid);
568
}
569
- head_commit = lookup_commit(head);
569
+ head_commit = lookup_commit(head->hash);
570
if (head_commit)
571
commit_list_insert(head_commit, &parents);
572
parents = reduce_heads(parents);
@@ -574,7 +574,7 @@ static void find_merge_parents(struct merge_parents *result,
574
while (parents) {
575
struct commit *cmit = pop_commit(&parents);
576
for (i = 0; i < result->nr; i++)
577
- if (!hashcmp(result->item[i].commit, cmit->object.oid.hash))
577
+ if (!oidcmp(&result->item[i].commit, &cmit->object.oid))
578
result->item[i].used = 1;
579
}
580
@@ -592,7 +592,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
592
struct fmt_merge_msg_opts *opts)
593
{
594
int i = 0, pos = 0;
595
- unsigned char head_sha1[20];
595
+ struct object_id head_oid;
596
const char *current_branch;
597
void *current_branch_to_free;
598
struct merge_parents merge_parents;
@@ -601,13 +601,13 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
601
602
/* get current branch */
603
current_branch = current_branch_to_free =
604
- resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
604
+ resolve_refdup("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL);
605
if (!current_branch)
606
die("No current branch");
607
if (starts_with(current_branch, "refs/heads/"))
608
current_branch += 11;
609
610
- find_merge_parents(&merge_parents, in, head_sha1);
610
+ find_merge_parents(&merge_parents, in, &head_oid);
611
612
/* get a line */
613
while (pos < in->len) {
@@ -633,7 +633,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
633
struct commit *head;
634
struct rev_info rev;
635
636
- head = lookup_commit_or_die(head_sha1, "HEAD");
636
+ head = lookup_commit_or_die(head_oid.hash, "HEAD");
637
init_revisions(&rev, NULL);
638
rev.commit_format = CMIT_FMT_ONELINE;
639
rev.ignore_merges = 1;