]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Make "git notes add" more user-friendly when there are existing notes
authorJohan Herland <johan@herland.net>
Wed, 30 Mar 2011 00:02:55 +0000 (02:02 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 Mar 2011 20:13:52 +0000 (13:13 -0700)
Currently, "notes add" (without -f/--force) will abort when the given object
already has existing notes. This makes sense for the modes of "git notes add"
that would necessarily overwrite the old message (when using the -m/-F/-C/-c
options). However, when no options are given (meaning the notes are created
from scratch in the editor) it is not very user-friendly to abort on existing
notes, and forcing the user to run "git notes edit".

Instead, it is better to simply "redirect" to "git notes edit" automatically,
i.e. open the existing notes in the editor and let the user edit them.
This patch does just that.

This changes the behavior of "git notes add" without options when notes
already exist for the given object, but I doubt that many users really depend
on the previous failure from "git notes add" in this case.

Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-notes.txt
builtin/notes.c
t/t3301-notes.sh

index 296f314eae5af684e68965f4b04bd9acbd4c35a1..913ecd8c433b7ebaaf932dd5cf9b789dc06b7b95 100644 (file)
@@ -57,8 +57,11 @@ list::
 
 add::
        Add notes for a given object (defaults to HEAD). Abort if the
-       object already has notes (use `-f` to overwrite an
-       existing note).
+       object already has notes (use `-f` to overwrite existing notes).
+       However, if you're using `add` interactively (using an editor
+       to supply the notes contents), then - instead of aborting -
+       the existing notes will be opened in the editor (like the `edit`
+       subcommand).
 
 copy::
        Copy the notes for the first object onto the second object.
index a0f310b72956dcb00d2a0899d39ddfb9f7811fa4..0d133be49b1987e8a3c6cb8d404654ed9c3328c3 100644 (file)
@@ -527,6 +527,8 @@ static int list(int argc, const char **argv, const char *prefix)
        return retval;
 }
 
+static int append_edit(int argc, const char **argv, const char *prefix);
+
 static int add(int argc, const char **argv, const char *prefix)
 {
        int retval = 0, force = 0;
@@ -554,14 +556,14 @@ static int add(int argc, const char **argv, const char *prefix)
        };
 
        argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
-                            0);
+                            PARSE_OPT_KEEP_ARGV0);
 
-       if (1 < argc) {
+       if (2 < argc) {
                error("too many parameters");
                usage_with_options(git_notes_add_usage, options);
        }
 
-       object_ref = argc ? argv[0] : "HEAD";
+       object_ref = argc > 1 ? argv[1] : "HEAD";
 
        if (get_sha1(object_ref, object))
                die("Failed to resolve '%s' as a valid ref.", object_ref);
@@ -571,6 +573,18 @@ static int add(int argc, const char **argv, const char *prefix)
 
        if (note) {
                if (!force) {
+                       if (!msg.given) {
+                               /*
+                                * Redirect to "edit" subcommand.
+                                *
+                                * We only end up here if none of -m/-F/-c/-C
+                                * or -f are given. The original args are
+                                * therefore still in argv[0-1].
+                                */
+                               argv[0] = "edit";
+                               free_notes(t);
+                               return append_edit(argc, argv, prefix);
+                       }
                        retval = error("Cannot add notes. Found existing notes "
                                       "for object %s. Use '-f' to overwrite "
                                       "existing notes", sha1_to_hex(object));
index 1921ca3a73370d378e6aedd0820fd8d2a030da03..3448c23776dc867fa0542665f69ccb27ac564337 100755 (executable)
@@ -101,8 +101,8 @@ test_expect_success 'edit existing notes' '
        test_must_fail git notes show HEAD^
 '
 
-test_expect_success 'cannot add note where one exists' '
-       ! MSG=b2 git notes add &&
+test_expect_success 'cannot "git notes add -m" where notes already exists' '
+       test_must_fail git notes add -m "b2" &&
        test ! -f .git/NOTES_EDITMSG &&
        test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
        test b3 = $(git notes show) &&
@@ -110,6 +110,24 @@ test_expect_success 'cannot add note where one exists' '
        test_must_fail git notes show HEAD^
 '
 
+test_expect_success 'can overwrite existing note with "git notes add -f -m"' '
+       git notes add -f -m "b1" &&
+       test ! -f .git/NOTES_EDITMSG &&
+       test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
+       test b1 = $(git notes show) &&
+       git show HEAD^ &&
+       test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'add w/no options on existing note morphs into edit' '
+       MSG=b2 git notes add &&
+       test ! -f .git/NOTES_EDITMSG &&
+       test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
+       test b2 = $(git notes show) &&
+       git show HEAD^ &&
+       test_must_fail git notes show HEAD^
+'
+
 test_expect_success 'can overwrite existing note with "git notes add -f"' '
        MSG=b1 git notes add -f &&
        test ! -f .git/NOTES_EDITMSG &&
@@ -194,6 +212,13 @@ test_expect_success 'show -F notes' '
        test_cmp expect-F output
 '
 
+test_expect_success 'Re-adding -F notes without -f fails' '
+       echo "zyxxy" > note5 &&
+       test_must_fail git notes add -F note5 &&
+       git log -3 > output &&
+       test_cmp expect-F output
+'
+
 cat >expect << EOF
 commit 15023535574ded8b1a89052b32673f84cf9582b8
 tree e070e3af51011e47b183c33adf9736736a525709