-2

Executing the following gives me the differences in line count between local changes and the remote versions of the file:

git diff --shortstat C:\dev\Sprint7 origin/master:C:\dev\Sprint7

Example result:

18 files changed, 11 insertions(+), 8 deletions(-)

Please, how do I calculate the total number of unique changed lines?

For instance, if line 5 of Login.jsp file is deleted and same line 5 is replace by another line (or string) (i.e. deletion and insertion is done on line 5), I want this to be counted as 1 changed line.

Can I get that count from the command above by summing the insertions and deletions?

Any help will be appreciated.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Mega
  • 1,304
  • 5
  • 22
  • 37
  • No you can't. You could remove line 5 and add line 8 and they could be totally unrelated. – Daniel Hilgarth Mar 26 '18 at 12:59
  • Possible duplicate of [How can I calculate the number of lines changed between two commits in git?](https://stackoverflow.com/questions/2528111/how-can-i-calculate-the-number-of-lines-changed-between-two-commits-in-git) – amin saffar Mar 26 '18 at 13:00
  • Daniel, I appreciate your comment, but you were stating the obvious! – Mega Apr 15 '18 at 10:52

1 Answers1

1

Here I am pasting a small shell script. Simply give the git diff command as input and rest will take care by the script itself.

#echo "Input Command : $1"
TOTAL_CHANGES_WITH_FILES="$($1 | grep '^+' | wc -l)"
TOTAL_CHANGED_FILES="$($1 | grep '^+++' | wc -l)"
RESULT=`expr ${TOTAL_CHANGES_WITH_FILES} - ${TOTAL_CHANGED_FILES}`
#echo "Total Changed Lines: ${RESULT}"
echo "${RESULT}"

save this code to file changed_lines.sh and run this file using command sh changed_lines.sh "git diff HEAD". Replace "git diff HEAD" with the git diff C:\dev\Sprint7 origin/master:C:\dev\Sprint7 as per your config. This will simply print the no. of lines that changed. If anything wrong. please feel free to comment. Hope this helps.

SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33
  • Thanks SV, I love the bash solution, but it is giving no of lines changes = insertions e.g. git diff --shortstat C:\dev\Sprint7 origin/master:C:\dev\Sprint7 gives 2 files changed, 48 insertions(+), 45 deletions(-). Your solution displays 48. Does that mean deletions is not taken into consideration? Also, how do I enhance the script to give the lines changed per file type or extenstion? – Mega Apr 12 '18 at 16:49
  • But mentioned in your question that you want only the `unique changed lines`. `For instance, if line 5 of Login.jsp file is deleted and same line 5 is replace by another line (or string) (i.e. deletion and insertion is done on line 5), I want this to be counted as 1 changed line.` So that's why my solution is like that. – SV Madhava Reddy Apr 13 '18 at 07:00
  • So you want me to count the removed lines also? Exception case is if the same line is edited (+, - consecutively represents 1), I have to count it as one. Is that what you are saying? – SV Madhava Reddy Apr 13 '18 at 08:44
  • Goodday SV and thanks. A changed line inc delete, insert and updated.I v 2 usecases: (1). determine no of changed lines in a remote repo per file type. e.g. JSP,JS,Java,XML,XLSM. No local vs remote diff required, just remote git query. (2) no of changes per file type, ur solution is close, but we need to specify a root folder and file type for recursive git query. ATM, My workaround is e.g. for JSP > git diff --shortstat C:\[path-to-jsp] origin/master:C:\[path-to-jsp] . Which means having multiple hard coded path to known folders.Shell solution is good, but i'm happy w git cmds too. Thanks SV. – Mega Apr 15 '18 at 09:19
  • I am trying to apply the following to the script but no joy yet: shell globbing syntax (https://stackoverflow.com/questions/221921/use-grep-exclude-include-syntax-to-not-grep-through-certain-files) : grep pattern -r --include=\*.{jsp,java} rootdir – Mega Apr 15 '18 at 10:08