worktree: add can be created from any commit-ish
[gitweb.git] / builtin / notes.c
index 7947a16ede29508aa32fe176726edd67fa4e78cd..d7754db143e21ed92e03a29ea7932aec599ea63f 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "notes.h"
 #include "blob.h"
@@ -309,7 +310,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
                if (rewrite_cmd)
                        err = copy_note_for_rewrite(c, &from_obj, &to_obj);
                else
-                       err = copy_note(t, from_obj.hash, to_obj.hash, force,
+                       err = copy_note(t, &from_obj, &to_obj, force,
                                        combine_notes_overwrite);
 
                if (err) {
@@ -327,6 +328,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
        } else {
                finish_copy_notes_for_rewrite(c, msg);
        }
+       strbuf_release(&buf);
        return ret;
 }
 
@@ -340,8 +342,10 @@ static struct notes_tree *init_notes_check(const char *subcommand,
 
        ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
        if (!starts_with(ref, "refs/notes/"))
-               /* TRANSLATORS: the first %s will be replaced by a
-                  git notes command: 'add', 'merge', 'remove', etc.*/
+               /*
+                * TRANSLATORS: the first %s will be replaced by a git
+                * notes command: 'add', 'merge', 'remove', etc.
+                */
                die(_("refusing to %s notes in %s (outside of refs/notes/)"),
                    subcommand, ref);
        return t;
@@ -370,7 +374,7 @@ static int list(int argc, const char **argv, const char *prefix)
        if (argc) {
                if (get_oid(argv[0], &object))
                        die(_("failed to resolve '%s' as a valid ref."), argv[0]);
-               note = get_note(t, object.hash);
+               note = get_note(t, &object);
                if (note) {
                        puts(oid_to_hex(note));
                        retval = 0;
@@ -427,7 +431,7 @@ static int add(int argc, const char **argv, const char *prefix)
                die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
        t = init_notes_check("add", NOTES_INIT_WRITABLE);
-       note = get_note(t, object.hash);
+       note = get_note(t, &object);
 
        if (note) {
                if (!force) {
@@ -453,10 +457,10 @@ static int add(int argc, const char **argv, const char *prefix)
                        oid_to_hex(&object));
        }
 
-       prepare_note_data(&object, &d, note->hash);
+       prepare_note_data(&object, &d, note ? note->hash : NULL);
        if (d.buf.len || allow_empty) {
                write_note_data(&d, new_note.hash);
-               if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite))
+               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 {
@@ -518,7 +522,7 @@ static int copy(int argc, const char **argv, const char *prefix)
                die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
        t = init_notes_check("copy", NOTES_INIT_WRITABLE);
-       note = get_note(t, object.hash);
+       note = get_note(t, &object);
 
        if (note) {
                if (!force) {
@@ -532,14 +536,14 @@ static int copy(int argc, const char **argv, const char *prefix)
                        oid_to_hex(&object));
        }
 
-       from_note = get_note(t, from_obj.hash);
+       from_note = get_note(t, &from_obj);
        if (!from_note) {
                retval = error(_("missing notes on source object %s. Cannot "
                               "copy."), oid_to_hex(&from_obj));
                goto out;
        }
 
-       if (add_note(t, object.hash, from_note->hash, 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:
@@ -596,7 +600,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
                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.hash);
+       note = get_note(t, &object);
 
        prepare_note_data(&object, &d, edit && note ? note->hash : NULL);
 
@@ -616,7 +620,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 
        if (d.buf.len || allow_empty) {
                write_note_data(&d, new_note.hash);
-               if (add_note(t, object.hash, new_note.hash, combine_notes_overwrite))
+               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 {
@@ -658,7 +662,7 @@ static int show(int argc, const char **argv, const char *prefix)
                die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
        t = init_notes_check("show", 0);
-       note = get_note(t, object.hash);
+       note = get_note(t, &object);
 
        if (!note)
                retval = error(_("no note found for object %s."),
@@ -682,7 +686,7 @@ static int merge_abort(struct notes_merge_options *o)
 
        if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
                ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
-       if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
+       if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
                ret += error(_("failed to delete ref NOTES_MERGE_REF"));
        if (notes_merge_abort(o))
                ret += error(_("failed to remove 'git notes merge' worktree"));
@@ -720,11 +724,11 @@ static int merge_commit(struct notes_merge_options *o)
        init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
 
        o->local_ref = local_ref_to_free =
-               resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
+               resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
        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 */
@@ -732,8 +736,8 @@ static int merge_commit(struct notes_merge_options *o)
        format_commit_message(partial, "%s", &msg, &pretty_ctx);
        strbuf_trim(&msg);
        strbuf_insert(&msg, 0, "notes: ", 7);
-       update_ref(msg.buf, o->local_ref, oid.hash,
-                  is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
+       update_ref(msg.buf, o->local_ref, &oid,
+                  is_null_oid(&parent_oid) ? NULL : &parent_oid,
                   0, UPDATE_REFS_DIE_ON_ERR);
 
        free_notes(t);
@@ -842,16 +846,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_oid.hash);
+       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_oid.hash, NULL,
-                          0, UPDATE_REFS_DIE_ON_ERR);
+               update_ref(msg.buf, default_notes_ref(), &result_oid, 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_oid.hash, NULL,
+               update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, 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());