scripts: git-resolve: search beyond ten body-only matches
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.
--- scripts/git-resolve.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/git-resolve.sh +1 -1
@@ -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"
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
PATCH v2 1/1 scripts: git-resolve: search beyond ten body-only matches
In reply to Hamdy Abou El AneinFrom: 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.
--- scripts/git-resolve.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
scripts/git-resolve.sh +2 -7
@@ -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
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