builtin/notes: convert to struct object_id

Convert most of the static functions to use struct object_id. In addition, convert copy_notes_for_rewrite and its callers. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 30, 2017 at 10:30 UTC bb7e4739712e3f9eee0dfd60088b6d8983067960
5 files changed +60 -60
builtin/am.c
+1 -1
@@ -563,7 +563,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
563 goto finish;
564 }
565
566 - if (copy_note_for_rewrite(c, from_obj.hash, to_obj.hash))
566 + if (copy_note_for_rewrite(c, &from_obj, &to_obj))
567 ret = error(_("Failed to copy notes from '%s' to '%s'"),
568 oid_to_hex(&from_obj), oid_to_hex(&to_obj));
569 }
builtin/commit.c
+1 -1
@@ -1809,7 +1809,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1809 cfg = init_copy_notes_for_rewrite("amend");
1810 if (cfg) {
1811 /* we are amending, so current_head is not NULL */
1812 - copy_note_for_rewrite(cfg, current_head->object.oid.hash, oid.hash);
1812 + copy_note_for_rewrite(cfg, &current_head->object.oid, &oid);
1813 finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
1814 }
1815 run_rewrite_hook(&current_head->object.oid, &oid);
builtin/notes.c
+55 -55
@@ -129,10 +129,10 @@ static void copy_obj_to_fd(int fd, const unsigned char *sha1)
129 }
130 }
131
132 -static void write_commented_object(int fd, const unsigned char *object)
132 +static void write_commented_object(int fd, const struct object_id *object)
133 {
134 const char *show_args[5] =
135 - {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
135 + {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
136 struct child_process show = CHILD_PROCESS_INIT;
137 struct strbuf buf = STRBUF_INIT;
138 struct strbuf cbuf = STRBUF_INIT;
@@ -145,7 +145,7 @@ static void write_commented_object(int fd, const unsigned char *object)
145 show.git_cmd = 1;
146 if (start_command(&show))
147 die(_("unable to start 'show' for object '%s'"),
148 - sha1_to_hex(object));
148 + oid_to_hex(object));
149
150 if (strbuf_read(&buf, show.out, 0) < 0)
151 die_errno(_("could not read 'show' output"));
@@ -157,10 +157,10 @@ static void write_commented_object(int fd, const unsigned char *object)
157
158 if (finish_command(&show))
159 die(_("failed to finish 'show' for object '%s'"),
160 - sha1_to_hex(object));
160 + oid_to_hex(object));
161 }
162
163 -static void prepare_note_data(const unsigned char *object, struct note_data *d,
163 +static void prepare_note_data(const struct object_id *object, struct note_data *d,
164 const unsigned char *old_note)
165 {
166 if (d->use_editor || !d->given) {
@@ -243,16 +243,16 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
243 {
244 struct note_data *d = opt->value;
245 char *buf;
246 - unsigned char object[20];
246 + struct object_id object;
247 enum object_type type;
248 unsigned long len;
249
250 if (d->buf.len)
251 strbuf_addch(&d->buf, '\n');
252
253 - if (get_sha1(arg, object))
253 + if (get_oid(arg, &object))
254 die(_("failed to resolve '%s' as a valid ref."), arg);
255 - if (!(buf = read_sha1_file(object, &type, &len))) {
255 + if (!(buf = read_sha1_file(object.hash, &type, &len))) {
256 free(buf);
257 die(_("failed to read object '%s'."), arg);
258 }
@@ -292,7 +292,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
292 }
293
294 while (strbuf_getline_lf(&buf, stdin) != EOF) {
295 - unsigned char from_obj[20], to_obj[20];
295 + struct object_id from_obj, to_obj;
296 struct strbuf **split;
297 int err;
298
@@ -301,15 +301,15 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
301 die(_("malformed input line: '%s'."), buf.buf);
302 strbuf_rtrim(split[0]);
303 strbuf_rtrim(split[1]);
304 - if (get_sha1(split[0]->buf, from_obj))
304 + if (get_oid(split[0]->buf, &from_obj))
305 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
306 - if (get_sha1(split[1]->buf, to_obj))
306 + if (get_oid(split[1]->buf, &to_obj))
307 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
308
309 if (rewrite_cmd)
310 - err = copy_note_for_rewrite(c, from_obj, to_obj);
310 + err = copy_note_for_rewrite(c, &from_obj, &to_obj);
311 else
312 - err = copy_note(t, from_obj, to_obj, force,
312 + err = copy_note(t, from_obj.hash, to_obj.hash, force,
313 combine_notes_overwrite);
314
315 if (err) {
@@ -350,7 +350,7 @@ static struct notes_tree *init_notes_check(const char *subcommand,
350 static int list(int argc, const char **argv, const char *prefix)
351 {
352 struct notes_tree *t;
353 - unsigned char object[20];
353 + struct object_id object;
354 const struct object_id *note;
355 int retval = -1;
356 struct option options[] = {
@@ -368,15 +368,15 @@ static int list(int argc, const char **argv, const char *prefix)
368
369 t = init_notes_check("list", 0);
370 if (argc) {
371 - if (get_sha1(argv[0], object))
371 + if (get_oid(argv[0], &object))
372 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
373 - note = get_note(t, object);
373 + note = get_note(t, object.hash);
374 if (note) {
375 puts(oid_to_hex(note));
376 retval = 0;
377 } else
378 retval = error(_("no note found for object %s."),
379 - sha1_to_hex(object));
379 + oid_to_hex(&object));
380 } else
381 retval = for_each_note(t, 0, list_each_note, NULL);
382
@@ -391,7 +391,7 @@ static int add(int argc, const char **argv, const char *prefix)
391 int force = 0, allow_empty = 0;
392 const char *object_ref;
393 struct notes_tree *t;
394 - unsigned char object[20], new_note[20];
394 + struct object_id object, new_note;
395 const struct object_id *note;
396 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
397 struct option options[] = {
@@ -423,11 +423,11 @@ static int add(int argc, const char **argv, const char *prefix)
423
424 object_ref = argc > 1 ? argv[1] : "HEAD";
425
426 - if (get_sha1(object_ref, object))
426 + if (get_oid(object_ref, &object))
427 die(_("failed to resolve '%s' as a valid ref."), object_ref);
428
429 t = init_notes_check("add", NOTES_INIT_WRITABLE);
430 - note = get_note(t, object);
430 + note = get_note(t, object.hash);
431
432 if (note) {
433 if (!force) {
@@ -437,7 +437,7 @@ static int add(int argc, const char **argv, const char *prefix)
437 return error(_("Cannot add notes. "
438 "Found existing notes for object %s. "
439 "Use '-f' to overwrite existing notes"),
440 - sha1_to_hex(object));
440 + oid_to_hex(&object));
441 }
442 /*
443 * Redirect to "edit" subcommand.
@@ -450,19 +450,19 @@ static int add(int argc, const char **argv, const char *prefix)
450 return append_edit(argc, argv, prefix);
451 }
452 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
453 - sha1_to_hex(object));
453 + oid_to_hex(&object));
454 }
455
456 - prepare_note_data(object, &d, note->hash);
456 + prepare_note_data(&object, &d, note->hash);
457 if (d.buf.len || allow_empty) {
458 - write_note_data(&d, new_note);
459 - if (add_note(t, object, new_note, combine_notes_overwrite))
458 + write_note_data(&d, new_note.hash);
459 + if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite))
460 die("BUG: combine_notes_overwrite failed");
461 commit_notes(t, "Notes added by 'git notes add'");
462 } else {
463 fprintf(stderr, _("Removing note for object %s\n"),
464 - sha1_to_hex(object));
465 - remove_note(t, object);
464 + oid_to_hex(&object));
465 + remove_note(t, object.hash);
466 commit_notes(t, "Notes removed by 'git notes add'");
467 }
468
@@ -476,7 +476,7 @@ static int copy(int argc, const char **argv, const char *prefix)
476 int retval = 0, force = 0, from_stdin = 0;
477 const struct object_id *from_note, *note;
478 const char *object_ref;
479 - unsigned char object[20], from_obj[20];
479 + struct object_id object, from_obj;
480 struct notes_tree *t;
481 const char *rewrite_cmd = NULL;
482 struct option options[] = {
@@ -509,37 +509,37 @@ static int copy(int argc, const char **argv, const char *prefix)
509 usage_with_options(git_notes_copy_usage, options);
510 }
511
512 - if (get_sha1(argv[0], from_obj))
512 + if (get_oid(argv[0], &from_obj))
513 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
514
515 object_ref = 1 < argc ? argv[1] : "HEAD";
516
517 - if (get_sha1(object_ref, object))
517 + if (get_oid(object_ref, &object))
518 die(_("failed to resolve '%s' as a valid ref."), object_ref);
519
520 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
521 - note = get_note(t, object);
521 + note = get_note(t, object.hash);
522
523 if (note) {
524 if (!force) {
525 retval = error(_("Cannot copy notes. Found existing "
526 "notes for object %s. Use '-f' to "
527 "overwrite existing notes"),
528 - sha1_to_hex(object));
528 + oid_to_hex(&object));
529 goto out;
530 }
531 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
532 - sha1_to_hex(object));
532 + oid_to_hex(&object));
533 }
534
535 - from_note = get_note(t, from_obj);
535 + from_note = get_note(t, from_obj.hash);
536 if (!from_note) {
537 retval = error(_("missing notes on source object %s. Cannot "
538 - "copy."), sha1_to_hex(from_obj));
538 + "copy."), oid_to_hex(&from_obj));
539 goto out;
540 }
541
542 - if (add_note(t, object, from_note->hash, combine_notes_overwrite))
542 + if (add_note(t, object.hash, from_note->hash, combine_notes_overwrite))
543 die("BUG: combine_notes_overwrite failed");
544 commit_notes(t, "Notes added by 'git notes copy'");
545 out:
@@ -552,7 +552,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
552 int allow_empty = 0;
553 const char *object_ref;
554 struct notes_tree *t;
555 - unsigned char object[20], new_note[20];
555 + struct object_id object, new_note;
556 const struct object_id *note;
557 char *logmsg;
558 const char * const *usage;
@@ -592,13 +592,13 @@ static int append_edit(int argc, const char **argv, const char *prefix)
592
593 object_ref = 1 < argc ? argv[1] : "HEAD";
594
595 - if (get_sha1(object_ref, object))
595 + if (get_oid(object_ref, &object))
596 die(_("failed to resolve '%s' as a valid ref."), object_ref);
597
598 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
599 - note = get_note(t, object);
599 + note = get_note(t, object.hash);
600
601 - prepare_note_data(object, &d, edit && note ? note->hash : NULL);
601 + prepare_note_data(&object, &d, edit && note ? note->hash : NULL);
602
603 if (note && !edit) {
604 /* Append buf to previous note contents */
@@ -615,14 +615,14 @@ static int append_edit(int argc, const char **argv, const char *prefix)
615 }
616
617 if (d.buf.len || allow_empty) {
618 - write_note_data(&d, new_note);
619 - if (add_note(t, object, new_note, combine_notes_overwrite))
618 + write_note_data(&d, new_note.hash);
619 + if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite))
620 die("BUG: combine_notes_overwrite failed");
621 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
622 } else {
623 fprintf(stderr, _("Removing note for object %s\n"),
624 - sha1_to_hex(object));
625 - remove_note(t, object);
624 + oid_to_hex(&object));
625 + remove_note(t, object.hash);
626 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
627 }
628 commit_notes(t, logmsg);
@@ -637,7 +637,7 @@ static int show(int argc, const char **argv, const char *prefix)
637 {
638 const char *object_ref;
639 struct notes_tree *t;
640 - unsigned char object[20];
640 + struct object_id object;
641 const struct object_id *note;
642 int retval;
643 struct option options[] = {
@@ -654,15 +654,15 @@ static int show(int argc, const char **argv, const char *prefix)
654
655 object_ref = argc ? argv[0] : "HEAD";
656
657 - if (get_sha1(object_ref, object))
657 + if (get_oid(object_ref, &object))
658 die(_("failed to resolve '%s' as a valid ref."), object_ref);
659
660 t = init_notes_check("show", 0);
661 - note = get_note(t, object);
661 + note = get_note(t, object.hash);
662
663 if (!note)
664 retval = error(_("no note found for object %s."),
665 - sha1_to_hex(object));
665 + oid_to_hex(&object));
666 else {
667 const char *show_args[3] = {"show", oid_to_hex(note), NULL};
668 retval = execv_git_cmd(show_args);
@@ -760,7 +760,7 @@ static int git_config_get_notes_strategy(const char *key,
760 static int merge(int argc, const char **argv, const char *prefix)
761 {
762 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
763 - unsigned char result_sha1[20];
763 + struct object_id result_oid;
764 struct notes_tree *t;
765 struct notes_merge_options o;
766 int do_merge = 0, do_commit = 0, do_abort = 0;
@@ -842,16 +842,16 @@ static int merge(int argc, const char **argv, const char *prefix)
842 remote_ref.buf, default_notes_ref());
843 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
844
845 - result = notes_merge(&o, t, result_sha1);
845 + result = notes_merge(&o, t, result_oid.hash);
846
847 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
848 /* Update default notes ref with new commit */
849 - update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
849 + update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL,
850 0, UPDATE_REFS_DIE_ON_ERR);
851 else { /* Merge has unresolved conflicts */
852 const struct worktree *wt;
853 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
854 - update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
854 + update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL,
855 0, UPDATE_REFS_DIE_ON_ERR);
856 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
857 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
@@ -878,10 +878,10 @@ static int merge(int argc, const char **argv, const char *prefix)
878 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
879 {
880 int status;
881 - unsigned char sha1[20];
882 - if (get_sha1(name, sha1))
881 + struct object_id oid;
882 + if (get_oid(name, &oid))
883 return error(_("Failed to resolve '%s' as a valid ref."), name);
884 - status = remove_note(t, sha1);
884 + status = remove_note(t, oid.hash);
885 if (status)
886 fprintf(stderr, _("Object %s has no note\n"), name);
887 else
notes-utils.c
+2 -2
@@ -155,12 +155,12 @@ struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
155 }
156
157 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
158 - const unsigned char *from_obj, const unsigned char *to_obj)
158 + const struct object_id *from_obj, const struct object_id *to_obj)
159 {
160 int ret = 0;
161 int i;
162 for (i = 0; c->trees[i]; i++)
163 - ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
163 + ret = copy_note(c->trees[i], from_obj->hash, to_obj->hash, 1, c->combine) || ret;
164 return ret;
165 }
166
notes-utils.h
+1 -1
@@ -40,7 +40,7 @@ struct notes_rewrite_cfg {
40 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s);
41 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
42 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
43 - const unsigned char *from_obj, const unsigned char *to_obj);
43 + const struct object_id *from_obj, const struct object_id *to_obj);
44 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg);
45
46 #endif