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

Re: how to track the history of a line in a file

From
Ddavid@lang.hm <david@lang.hm>
Date
Jan 2, 2009, 23:56 UTC
Message-ID
<alpine.DEB.1.10.0901021554581.21567@asgard.lang.hm>
In-Reply-To
<alpine.DEB.1.10.0901021544580.21567@asgard.lang.hm>
On Fri, 2 Jan 2009, david@lang.hm wrote:
Show 49 quoted lines
> On Fri, 2 Jan 2009, david@lang.hm wrote:
>
>> On Fri, 2 Jan 2009, Jeff King wrote:
>> 
>>> The tricky thing here is what is "this line"? Using the line number
>>> isn't right, since it will change based on other content coming in and
>>> out of the file. You can keep drilling down by reblaming parent commits,
>>> but remember that each time you do that you are manually looking at the
>>> content and saying "Oh, this is the line I am still interested in." So I
>>> a script would have to correlate the old version and new version of the
>>> line and realize how to follow the "interesting" thing.
>>> 
>>> In your case, I think you want to see any commit in Makefile which
>>> changed a line with SUBLEVEL in it. Which is maybe easiest done as:
>>>
>>>  git log -z -p Makefile |
>>>    perl -0ne 'print if /\n[+-]SUBLEVEL/' |
>>>    tr '\0' '\n'
>>> 
>>> and is pretty fast. But obviously we're leveraging some content-specific
>>> knowledge about what's in the Makefile.
>> 
>> Ok, I hacked togeather a quick bash script to try this
>> 
> <SNIP>
>> the problem that this has is that line 3 of $COMMIT may not be line 3 of 
>> $COMMIT^, and if they aren't it ends up hunting down the wrong data
>> 
>> either that or I am not understanding the output of git blame properly 
>> (also very possible)
>
> I was misunderstanding git blame
>
> new script is
>
> #!/bin/bash
> line=`git blame -n -b -l -L /$1/,+1 -M $2`
> echo "-$line"
> foundCOMMIT=`echo "$line" |cut -c -40`
> foundline=`echo "$line" |cut -c 42- |cut -f 1 -d " "`
> while [ "$foundCOMMIT" != "                                        " ] ;do
> #git diff -U0 $foundCOMMIT..$foundCOMMIT^ $2
> line=`git blame -n -b -l -L $foundline,+1 -M $2 $foundCOMMIT^`
> echo "-$line"
> foundCOMMIT=`echo "$line" |cut -c -40`
> foundline=`echo "$line" |cut -c 42- |cut -f 1 -d " "`
> done
>
> this seems to be working for me now.

not quite, it works as long as the line doesn't move in the commit where it changes.

David Lang
Previous: david@lang.hmNext: david@lang.hm
Message 12 of 17 in “how to track the history of a line in a file”
  1. david@lang.hmJan 2, 2009
  2. Jeff KingJan 2, 2009
  3. david@lang.hmJan 2, 2009
  4. Jeff WhitesideJan 2, 2009
  5. david@lang.hmJan 2, 2009
  6. david@lang.hmJan 2, 2009
  7. david@lang.hmJan 2, 2009
  8. Junio C HamanoJan 2, 2009
  9. david@lang.hmJan 3, 2009
  10. Thomas RastJan 3, 2009
  11. david@lang.hmJan 3, 2009
  12. david@lang.hmJan 2, 2009
  13. david@lang.hmJan 3, 2009
  14. Bernt HansenJan 6, 2009
  15. Miklos VajnaJan 6, 2009
  16. Bernt HansenJan 6, 2009
  17. Bernt HansenJan 6, 2009

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.