diff options
Diffstat (limited to 'builtin/notes.c')
-rw-r--r-- | builtin/notes.c | 139 |
1 files changed, 70 insertions, 69 deletions
diff --git a/builtin/notes.c b/builtin/notes.c index fb856e53b6..4303848e04 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -8,6 +8,7 @@ */ #include "cache.h" +#include "config.h" #include "builtin.h" #include "notes.h" #include "blob.h" @@ -109,11 +110,11 @@ static void free_note_data(struct note_data *d) strbuf_release(&d->buf); } -static int list_each_note(const unsigned char *object_sha1, - const unsigned char *note_sha1, char *note_path, +static int list_each_note(const struct object_id *object_oid, + const struct object_id *note_oid, char *note_path, void *cb_data) { - printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1)); + printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid)); return 0; } @@ -129,10 +130,10 @@ static void copy_obj_to_fd(int fd, const unsigned char *sha1) } } -static void write_commented_object(int fd, const unsigned char *object) +static void write_commented_object(int fd, const struct object_id *object) { const char *show_args[5] = - {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL}; + {"show", "--stat", "--no-notes", oid_to_hex(object), NULL}; struct child_process show = CHILD_PROCESS_INIT; struct strbuf buf = STRBUF_INIT; struct strbuf cbuf = STRBUF_INIT; @@ -145,7 +146,7 @@ static void write_commented_object(int fd, const unsigned char *object) show.git_cmd = 1; if (start_command(&show)) die(_("unable to start 'show' for object '%s'"), - sha1_to_hex(object)); + oid_to_hex(object)); if (strbuf_read(&buf, show.out, 0) < 0) die_errno(_("could not read 'show' output")); @@ -157,10 +158,10 @@ static void write_commented_object(int fd, const unsigned char *object) if (finish_command(&show)) die(_("failed to finish 'show' for object '%s'"), - sha1_to_hex(object)); + oid_to_hex(object)); } -static void prepare_note_data(const unsigned char *object, struct note_data *d, +static void prepare_note_data(const struct object_id *object, struct note_data *d, const unsigned char *old_note) { if (d->use_editor || !d->given) { @@ -243,16 +244,16 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) { struct note_data *d = opt->value; char *buf; - unsigned char object[20]; + struct object_id object; enum object_type type; unsigned long len; if (d->buf.len) strbuf_addch(&d->buf, '\n'); - if (get_sha1(arg, object)) + if (get_oid(arg, &object)) die(_("failed to resolve '%s' as a valid ref."), arg); - if (!(buf = read_sha1_file(object, &type, &len))) { + if (!(buf = read_sha1_file(object.hash, &type, &len))) { free(buf); die(_("failed to read object '%s'."), arg); } @@ -292,7 +293,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd) } while (strbuf_getline_lf(&buf, stdin) != EOF) { - unsigned char from_obj[20], to_obj[20]; + struct object_id from_obj, to_obj; struct strbuf **split; int err; @@ -301,15 +302,15 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd) die(_("malformed input line: '%s'."), buf.buf); strbuf_rtrim(split[0]); strbuf_rtrim(split[1]); - if (get_sha1(split[0]->buf, from_obj)) + if (get_oid(split[0]->buf, &from_obj)) die(_("failed to resolve '%s' as a valid ref."), split[0]->buf); - if (get_sha1(split[1]->buf, to_obj)) + if (get_oid(split[1]->buf, &to_obj)) die(_("failed to resolve '%s' as a valid ref."), split[1]->buf); if (rewrite_cmd) - err = copy_note_for_rewrite(c, from_obj, to_obj); + err = copy_note_for_rewrite(c, &from_obj, &to_obj); else - err = copy_note(t, from_obj, to_obj, force, + err = copy_note(t, &from_obj, &to_obj, force, combine_notes_overwrite); if (err) { @@ -352,8 +353,8 @@ static struct notes_tree *init_notes_check(const char *subcommand, static int list(int argc, const char **argv, const char *prefix) { struct notes_tree *t; - unsigned char object[20]; - const unsigned char *note; + struct object_id object; + const struct object_id *note; int retval = -1; struct option options[] = { OPT_END() @@ -370,15 +371,15 @@ static int list(int argc, const char **argv, const char *prefix) t = init_notes_check("list", 0); if (argc) { - if (get_sha1(argv[0], object)) + if (get_oid(argv[0], &object)) die(_("failed to resolve '%s' as a valid ref."), argv[0]); - note = get_note(t, object); + note = get_note(t, &object); if (note) { - puts(sha1_to_hex(note)); + puts(oid_to_hex(note)); retval = 0; } else retval = error(_("no note found for object %s."), - sha1_to_hex(object)); + oid_to_hex(&object)); } else retval = for_each_note(t, 0, list_each_note, NULL); @@ -393,8 +394,8 @@ static int add(int argc, const char **argv, const char *prefix) int force = 0, allow_empty = 0; const char *object_ref; struct notes_tree *t; - unsigned char object[20], new_note[20]; - const unsigned char *note; + struct object_id object, new_note; + const struct object_id *note; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), @@ -425,11 +426,11 @@ static int add(int argc, const char **argv, const char *prefix) object_ref = argc > 1 ? argv[1] : "HEAD"; - if (get_sha1(object_ref, object)) + if (get_oid(object_ref, &object)) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("add", NOTES_INIT_WRITABLE); - note = get_note(t, object); + note = get_note(t, &object); if (note) { if (!force) { @@ -439,7 +440,7 @@ static int add(int argc, const char **argv, const char *prefix) return error(_("Cannot add notes. " "Found existing notes for object %s. " "Use '-f' to overwrite existing notes"), - sha1_to_hex(object)); + oid_to_hex(&object)); } /* * Redirect to "edit" subcommand. @@ -452,19 +453,19 @@ static int add(int argc, const char **argv, const char *prefix) return append_edit(argc, argv, prefix); } fprintf(stderr, _("Overwriting existing notes for object %s\n"), - sha1_to_hex(object)); + oid_to_hex(&object)); } - prepare_note_data(object, &d, note); + prepare_note_data(&object, &d, note ? note->hash : NULL); if (d.buf.len || allow_empty) { - write_note_data(&d, new_note); - if (add_note(t, object, new_note, combine_notes_overwrite)) + write_note_data(&d, new_note.hash); + if (add_note(t, &object, &new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes add'"); } else { fprintf(stderr, _("Removing note for object %s\n"), - sha1_to_hex(object)); - remove_note(t, object); + oid_to_hex(&object)); + remove_note(t, object.hash); commit_notes(t, "Notes removed by 'git notes add'"); } @@ -476,9 +477,9 @@ static int add(int argc, const char **argv, const char *prefix) static int copy(int argc, const char **argv, const char *prefix) { int retval = 0, force = 0, from_stdin = 0; - const unsigned char *from_note, *note; + const struct object_id *from_note, *note; const char *object_ref; - unsigned char object[20], from_obj[20]; + struct object_id object, from_obj; struct notes_tree *t; const char *rewrite_cmd = NULL; struct option options[] = { @@ -511,37 +512,37 @@ static int copy(int argc, const char **argv, const char *prefix) usage_with_options(git_notes_copy_usage, options); } - if (get_sha1(argv[0], from_obj)) + if (get_oid(argv[0], &from_obj)) die(_("failed to resolve '%s' as a valid ref."), argv[0]); object_ref = 1 < argc ? argv[1] : "HEAD"; - if (get_sha1(object_ref, object)) + if (get_oid(object_ref, &object)) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("copy", NOTES_INIT_WRITABLE); - note = get_note(t, object); + note = get_note(t, &object); if (note) { if (!force) { retval = error(_("Cannot copy notes. Found existing " "notes for object %s. Use '-f' to " "overwrite existing notes"), - sha1_to_hex(object)); + oid_to_hex(&object)); goto out; } fprintf(stderr, _("Overwriting existing notes for object %s\n"), - sha1_to_hex(object)); + oid_to_hex(&object)); } - from_note = get_note(t, from_obj); + from_note = get_note(t, &from_obj); if (!from_note) { retval = error(_("missing notes on source object %s. Cannot " - "copy."), sha1_to_hex(from_obj)); + "copy."), oid_to_hex(&from_obj)); goto out; } - if (add_note(t, object, from_note, combine_notes_overwrite)) + if (add_note(t, &object, from_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes copy'"); out: @@ -554,8 +555,8 @@ static int append_edit(int argc, const char **argv, const char *prefix) int allow_empty = 0; const char *object_ref; struct notes_tree *t; - unsigned char object[20], new_note[20]; - const unsigned char *note; + struct object_id object, new_note; + const struct object_id *note; char *logmsg; const char * const *usage; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; @@ -594,19 +595,19 @@ static int append_edit(int argc, const char **argv, const char *prefix) object_ref = 1 < argc ? argv[1] : "HEAD"; - if (get_sha1(object_ref, object)) + if (get_oid(object_ref, &object)) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); - note = get_note(t, object); + note = get_note(t, &object); - prepare_note_data(object, &d, edit ? note : NULL); + prepare_note_data(&object, &d, edit && note ? note->hash : NULL); if (note && !edit) { /* Append buf to previous note contents */ unsigned long size; enum object_type type; - char *prev_buf = read_sha1_file(note, &type, &size); + char *prev_buf = read_sha1_file(note->hash, &type, &size); strbuf_grow(&d.buf, size + 1); if (d.buf.len && prev_buf && size) @@ -617,14 +618,14 @@ static int append_edit(int argc, const char **argv, const char *prefix) } if (d.buf.len || allow_empty) { - write_note_data(&d, new_note); - if (add_note(t, object, new_note, combine_notes_overwrite)) + write_note_data(&d, new_note.hash); + if (add_note(t, &object, &new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); } else { fprintf(stderr, _("Removing note for object %s\n"), - sha1_to_hex(object)); - remove_note(t, object); + oid_to_hex(&object)); + remove_note(t, object.hash); logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); } commit_notes(t, logmsg); @@ -639,8 +640,8 @@ static int show(int argc, const char **argv, const char *prefix) { const char *object_ref; struct notes_tree *t; - unsigned char object[20]; - const unsigned char *note; + struct object_id object; + const struct object_id *note; int retval; struct option options[] = { OPT_END() @@ -656,17 +657,17 @@ static int show(int argc, const char **argv, const char *prefix) object_ref = argc ? argv[0] : "HEAD"; - if (get_sha1(object_ref, object)) + if (get_oid(object_ref, &object)) die(_("failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("show", 0); - note = get_note(t, object); + note = get_note(t, &object); if (!note) retval = error(_("no note found for object %s."), - sha1_to_hex(object)); + oid_to_hex(&object)); else { - const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; + const char *show_args[3] = {"show", oid_to_hex(note), NULL}; retval = execv_git_cmd(show_args); } free_notes(t); @@ -708,7 +709,7 @@ static int merge_commit(struct notes_merge_options *o) if (get_oid("NOTES_MERGE_PARTIAL", &oid)) die(_("failed to read ref NOTES_MERGE_PARTIAL")); - else if (!(partial = lookup_commit_reference(oid.hash))) + else if (!(partial = lookup_commit_reference(&oid))) die(_("could not find commit from NOTES_MERGE_PARTIAL.")); else if (parse_commit(partial)) die(_("could not parse commit from NOTES_MERGE_PARTIAL.")); @@ -726,7 +727,7 @@ static int merge_commit(struct notes_merge_options *o) if (!o->local_ref) die(_("failed to resolve NOTES_MERGE_REF")); - if (notes_merge_commit(o, t, partial, oid.hash)) + if (notes_merge_commit(o, t, partial, &oid)) die(_("failed to finalize notes merge")); /* Reuse existing commit message in reflog message */ @@ -762,7 +763,7 @@ static int git_config_get_notes_strategy(const char *key, static int merge(int argc, const char **argv, const char *prefix) { struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; - unsigned char result_sha1[20]; + struct object_id result_oid; struct notes_tree *t; struct notes_merge_options o; int do_merge = 0, do_commit = 0, do_abort = 0; @@ -844,16 +845,16 @@ static int merge(int argc, const char **argv, const char *prefix) remote_ref.buf, default_notes_ref()); strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ - result = notes_merge(&o, t, result_sha1); + result = notes_merge(&o, t, &result_oid); - if (result >= 0) /* Merge resulted (trivially) in result_sha1 */ + if (result >= 0) /* Merge resulted (trivially) in result_oid */ /* Update default notes ref with new commit */ - update_ref(msg.buf, default_notes_ref(), result_sha1, NULL, + update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR); else { /* Merge has unresolved conflicts */ const struct worktree *wt; /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ - update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL, + update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR); /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref()); @@ -880,10 +881,10 @@ static int merge(int argc, const char **argv, const char *prefix) static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) { int status; - unsigned char sha1[20]; - if (get_sha1(name, sha1)) + struct object_id oid; + if (get_oid(name, &oid)) return error(_("Failed to resolve '%s' as a valid ref."), name); - status = remove_note(t, sha1); + status = remove_note(t, oid.hash); if (status) fprintf(stderr, _("Object %s has no note\n"), name); else |