Search code examples
gitsearchgit-log

Why does git log command only respond to a committed code line that is referenced without the # comment?


I had commented out line of code in a calling script. I knew my last commit date. But was not sure whether I had made this code change at that time or earlier.

github repo line of code

I wanted to know when I removed this function call

$ git log -S"# $pageString = sortRollListings $pageString"

No return from git log for this search option. But when I take out the comment-space characters, in the quoted code string, git log returns what I am after.

$ git log -S"$pageString = sortRollListings $pageString"
commit 6be2eb07f432069c57b729568f08b305f7ec5b78 (HEAD -> master)
Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date:   Wed Dec 7 15:46:49 2022 +1300

    Removed redundant code from rollConvertReport.ps1

commit 4eea8cdbc364b80fc63996294b309168b3d2e911
Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date:   Sat Dec 3 15:27:19 2022 +1300

    Function readPageOfDblePage redeveloped from halveListings.ps1

commit b9f7fa13cc99caed32be55f57e0fa2fcd9a97db3
Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date:   Sat Oct 15 17:08:10 2022 +1300

    Revised function expandStreet() to be clearer

commit 55f83a1e9737ea981a4badc8c03b6e5ef31c825f
Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date:   Sun Oct 9 17:39:21 2022 +1300

    Created rollConvertReport & modified headParserRev() to return header line
:

The docs paragraph on -S says:

-S<string>

Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.

It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.

I don't understand "use the feature iteratively to feed the interesting block in the preimage back into -S" But I'm pretty sure I'm using this search option as intended.

It appears to me that the quote string, without the hash comment symbol, returns the line of code (being quoted in the git log search option -S) to it's pre-commit state. And because of this is successful. This is like having to quote missing lines of code to find when the lines were removed. Which clearly is not right.

Does anyone have an idea why I'm getting this behavior from git log


Solution

  • This is probably due to using double-quotes ("), with which the shell tries to parse the strings, replacing the dollar-sign variables with its own variables, which are probably nonexistent (therefore replaced with empty strings). Consider the following git history:

    commit 58861d3e06e861244bf15ad3ea83e04320634399 (HEAD -> main)
    
        NewStuff
    
    diff --git a/asdf b/asdf
    index af65c13..6262dfa 100644
    --- a/asdf
    +++ b/asdf
    @@ -4,7 +4,7 @@ first
     3
     4
     somefunc New_func(): {
    -    $newString = sortRollListings $newString
    +    someNewStuff()
     }
     5
     6
    
    commit a960766cdeba9eaa6ac7bd9e07814e27985c3714
    
        uncomment Newfunc
    
    diff --git a/asdf b/asdf
    index 57e6274..af65c13 100644
    --- a/asdf
    +++ b/asdf
    @@ -4,7 +4,7 @@ first
     3
     4
     somefunc New_func(): {
    -    # $newString = sortRollListings $newString
    +    $newString = sortRollListings $newString
     }
     5
     6
    
    commit ec89357696d25239c418a72aed832d9a0d431508
    
        Newfunc
    
    diff --git a/asdf b/asdf
    index d33a71a..57e6274 100644
    --- a/asdf
    +++ b/asdf
    @@ -3,6 +3,9 @@ first
     2
     3
     4
    +somefunc New_func(): {
    +    # $newString = sortRollListings $newString
    +}
     5
     6
     7
    
    commit b953ec0117cd41ba9a36a3a1b0bf925997bb6eb5
    
        Other stuff
    
    diff --git a/asdf b/asdf
    index 84d1114..d33a71a 100644
    --- a/asdf
    +++ b/asdf
    @@ -12,7 +12,7 @@ first
     11
     12
     somefunc func_name(): {
    -    # $pageString = sortRollListings $pageString
    +    someOtherStuff()
     }
     13
     14
    
    commit 116d0eee9fa6fd4280e41a15469bf6a7361cc63a
    
        comment out
    
    diff --git a/asdf b/asdf
    index bb31e52..84d1114 100644
    --- a/asdf
    +++ b/asdf
    @@ -12,7 +12,7 @@ first
     11
     12
     somefunc func_name(): {
    -    $pageString = sortRollListings $pageString
    +    # $pageString = sortRollListings $pageString
     }
     13
     14
    
    commit f536bdc3f46de0ddfa1fec0e36eb0c311c5e39b6
    
        added func
    
    diff --git a/asdf b/asdf
    index e08a598..bb31e52 100644
    --- a/asdf
    +++ b/asdf
    @@ -11,6 +11,9 @@ first
     10
     11
     12
    +somefunc func_name(): {
    +    $pageString = sortRollListings $pageString
    +}
     13
     14
     15
    
    

    Running git log -S"# $newString = sortRollListings $newString" yields no results, however when removing the pound-sign (#), the results are as follows:

    $ git log --oneline -S"$newString = sortRollListings $newString"
    58861d3 (HEAD -> main) NewStuff
    ec89357 Newfunc
    b953ec0 Other stuff
    f536bdc added func
    

    Looking closely, commit f536bdc has nothing to do with $newString, but is included as that gets interpreted as empty-string, and = sortRollListings is relevant to the search. By changing to single-quotes ('), the shell will no longer attempt to parse the string, so the results are more appropriate:

    $ git log --oneline -S'$newString = sortRollListings $newString'
    58861d3 (HEAD -> main) NewStuff
    ec89357 Newfunc
    

    This will also work with the pound-sign:

    $ git log --oneline -S'# $newString = sortRollListings $newString'
    a960766 uncomment Newfunc
    ec89357 Newfunc
    

    and also for something more like the code in question:

    $ git log -p -S'# $pageString = sortRollListings $pageString'
    commit b953ec0117cd41ba9a36a3a1b0bf925997bb6eb5
    
        Other stuff
    
    diff --git a/asdf b/asdf
    index 84d1114..d33a71a 100644
    --- a/asdf
    +++ b/asdf
    @@ -12,7 +12,7 @@ first
     11
     12
     somefunc func_name(): {
    -    # $pageString = sortRollListings $pageString
    +    someOtherStuff()
     }
     13
     14
    
    commit 116d0eee9fa6fd4280e41a15469bf6a7361cc63a
    
        comment out
    
    diff --git a/asdf b/asdf
    index bb31e52..84d1114 100644
    --- a/asdf
    +++ b/asdf
    @@ -12,7 +12,7 @@ first
     11
     12
     somefunc func_name(): {
    -    $pageString = sortRollListings $pageString
    +    # $pageString = sortRollListings $pageString
     }
     13
     14
    

    To sum up: replacing the double-quotes (") with single-quotes (') would probably resolve this issue.