Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git to find first introduction of token on a specific line of a file

Let's say I have a file A.cpp, and I notice an error on line 15 of the file. Let's say the error is a "const" on a function that returns a pointer to a member variable, meaning using const on the function is technically correct but semantically wrong. I would like to discuss the semantics with the author who made the change.

Using git, is there a way to find out which revision introduced the "const" token? More specifically, I'd like to know who introduced the token.

"git blame" shows who made the last change to the line, but I would actually like to find the first commit containing the token.

like image 806
paxos1977 Avatar asked Dec 07 '09 22:12

paxos1977


People also ask

How do I blame a specific line in git?

If you want to inspect only specific ranges of lines of a file using git blame, then you can use the -L option.

Which command shows author of each line in git?

The git blame command is used to examine the contents of a file line by line and see when each line was last modified and who the author of the modifications was.

How do I find a specific commit in git?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do you get details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


1 Answers

There are a few possible ways of doing it.

  • git blame, or better graphical blame tool (like git gui blame or the blame view in git instaweb / gitweb) to browse history of a line, going back in history until you find appropriate commit.

  • so called "pickaxe search", i.e. git log -S with appropriate token / regexp, to find (list) all commits where number of given tokens changed (which usually means where given token was added or deleted), e.g.:

    git log --reverse -p -S'const int foobar' -- A.cpp
    
  • git bisect where "bad" commit would mean the one with 'const' where there it shouldn't be (testing using e.g. grep).

like image 100
Jakub Narębski Avatar answered Nov 16 '22 01:11

Jakub Narębski