3

I´d like to display the change log of a line in a file. The problem is that the command git log {commitHash} -p -1 -L 17,+1:{filePath} gives me the history of what is line 17 after the {commitHash}. What I want is the changelog of what used to be Line 17.

I´ve looked at the file and saw that after the commit Line 17 is now Line 20-22. So I tried git log {commitHash} -p -1 -L 20,+3:{filePath} :

commit {commitHash} 
Author: {author}
Date:   {date}

   {commitMessage}

diff --git a/{filePath} b/{filePath}
--- a/{filePath}
+++ b/{filePath}
@@ -17,1 +20,3 @@
-   <button type="button" class="btn btn-info" tooltip="someTooltip" placement="bottom" disabled>
+   <button type="button" class="btn btn-info"
+    tooltip="someTooltip"
+    placement="bottom" disabled>

What I want is basicly a command where I provide Line 17,+1 but get the result of the git log {commitHash} -p -1 -L 20,+3:{filePath} command.

Related Question: Retrieve the commit log for a specific line in a file?

LeagueCode
  • 33
  • 3
  • Can you explain why it is so important that you use the "old" line number? Also, what do you do with the output of this command? Is it for human viewing, or is it for some other purpose? – dss539 May 14 '19 at 15:14
  • The information should be minded automaticly and will be proccessed automaticly later. I will provide more information and an example in a git repo – LeagueCode May 17 '19 at 09:54
  • Here is the git repo with an example of the problem: https://github.com/LeagueCode/old-line-changes – LeagueCode May 21 '19 at 09:29
  • 1
    Ah I see. The example helps make it clear. I am guessing you have static code analysis that complains about line X and you want to see how someone fixed it, but using automated tooling to report the new line. – dss539 May 21 '19 at 14:42
  • Yes, this is correct. – LeagueCode May 21 '19 at 14:44

1 Answers1

1

It appears that you want to grab hunks from a git diff output based on the base line number. Here is some ugly perl that can do that.

git diff -U0 d462a1f6 e627666c | perl -e 'while(my $line = <>){ if($line =~ /^@@ -14/) { while($line = <>) { if($line =~ /^@@/) {exit} print $line} } }'

This works ok for me using git bash on Windows.

You would of course want to replace the "14" in the example with the desired line number.

You can also filter the output to show only added lines with a small change like

print $line if $line =~/\+/
dss539
  • 6,804
  • 2
  • 34
  • 64
  • So I get the line I'm looking for when I use: git log "{commitHash}^" -p -1 -L 17,+1:{filePath}. The command also shows how this line changed in the commits before "{commitHash}^". The only problem is that I don´t get the information about how it changed in the next commit ({commitHash}). So what I would like tho know is what happened to the given Line after "{commitHash}^" – LeagueCode May 14 '19 at 14:09
  • If you want to know what happens to the line after some commit, you have to request it from git in the way git understands. When you ask git about commit X and the line is number 20 in commit X, you have to ask it about line 20, not line 17. It seems you need an extra step where you take the line number you like to use (17) and then convert it to the line number git requires (20) – dss539 May 14 '19 at 15:06
  • Try the two command approach in my edit. I think it's going to take a creative approach to solve your need, so it would be helpful to have more details about why you are doing this. If the 2 commands don't work for you, maybe take the full output from `git log -p` and use `sed` to slice up the result so you only see the desired lines. There are probably other ways to approach this if you can share more info about your use case. – dss539 May 14 '19 at 15:23