1

Intention: Due to a heap of legacy code, I'd like to lint only lines added or changed in my Pull Requests to make the environment gradually better.

Input: Name of the base branch (master), name of my PR branch (e.g. honzajavorek/my-cool-feature), hash of the last commit in the feature branch (e.g. 53253a3e8d9b1e3ed7d45b91e045c59d50aefdf0).

Output: Affected (added or changed, obviously not deleted) line numbers for each file, so I could filter linter output to contain only those.

I'm not looking for a one liner, I'm okay with writing a short bash/Python/node.js script to do this, but only with reasonable complexity (several lines).


Update: Just found Git diff with line numbers (Git log with line numbers). Doesn't really seem to be an easy task :(

Community
  • 1
  • 1
Honza Javorek
  • 8,566
  • 8
  • 47
  • 66

2 Answers2

1

I was able to come up with a naive code in bash that would produce what you want.

The naivity here lies in the fact that that this requires clean (linear) history between master an feature branch, which is not always the case. In fact, you should use the hash of commit that was master once this branch got diverted instead of master in this example, or rebase the branch before you try to do this.

Also, it was only tested with clean working directory, the feature branch checkouted.

for COMMIT in $(git log --pretty=format:%h master...feature); do
  for FILE in $(git ls-tree -r feature --name-only); do
    for NUMBER in $(git blame --line-porcelain "$FILE" | egrep ^$COMMIT | cut -d' ' -f3); do
      echo "$FILE":$NUMBER
    done
  done
done

First loop walks all commits in between master and the feature branch.

The second loop walks all tracked files in the feature branch.

And the third (most inner) loop finds all lines that are blamed by the commit hash provided in that context.

In the end, you should get a following output:

points.yml:32
points.yml:33
points.yml:34
points.yml:37
site.py:12

The format is obvious.

hroncok
  • 1,147
  • 8
  • 12
  • 1
    I think that it loops too much - the most outward loop can be split apart and generate one regexp for egrep – MacHala Sep 14 '16 at 17:22
  • You should be able to combine `git diff --name-only master..feature` and then `git blame -b master..feature` in a similar way for a more robust solution. – Petr Viktorin Sep 14 '16 at 21:09
0

Try https://unix.stackexchange.com/questions/34874/diff-output-line-numbers for one possible way of getting a suitable diff output, and then then make that part of a custom Git diff driver, so you can do all your tidy-ups?

Community
  • 1
  • 1
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71