Skip to content

Commit b3715b7

Browse files
jacob-kellergitster
authored andcommitted
notes: allow merging from arbitrary references
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 <[email protected]> Reviewed-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28274d0 commit b3715b7

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
@@ -806,7 +806,7 @@ static int merge(int argc, const char **argv, const char *prefix)
806806

807807
o.local_ref = default_notes_ref();
808808
strbuf_addstr(&remote_ref, argv[0]);
809-
expand_notes_ref(&remote_ref);
809+
expand_loose_notes_ref(&remote_ref);
810810
o.remote_ref = remote_ref.buf;
811811

812812
t = init_notes_check("merge");

notes.c

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

notes.h

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

297+
/*
298+
* Similar to expand_notes_ref, but will check whether the ref can be located
299+
* via get_sha1 first, and only falls back to expand_notes_ref in the case
300+
* where get_sha1 fails.
301+
*/
302+
void expand_loose_notes_ref(struct strbuf *sb);
303+
297304
#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)