Skip to content

Commit 6b3351e

Browse files
wchargingitster
authored andcommitted
sha1-name.c: for ":/", find detached HEAD commits
This patch broadens the set of commits matched by ":/<pattern>" to include commits reachable from HEAD but not any named ref. This avoids surprising behavior when working with a detached HEAD and trying to refer to a commit that was recently created and only exists within the detached state. If multiple worktrees exist, only the current worktree's HEAD is considered reachable. This is consistent with the existing behavior for other per-worktree refs: e.g., bisect refs are considered reachable, but only within the relevant worktree. Signed-off-by: Jeff King <[email protected]> Signed-off-by: William Chargin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a42a58d commit 6b3351e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Documentation/revisions.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ existing tag object.
180180
A colon, followed by a slash, followed by a text, names
181181
a commit whose commit message matches the specified regular expression.
182182
This name returns the youngest matching commit which is
183-
reachable from any ref. The regular expression can match any part of the
183+
reachable from any ref, including HEAD.
184+
The regular expression can match any part of the
184185
commit message. To match messages starting with a string, one can use
185186
e.g. ':/^foo'. The special sequence ':/!' is reserved for modifiers to what
186187
is matched. ':/!-foo' performs a negative match, while ':/!!foo' matches a

sha1_name.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ static int get_oid_with_context_1(const char *name,
16491649
struct commit_list *list = NULL;
16501650

16511651
for_each_ref(handle_one_ref, &list);
1652+
head_ref(handle_one_ref, &list);
16521653
commit_list_sort_by_date(&list);
16531654
return get_oid_oneline(name + 2, oid, list);
16541655
}

t/t4208-log-magic-pathspec.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ test_expect_success '"git log :/a -- " should not be ambiguous' '
2525
git log :/a --
2626
'
2727

28+
test_expect_success '"git log :/detached -- " should find a commit only in HEAD' '
29+
test_when_finished "git checkout master" &&
30+
git checkout --detach &&
31+
# Must manually call `test_tick` instead of using `test_commit`,
32+
# because the latter additionally creates a tag, which would make
33+
# the commit reachable not only via HEAD.
34+
test_tick &&
35+
git commit --allow-empty -m detached &&
36+
test_tick &&
37+
git commit --allow-empty -m something-else &&
38+
git log :/detached --
39+
'
40+
41+
test_expect_success '"git log :/detached -- " should not find an orphaned commit' '
42+
test_must_fail git log :/detached --
43+
'
44+
45+
test_expect_success '"git log :/detached -- " should find HEAD only of own worktree' '
46+
git worktree add other-tree HEAD &&
47+
git -C other-tree checkout --detach &&
48+
test_tick &&
49+
git -C other-tree commit --allow-empty -m other-detached &&
50+
git -C other-tree log :/other-detached -- &&
51+
test_must_fail git log :/other-detached --
52+
'
53+
2854
test_expect_success '"git log -- :/a" should not be ambiguous' '
2955
git log -- :/a
3056
'

0 commit comments

Comments
 (0)