builtin/name-rev: convert to struct object_id

Convert all the uses of unsigned char [20] to struct object_id. Also, convert some hard-coded integers into constants. name_rev_line accepts a wide variety of free-form input and only interprets 40-character hex values, passing through everything else. Consequently, it is not a good candidate for parse_oid_hex, which is much stricter. This change is a prerequisite for converting parse_object. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 1, 2017 at 02:28 UTC 511dca80cce868237b2b64f909ed83863fcf5455
1 file changed +14 -14
builtin/name-rev.c
+14 -14
@@ -114,7 +114,7 @@ struct name_ref_data {
114
115 static struct tip_table {
116 struct tip_table_entry {
117 - unsigned char sha1[20];
117 + struct object_id oid;
118 const char *refname;
119 } *table;
120 int nr;
@@ -122,13 +122,13 @@ static struct tip_table {
122 int sorted;
123 } tip_table;
124
125 -static void add_to_tip_table(const unsigned char *sha1, const char *refname,
125 +static void add_to_tip_table(const struct object_id *oid, const char *refname,
126 int shorten_unambiguous)
127 {
128 refname = name_ref_abbrev(refname, shorten_unambiguous);
129
130 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
131 - hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
131 + oidcpy(&tip_table.table[tip_table.nr].oid, oid);
132 tip_table.table[tip_table.nr].refname = xstrdup(refname);
133 tip_table.nr++;
134 tip_table.sorted = 0;
@@ -137,7 +137,7 @@ static void add_to_tip_table(const unsigned char *sha1, const char *refname,
137 static int tipcmp(const void *a_, const void *b_)
138 {
139 const struct tip_table_entry *a = a_, *b = b_;
140 - return hashcmp(a->sha1, b->sha1);
140 + return oidcmp(&a->oid, &b->oid);
141 }
142
143 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
@@ -194,7 +194,7 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
194 return 0;
195 }
196
197 - add_to_tip_table(oid->hash, path, can_abbreviate_output);
197 + add_to_tip_table(oid, path, can_abbreviate_output);
198
199 while (o && o->type == OBJ_TAG) {
200 struct tag *t = (struct tag *) o;
@@ -216,7 +216,7 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
216 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
217 {
218 struct tip_table_entry *table = table_;
219 - return table[ix].sha1;
219 + return table[ix].oid.hash;
220 }
221
222 static const char *get_exact_ref_match(const struct object *o)
@@ -301,9 +301,9 @@ static void name_rev_line(char *p, struct name_ref_data *data)
301 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
302 if (!ishex(*p))
303 forty = 0;
304 - else if (++forty == 40 &&
304 + else if (++forty == GIT_SHA1_HEXSZ &&
305 !ishex(*(p+1))) {
306 - unsigned char sha1[40];
306 + struct object_id oid;
307 const char *name = NULL;
308 char c = *(p+1);
309 int p_len = p - p_start + 1;
@@ -311,9 +311,9 @@ static void name_rev_line(char *p, struct name_ref_data *data)
311 forty = 0;
312
313 *(p+1) = 0;
314 - if (!get_sha1(p - 39, sha1)) {
314 + if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
315 struct object *o =
316 - lookup_object(sha1);
316 + lookup_object(oid.hash);
317 if (o)
318 name = get_rev_name(o, &buf);
319 }
@@ -323,7 +323,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
323 continue;
324
325 if (data->name_only)
326 - printf("%.*s%s", p_len - 40, p_start, name);
326 + printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
327 else
328 printf("%.*s (%s)", p_len, p_start, name);
329 p_start = p + 1;
@@ -374,18 +374,18 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
374 cutoff = 0;
375
376 for (; argc; argc--, argv++) {
377 - unsigned char sha1[20];
377 + struct object_id oid;
378 struct object *object;
379 struct commit *commit;
380
381 - if (get_sha1(*argv, sha1)) {
381 + if (get_oid(*argv, &oid)) {
382 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
383 *argv);
384 continue;
385 }
386
387 commit = NULL;
388 - object = parse_object(sha1);
388 + object = parse_object(oid.hash);
389 if (object) {
390 struct object *peeled = deref_tag(object, *argv, 0);
391 if (peeled && peeled->type == OBJ_COMMIT)