Skip to content

Commit 1cb3ed3

Browse files
committed
Merge branch 'jk/notes-merge-from-anywhere'
"git notes merge" used to limit the source of the merged notes tree to somewhere under refs/notes/ hierarchy, which was too limiting when inventing a workflow to exchange notes with remote repositories using remote-tracking notes trees (located in e.g. refs/remote-notes/ or somesuch). * jk/notes-merge-from-anywhere: notes: allow merging from arbitrary references
2 parents 0175655 + b3715b7 commit 1cb3ed3

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

builtin/notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static int merge(int argc, const char **argv, const char *prefix)
809809

810810
o.local_ref = default_notes_ref();
811811
strbuf_addstr(&remote_ref, argv[0]);
812-
expand_notes_ref(&remote_ref);
812+
expand_loose_notes_ref(&remote_ref);
813813
o.remote_ref = remote_ref.buf;
814814

815815
t = init_notes_check("merge", NOTES_INIT_WRITABLE);

notes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,3 +1306,13 @@ void expand_notes_ref(struct strbuf *sb)
13061306
else
13071307
strbuf_insert(sb, 0, "refs/notes/", 11);
13081308
}
1309+
1310+
void expand_loose_notes_ref(struct strbuf *sb)
1311+
{
1312+
unsigned char object[20];
1313+
1314+
if (get_sha1(sb->buf, object)) {
1315+
/* fallback to expand_notes_ref */
1316+
expand_notes_ref(sb);
1317+
}
1318+
}

notes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,11 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
302302
/* Expand inplace a note ref like "foo" or "notes/foo" into "refs/notes/foo" */
303303
void expand_notes_ref(struct strbuf *sb);
304304

305+
/*
306+
* Similar to expand_notes_ref, but will check whether the ref can be located
307+
* via get_sha1 first, and only falls back to expand_notes_ref in the case
308+
* where get_sha1 fails.
309+
*/
310+
void expand_loose_notes_ref(struct strbuf *sb);
311+
305312
#endif

t/t3308-notes-merge.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ test_expect_success setup '
1818
git notes add -m "Notes on 1st commit" 1st &&
1919
git notes add -m "Notes on 2nd commit" 2nd &&
2020
git notes add -m "Notes on 3rd commit" 3rd &&
21-
git notes add -m "Notes on 4th commit" 4th
21+
git notes add -m "Notes on 4th commit" 4th &&
22+
# Copy notes to remote-notes
23+
git fetch . refs/notes/*:refs/remote-notes/origin/*
2224
'
2325

2426
commit_sha1=$(git rev-parse 1st^{commit})
@@ -66,7 +68,9 @@ test_expect_success 'verify initial notes (x)' '
6668
'
6769

6870
cp expect_notes_x expect_notes_y
71+
cp expect_notes_x expect_notes_v
6972
cp expect_log_x expect_log_y
73+
cp expect_log_x expect_log_v
7074

7175
test_expect_success 'fail to merge empty notes ref into empty notes ref (z => y)' '
7276
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' '
8488
test_must_fail git -c "core.notesRef=refs/notes/foo^{bar" notes merge x
8589
'
8690

87-
test_expect_success 'fail to merge various non-note-trees' '
88-
git config core.notesRef refs/notes/y &&
89-
test_must_fail git notes merge refs/notes &&
90-
test_must_fail git notes merge refs/notes/ &&
91-
test_must_fail git notes merge refs/notes/dir &&
92-
test_must_fail git notes merge refs/notes/dir/ &&
93-
test_must_fail git notes merge refs/heads/master &&
94-
test_must_fail git notes merge x: &&
95-
test_must_fail git notes merge x:foo &&
96-
test_must_fail git notes merge foo^{bar
91+
test_expect_success 'merge non-notes ref into empty notes ref (remote-notes/origin/x => v)' '
92+
git config core.notesRef refs/notes/v &&
93+
git notes merge refs/remote-notes/origin/x &&
94+
verify_notes v &&
95+
# refs/remote-notes/origin/x and v should point to the same notes commit
96+
test "$(git rev-parse refs/remote-notes/origin/x)" = "$(git rev-parse refs/notes/v)"
9797
'
9898

9999
test_expect_success 'merge notes into empty notes ref (x => y)' '

0 commit comments

Comments
 (0)