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
BHBernt Hansen <bernt@norang.ca>
Date
Jan 6, 2009, 15:48 UTC
Message-ID
<87d4f01lmt.fsf@gollum.intra.norang.ca>
In-Reply-To
<alpine.DEB.1.10.0901021405460.21567@asgard.lang.hm>
david@lang.hm writes:
Show 31 quoted lines
> I have a need to setup a repository where I'm storing config files,
> and I need to be able to search the history of a particular line, not
> just when the last edit of the line was (which is what I see from git
> blame)
>
> I'm not seeing a obvious way to do this, am I missing something or
> does it need a non-obvious approach?
>
> for example, if I do
>
> git blame -L /SUBLEVEL/,+1 -M Makefile
>
> on the linux kernel it currently shows
>
> 57f8f7b6 (Linus Torvalds 2008-10-23 20:06:52 -0700 3) SUBLEVEL = 28
>
> what I would want it to show would be a list of the commits that have
> changed this line.
>
> It looks like I can write a script to do this
>
> git blame -L /SUBLEVEL/,+1 -M Makefile 57f8f7b6^
> 6e86841d (Linus Torvalds 2008-07-28 19:40:31 -0700 3) SUBLEVEL = 27
> git blame -L /SUBLEVEL/,+1 -M Makefile 6e86841d^
> 2ddcca36 (Linus Torvalds 2008-05-03 11:59:44 -0700 3) SUBLEVEL = 26
>
> etc.
>
> is there a better way to do this?
>
> David Lang
I think you need a script to do what you want.  I think this works...

Save the following script in ~/bin/git-rblame.sh, make it executable, and then create a global git alias for it as follows:

$ git config --global alias.rblame '!~/bin/git-rblame.sh $*'
Then you can just use

$ git rblame -L /SUBLEVEL/,+1 -M Makefile 6e86841d (Linus Torvalds 2008-07-28 19:40:31 -0700 3) SUBLEVEL = 27 2ddcca36 (Linus Torvalds 2008-05-03 11:59:44 -0700 3) SUBLEVEL = 26 ... 4c91aedb (Linus Torvalds 2005-06-28 22:57:29 -0700 3) SUBLEVEL = 13 ^1da177e (Linus Torvalds 2005-04-16 15:20:36 -0700 3) SUBLEVEL = 12

------cut here ------ !/bin/git-rblame.sh ---
#!/bin/sh
PARAMS="$*"
LINE=$(git blame $PARAMS)
while test $? == 0
do
    echo $LINE
    COMMIT="${LINE:0:8}^"
    LINE=$(git blame $PARAMS $COMMIT 2>/dev/null)
done
------cut here ------------------------------

Cheers, Bernt

Previous: david@lang.hmNext: Miklos Vajna
Message 14 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.