notes: allow merging from arbitrary references
authorJacob Keller <jacob.keller@gmail.com>
Tue, 29 Dec 2015 22:40:28 +0000 (14:40 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 17 Jan 2016 21:59:01 +0000 (13:59 -0800)
Create a new expansion function, expand_loose_notes_ref which will first
check whether the ref can be found using get_sha1. If it can't be found
then it will fallback to using expand_notes_ref. The content of the
strbuf will not be changed if the notes ref can be located using
get_sha1. Otherwise, it may be updated as done by expand_notes_ref.

Since we now support merging from non-notes refs, remove the test case
associated with that behavior. Add a test case for merging from a
non-notes ref.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
Reviewed-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/notes.c
notes.c
notes.h
t/t3308-notes-merge.sh
index 52aa9af74be8d47780d4762185c5cbe09624b382..b16b8b56a0a2291f1a27be8e025ab3773197d77c 100644 (file)
@@ -806,7 +806,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 
        o.local_ref = default_notes_ref();
        strbuf_addstr(&remote_ref, argv[0]);
-       expand_notes_ref(&remote_ref);
+       expand_loose_notes_ref(&remote_ref);
        o.remote_ref = remote_ref.buf;
 
        t = init_notes_check("merge");
diff --git a/notes.c b/notes.c
index db77922130b4f7df6ab72122206a76d6c580faac..086cc483e518573827490a789a77f2adfa45e945 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1303,3 +1303,13 @@ void expand_notes_ref(struct strbuf *sb)
        else
                strbuf_insert(sb, 0, "refs/notes/", 11);
 }
+
+void expand_loose_notes_ref(struct strbuf *sb)
+{
+       unsigned char object[20];
+
+       if (get_sha1(sb->buf, object)) {
+               /* fallback to expand_notes_ref */
+               expand_notes_ref(sb);
+       }
+}
diff --git a/notes.h b/notes.h
index 2a3f92338076ef8e4b8238aaf570c3273b0f28f0..431f14378817b429e1042925bd639e17f339eb33 100644 (file)
--- a/notes.h
+++ b/notes.h
@@ -294,4 +294,11 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 /* Expand inplace a note ref like "foo" or "notes/foo" into "refs/notes/foo" */
 void expand_notes_ref(struct strbuf *sb);
 
+/*
+ * Similar to expand_notes_ref, but will check whether the ref can be located
+ * via get_sha1 first, and only falls back to expand_notes_ref in the case
+ * where get_sha1 fails.
+ */
+void expand_loose_notes_ref(struct strbuf *sb);
+
 #endif
index 24d82b49bbea6535f486b2ffdb4a5a761f736dcc..19aed7ec953b3d384a2f44ee2bae7fd4c92fc5fa 100755 (executable)
@@ -18,7 +18,9 @@ test_expect_success setup '
        git notes add -m "Notes on 1st commit" 1st &&
        git notes add -m "Notes on 2nd commit" 2nd &&
        git notes add -m "Notes on 3rd commit" 3rd &&
-       git notes add -m "Notes on 4th commit" 4th
+       git notes add -m "Notes on 4th commit" 4th &&
+       # Copy notes to remote-notes
+       git fetch . refs/notes/*:refs/remote-notes/origin/*
 '
 
 commit_sha1=$(git rev-parse 1st^{commit})
@@ -66,7 +68,9 @@ test_expect_success 'verify initial notes (x)' '
 '
 
 cp expect_notes_x expect_notes_y
+cp expect_notes_x expect_notes_v
 cp expect_log_x expect_log_y
+cp expect_log_x expect_log_v
 
 test_expect_success 'fail to merge empty notes ref into empty notes ref (z => y)' '
        test_must_fail git -c "core.notesRef=refs/notes/y" notes merge z
@@ -84,16 +88,12 @@ test_expect_success 'fail to merge into various non-notes refs' '
        test_must_fail git -c "core.notesRef=refs/notes/foo^{bar" notes merge x
 '
 
-test_expect_success 'fail to merge various non-note-trees' '
-       git config core.notesRef refs/notes/y &&
-       test_must_fail git notes merge refs/notes &&
-       test_must_fail git notes merge refs/notes/ &&
-       test_must_fail git notes merge refs/notes/dir &&
-       test_must_fail git notes merge refs/notes/dir/ &&
-       test_must_fail git notes merge refs/heads/master &&
-       test_must_fail git notes merge x: &&
-       test_must_fail git notes merge x:foo &&
-       test_must_fail git notes merge foo^{bar
+test_expect_success 'merge non-notes ref into empty notes ref (remote-notes/origin/x => v)' '
+       git config core.notesRef refs/notes/v &&
+       git notes merge refs/remote-notes/origin/x &&
+       verify_notes v &&
+       # refs/remote-notes/origin/x and v should point to the same notes commit
+       test "$(git rev-parse refs/remote-notes/origin/x)" = "$(git rev-parse refs/notes/v)"
 '
 
 test_expect_success 'merge notes into empty notes ref (x => y)' '