0

The following bash script can compute the LOC change in a git repo in the previous 24 hours:

loc_changed() { since=${1:-24 hours} author=${2:[email protected]} git log --numstat --pretty="%H" --author="$author" --since="$since" | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("(+%d, -%d) LOC\n", plus, minus)}' } 

Problem: This command excludes unstaged LOC changes. I think it would be useful to add unstaged changes to this LOC count. How does one do this?

Basically, what I am trying to achieve is this: given a repo, how much changed in the past 24 hours (irrespective of whether those changes have been staged or even committed yet)?

2
  • It also excludes staged changes, as git log looks only at commits. Neither the index contents, nor the work-tree contents, are commits. You can use some of the git diff family of commands that compare from or to the index and/or from/to the work-tree (you'll need one command for HEAD-to-index, then one for index-to-work-tree, for instance, to get staged and unstaged), or else make two commits out of index and work-tree. Or, you can make commits out of these. However, if the index contains an in-progress merge, it's not possible to commit it and overly difficult to diff against it. Commented Feb 7, 2019 at 4:48
  • It's up to you to decide what to do and how to do it, here; Git merely provides some tools. To diff HEAD vs work-tree, bypassing the index entirely, consider using the confusingly-named git diff-index plumbing command. Commented Feb 7, 2019 at 4:52

1 Answer 1

1

Try with these changes. I'm sure this code can be optimized :)

loc_changed() { since=${1:-24 hours} author=${2:[email protected]} stagedPlus=$(git log --numstat --pretty="%H" --author="$author" --since="$since" | awk 'NF==3 {plus+=$1} END {print plus}') unstagedPlus=$(git diff $(git log -1 --pretty="%H" | tail -1) --numstat | awk 'NF==3 {plus+=$1} END {print plus}') stagedMinus=$(git log --numstat --pretty="%H" --author="$author" --since="$since" | awk 'NF==3 {minus+=$2} END {print minus}') unstagedMinus=$(git diff $(git log -1 --pretty="%H" | tail -1) --numstat | awk 'NF==3 {minus+=$2} END {print minus}') if [[ -z $unstagedPlus ]]; then unstagedPlus=0 fi if [[ -z $unstagedMinus ]]; then unstagedMinus=0 fi print "+$(($stagedPlus+$unstagedPlus)), -$(($stagedMinus+$unstagedMinus))" } 

The lines you need are related to diff, where I get all changes (counted with numstat) from the last commit to the current unstaged code.

Sign up to request clarification or add additional context in comments.

2 Comments

This is brilliant. Is there an easy way to change the units displayed by this command from LOC to +words added, -words removed?
I'm not sure, but maybe playing with the diff tool you can count the words added/removed, but in terms of code, a single change of a + by a - could be counted as a word, so the solution would be so tricky.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.