-1

I want to write shell script to parse java source files and find-out the common violations in source code based on some rule. This script should run on new code which is added/modified , this shouldn't get the old code. My code repository is git.

How to get the newly added/modified lines from git. I can then execute my script on top this.

JavaUser
  • 25,542
  • 46
  • 113
  • 139
  • 1
    "newly added" compared to what? You'll need to keep track of the "what" as you go. (Maybe "what" is the the last commit ID you checked up to?) – TTT Jun 10 '21 at 13:28

1 Answers1

1

If you want to apply some parsing action on code, you are probably better off parsing whole files rather than spotting only the modified lines. On top of that, the notion of "added line" depends a lot on the heuristic implemented in diff, and will often give you results that don't match what you would expect.


I didn't get the context from your question, especially what you intend to compare :

  • to list modified files, use git diff --name-only or git diff --name-status :
# list of tracked files modified on disk since last commit :
git diff --name-status HEAD

# list of files modified between 2 commits :
git diff --name-status <commit1> <commit2>

# list of files modified in branch 'feature' since it forked from 'master' :
# (this will give you the same list as in a PR view)
git diff --name-status master...feature  # 3 dots, not a typo

# list files modified in a specific commit :
# compare it to its parent (add a '^' at the end)
git diff --name-status <commit>^ <commit>

If you need to target merge commits (which have more than one parent), git show tries to be smart and may give you better results :

git show --format="" --name-only <commit>

If you really want just the modified/added lines : just grep them from git diff

for each modified file, run something like :

# get lines starting with '+', but drop the extra '+++ the/file' line added
# by git
git diff <a> <b> -- the/file | grep '^+' | grep -v '^+++'
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    By chance did you misread "newly added/modified *lines*" as "*files*"? Also, the way I interpreted the question at first read, the ask might be a compare between two commits. Looks like you interpreted it to be against uncommitted changes, or a single commit. (I suppose if we get clarification that the ask is indeed between two commits, you could add that option later too.) – TTT Jun 10 '21 at 14:02
  • (I don't know why I feel obliged to mention this, but the DV on both Q and A wasn't me, though it may appear that it was since I commented on both...) – TTT Jun 10 '21 at 14:32
  • @TTT : no worries :) I wrote a quick answer, hoping it would serve, then went on looking for a duplicate, but I didn't find it right away and got caught into smth else – LeGEC Jun 10 '21 at 17:07
  • I think you've covered all the likely scenarios now that OP envisions. I think I agree going by the entire file would be best, but I can imagine why OP might want just the modified/added lines, if say, new rules are trying to be enforced that may not work on existing code in the same file. Nice edit. – TTT Jun 10 '21 at 20:00