Skip to contentSkip to conversation

scripts: git-resolve: search beyond ten body-only matches

PATCH Superseded Hamdy Abou El Anein 2 participants 4 messages Nov 3 – Nov 4

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: git-resolve: search beyond ten body-only matches

From: Hamdy Abou El Anein <[email protected]>

The forced subject lookup asks git log for at most ten matches before checking their subjects. Since --grep also searches message bodies, ten newer body-only matches can hide an older commit with the requested subject.

Remove the premature limit. The existing reader still stops at the first matching subject.

Tested with a target commit followed by eleven commits whose bodies, but not subjects, match the query. Forced lookup now returns the target.

AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.

Fixes 7ae52a3d7f51 ("scripts: Add git-resolve tool for full SHA-1 resolution")
---
 scripts/git-resolve.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/git-resolve.sh +1 -1
index 0be8838cd13..4b51d40bebf 100755
@@ -74,7 +74,7 @@ git_resolve_commit() {
 		if [ ${#matches[@]} -eq 0 ] && [ $force -eq 1 ]; then
 			# Use git log to search, but filter to ensure subject matches exactly
 			local match
-			match=$(git log --format="%H %s" --grep="$grep_pattern" --perl-regexp -10 | \
+			match=$(git log --format="%H %s" --grep="$grep_pattern" --perl-regexp | \
 					while read -r hash subject; do
 						if echo "$subject" | grep -qP "$grep_pattern"; then
 							echo "$hash"
#1 lore raw
Quoted 7 lines · Hamdy Abou El Anein

The forced subject lookup asks git log for at most ten matches before checking their subjects. Since --grep also searches message bodies, ten newer body-only matches can hide an older commit with the requested subject.

Remove the premature limit. The existing reader still stops at the first matching subject.

The fix is correct, but without the limit a broad wildcard that matches many bodies and no subject gets slow. In linux.git, --force 999999999 '("...Tested-by...")' went from returning immediately to over a minute, since the loop runs grep once for each of the 78000 commits whose message matches.

Matching the subject directly avoids body matches altogether:

	match=$(git log --format="%H %s" | grep -m1 -P "^[0-9a-f]+ ${grep_pattern#^}" | cut -d" " -f1)

No limit is needed then. It gives the same results on the selftest and on your eleven commit case, finds an existing subject in 0.1s instead of 10s, and takes 12s for the wildcard above. It also resolves subjects with leading whitespace or that span several lines, which the read loop misses today.

Could you send a v2 that does this instead?

Signature · 2 lines
Thanks,
Sasha
#2 lore raw

PATCH v2 1/1 scripts: git-resolve: search beyond ten body-only matches

In reply to Hamdy Abou El Anein

From: Hamdy Abou El Anein <[email protected]>

The forced subject lookup asks git log for at most ten matches before checking their subjects. Since --grep also searches message bodies, ten newer body-only matches can hide an older commit with the requested subject.

Match commit subjects directly, so body-only matches cannot hide an older subject and grep stops at the first matching subject.

Tested with a target commit followed by eleven commits whose bodies, but not subjects, match the query. Forced lookup returns the target.

AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.

Fixes 7ae52a3d7f51 ("scripts: Add git-resolve tool for full SHA-1 resolution")
---
 scripts/git-resolve.sh | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
scripts/git-resolve.sh +2 -7
index 7d14cf279e86..f04a56c5fba0 100755
@@ -74,13 +74,8 @@ git_resolve_commit() {
 		if [ ${#matches[@]} -eq 0 ] && [ $force -eq 1 ]; then
 			# Use git log to search, but filter to ensure subject matches exactly
 			local match
-			match=$(git log --format="%H %s" --grep="$grep_pattern" --perl-regexp -10 | \
-					while read -r hash subject; do
-						if echo "$subject" | grep -qP "$grep_pattern"; then
-							echo "$hash"
-							break
-						fi
-					done)
+			match=$(git log --format="%H %s" | \
+				grep -m1 -P "^[0-9a-f]+ ${grep_pattern#^}" | cut -d" " -f1)
 			if [ -n "$match" ]; then
 				echo "$match"
 				return 0
Signature · 1 line
2.56.0
Acked-by on v2 1/1
#3 lore raw
Quoted 7 lines · Hamdy Abou El Anein

The forced subject lookup asks git log for at most ten matches before checking their subjects. Since --grep also searches message bodies, ten newer body-only matches can hide an older commit with the requested subject.

Match commit subjects directly, so body-only matches cannot hide an older subject and grep stops at the first matching subject.

--
Thanks,
Sasha