git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Expected behaviour of 'git log -S' when searching in a merged/deleted file?

From
Jonathan del Strother <jdelstrother@gmail.com>
Date
Dec 22, 2010, 13:37 UTC
Message-ID
<AANLkTimXk6ei6EAQfvTTfnMzdBqYHkNoaxkEab+atnHd@mail.gmail.com>
Hi,
I was trying to find a particular string in my project this morning.
'git grep mystring' suggested that the string didn't exist in my repo,
but 'git log -Smystring' turned up a single commit that had added it.
It took me a long time to figure out that in the past, a branch had
added that string to foo.c, but a second branch deleted foo.c, and the
two branches were later merged (deleting foo.c and ignoring mystring).
 I was surprised that 'git log -S' didn't show the merge commit as the
point at which the string had been removed.

I've attached a testcase which I would expect to pass (perhaps naively), but doesn't. Is this a git bug, or do I misunderstand git log -S?

--
#!/bin/sh
test_description='git log'
. ./test-lib.sh
test_expect_success setup '
	echo haystack\\nhaystack\\nhaystack\\nhaystack\\n > haystack &&
	git add haystack &&
	test_tick &&
	git commit -m initial &&
	git checkout -b branchA &&
	echo needle >haystack &&
	git add haystack &&
	test_tick &&
	git commit -m "adding needle" &&
	git checkout -b branchB HEAD~1 &&
	git rm haystack &&
	test_tick &&
	git commit -m "removing haystack" &&
	git merge branchA || git rm haystack &&
	test_tick &&
	git commit -m "merging: haystack and needle removed"
'

printf "merging: haystack and needle removed\nadding needle" > expect test_expect_success 'log -S in a merge-deleted file' '

	git log -Sneedle --pretty="format:%s" > actual &&
	test_cmp expect actual
'
test_done
Next: Junio C Hamano
Message 1 of 5 in “Expected behaviour of 'git log -S' when searching in a merged/deleted file?”
  1. Jonathan del StrotherDec 22, 2010
  2. Junio C HamanoDec 22, 2010
  3. Jonathan del StrotherDec 23, 2010
  4. Junio C HamanoDec 23, 2010
  5. Jonathan del StrotherDec 23, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.