builtin/tag: convert to struct object_id

Parts of this module call lookup_commit_reference, which we want to convert. The module is small and mostly self-contained, so convert the rest of it while we're at it. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 6, 2017 at 22:10 UTC 7422ab50d1011b2b26b59bf11b91a1202618f3e5
1 file changed +33 -33
builtin/tag.c
+33 -33
@@ -66,7 +66,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
66 }
67
68 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
69 - const unsigned char *sha1, const void *cb_data);
69 + const struct object_id *oid, const void *cb_data);
70
71 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
72 const void *cb_data)
@@ -74,17 +74,17 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
74 const char **p;
75 struct strbuf ref = STRBUF_INIT;
76 int had_error = 0;
77 - unsigned char sha1[20];
77 + struct object_id oid;
78
79 for (p = argv; *p; p++) {
80 strbuf_reset(&ref);
81 strbuf_addf(&ref, "refs/tags/%s", *p);
82 - if (read_ref(ref.buf, sha1)) {
82 + if (read_ref(ref.buf, oid.hash)) {
83 error(_("tag '%s' not found."), *p);
84 had_error = 1;
85 continue;
86 }
87 - if (fn(*p, ref.buf, sha1, cb_data))
87 + if (fn(*p, ref.buf, &oid, cb_data))
88 had_error = 1;
89 }
90 strbuf_release(&ref);
@@ -92,16 +92,16 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
92 }
93
94 static int delete_tag(const char *name, const char *ref,
95 - const unsigned char *sha1, const void *cb_data)
95 + const struct object_id *oid, const void *cb_data)
96 {
97 - if (delete_ref(NULL, ref, sha1, 0))
97 + if (delete_ref(NULL, ref, oid->hash, 0))
98 return 1;
99 - printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
99 + printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
100 return 0;
101 }
102
103 static int verify_tag(const char *name, const char *ref,
104 - const unsigned char *sha1, const void *cb_data)
104 + const struct object_id *oid, const void *cb_data)
105 {
106 int flags;
107 const char *fmt_pretty = cb_data;
@@ -110,11 +110,11 @@ static int verify_tag(const char *name, const char *ref,
110 if (fmt_pretty)
111 flags = GPG_VERIFY_OMIT_STATUS;
112
113 - if (gpg_verify_tag(sha1, name, flags))
113 + if (gpg_verify_tag(oid->hash, name, flags))
114 return -1;
115
116 if (fmt_pretty)
117 - pretty_print_ref(name, sha1, fmt_pretty);
117 + pretty_print_ref(name, oid->hash, fmt_pretty);
118
119 return 0;
120 }
@@ -182,13 +182,13 @@ static int git_tag_config(const char *var, const char *value, void *cb)
182 return git_default_config(var, value, cb);
183 }
184
185 -static void write_tag_body(int fd, const unsigned char *sha1)
185 +static void write_tag_body(int fd, const struct object_id *oid)
186 {
187 unsigned long size;
188 enum object_type type;
189 char *buf, *sp;
190
191 - buf = read_sha1_file(sha1, &type, &size);
191 + buf = read_sha1_file(oid->hash, &type, &size);
192 if (!buf)
193 return;
194 /* skip header */
@@ -204,11 +204,11 @@ static void write_tag_body(int fd, const unsigned char *sha1)
204 free(buf);
205 }
206
207 -static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
207 +static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
208 {
209 if (sign && do_sign(buf) < 0)
210 return error(_("unable to sign the tag"));
211 - if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
211 + if (write_sha1_file(buf->buf, buf->len, tag_type, result->hash) < 0)
212 return error(_("unable to write tag file"));
213 return 0;
214 }
@@ -223,15 +223,15 @@ struct create_tag_options {
223 } cleanup_mode;
224 };
225
226 -static void create_tag(const unsigned char *object, const char *tag,
226 +static void create_tag(const struct object_id *object, const char *tag,
227 struct strbuf *buf, struct create_tag_options *opt,
228 - unsigned char *prev, unsigned char *result)
228 + struct object_id *prev, struct object_id *result)
229 {
230 enum object_type type;
231 struct strbuf header = STRBUF_INIT;
232 char *path = NULL;
233
234 - type = sha1_object_info(object, NULL);
234 + type = sha1_object_info(object->hash, NULL);
235 if (type <= OBJ_NONE)
236 die(_("bad object type."));
237
@@ -240,7 +240,7 @@ static void create_tag(const unsigned char *object, const char *tag,
240 "type %s\n"
241 "tag %s\n"
242 "tagger %s\n\n",
243 - sha1_to_hex(object),
243 + oid_to_hex(object),
244 typename(type),
245 tag,
246 git_committer_info(IDENT_STRICT));
@@ -254,7 +254,7 @@ static void create_tag(const unsigned char *object, const char *tag,
254 if (fd < 0)
255 die_errno(_("could not create file '%s'"), path);
256
257 - if (!is_null_sha1(prev)) {
257 + if (!is_null_oid(prev)) {
258 write_tag_body(fd, prev);
259 } else {
260 struct strbuf buf = STRBUF_INIT;
@@ -296,7 +296,7 @@ static void create_tag(const unsigned char *object, const char *tag,
296 }
297 }
298
299 -static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb)
299 +static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
300 {
301 enum object_type type;
302 struct commit *c;
@@ -310,17 +310,17 @@ static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb)
310 strbuf_addstr(sb, rla);
311 } else {
312 strbuf_addstr(sb, _("tag: tagging "));
313 - strbuf_add_unique_abbrev(sb, sha1, DEFAULT_ABBREV);
313 + strbuf_add_unique_abbrev(sb, oid->hash, DEFAULT_ABBREV);
314 }
315
316 strbuf_addstr(sb, " (");
317 - type = sha1_object_info(sha1, NULL);
317 + type = sha1_object_info(oid->hash, NULL);
318 switch (type) {
319 default:
320 strbuf_addstr(sb, _("object of unknown type"));
321 break;
322 case OBJ_COMMIT:
323 - if ((buf = read_sha1_file(sha1, &type, &size)) != NULL) {
323 + if ((buf = read_sha1_file(oid->hash, &type, &size)) != NULL) {
324 subject_len = find_commit_subject(buf, &subject_start);
325 strbuf_insert(sb, sb->len, subject_start, subject_len);
326 } else {
@@ -328,7 +328,7 @@ static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb)
328 }
329 free(buf);
330
331 - if ((c = lookup_commit_reference(sha1)) != NULL)
331 + if ((c = lookup_commit_reference(oid->hash)) != NULL)
332 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
333 break;
334 case OBJ_TREE:
@@ -378,7 +378,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
378 struct strbuf buf = STRBUF_INIT;
379 struct strbuf ref = STRBUF_INIT;
380 struct strbuf reflog_msg = STRBUF_INIT;
381 - unsigned char object[20], prev[20];
381 + struct object_id object, prev;
382 const char *object_ref, *tag;
383 struct create_tag_options opt;
384 char *cleanup_arg = NULL;
@@ -528,14 +528,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
528 if (argc > 2)
529 die(_("too many params"));
530
531 - if (get_sha1(object_ref, object))
531 + if (get_oid(object_ref, &object))
532 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
533
534 if (strbuf_check_tag_ref(&ref, tag))
535 die(_("'%s' is not a valid tag name."), tag);
536
537 - if (read_ref(ref.buf, prev))
538 - hashclr(prev);
537 + if (read_ref(ref.buf, prev.hash))
538 + oidclr(&prev);
539 else if (!force)
540 die(_("tag '%s' already exists"), tag);
541
@@ -550,24 +550,24 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
550 else
551 die(_("Invalid cleanup mode %s"), cleanup_arg);
552
553 - create_reflog_msg(object, &reflog_msg);
553 + create_reflog_msg(&object, &reflog_msg);
554
555 if (create_tag_object) {
556 if (force_sign_annotate && !annotate)
557 opt.sign = 1;
558 - create_tag(object, tag, &buf, &opt, prev, object);
558 + create_tag(&object, tag, &buf, &opt, &prev, &object);
559 }
560
561 transaction = ref_transaction_begin(&err);
562 if (!transaction ||
563 - ref_transaction_update(transaction, ref.buf, object, prev,
563 + ref_transaction_update(transaction, ref.buf, object.hash, prev.hash,
564 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
565 reflog_msg.buf, &err) ||
566 ref_transaction_commit(transaction, &err))
567 die("%s", err.buf);
568 ref_transaction_free(transaction);
569 - if (force && !is_null_sha1(prev) && hashcmp(prev, object))
570 - printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
569 + if (force && !is_null_oid(&prev) && oidcmp(&prev, &object))
570 + printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev.hash, DEFAULT_ABBREV));
571
572 strbuf_release(&err);
573 strbuf_release(&buf);