2

Question: In git, how does see the difference between the current state of a file and the most recent commit before a specific timestamp (i.e.: yesterday at 2pm)? I'm specifically interested in getting the total changed words (similar to the total changed lines lines +/-).

Use case. The use case I'm after is being able to track how much writing progress I've made relative to a date. In writing, the unit of progress is typically "words changed" as opposed to "lines changed" (other posts on StackOverflow cover lines changed, but not words changed).

Example. So for example, if I've added 200 words to file.txt and deleted 100 words since yesterday at 2pm, I'd like the result of this command to be something like "+200/-100".

George
  • 6,927
  • 4
  • 34
  • 67
  • 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) – phd May 11 '18 at 19:31
  • @phd: That post discusses getting "number of lines changed"; my post discusses "total changed words". – George May 12 '18 at 08:06

1 Answers1

1

Git doesn't track words, only lines. You can still get the difference in the number of words in the file between two commits, with a bit of work, but there's no easy way (as far as I know) to get the number of words added and removed.

If you want to compare the number of words in a file as it was yesterday and as it is now, you can use the following Bash script (pass the filename as the first argument):

if [[ -z "$1" ]]; then
    echo "pass a file to compare"
    exit 1
fi
orig_branch="$(git symbolic-ref --short -q HEAD)"
if [[ -z "$orig_branch" ]]; then  # detached HEAD
    orig_branch="$(git rev-parse HEAD)"
fi
words_now=$(wc -w "$1" | cut -d' ' -f1)
git checkout '@{yesterday}'
words_then=$(wc -w "$1" | cut -d' ' -f1)
printf '%+d\n' $((words_now - words_then))  # prints something like "+10" or "-42"
git checkout "$orig_branch"

NB: if your working directory is dirty when you run this, you might have problems. It's probably a good idea to git stash save your changes beforehand, and git stash pop them afterwards.

To get an added/removed count like Git will give you for lines, I guess you could probably split the file you're comparing into one word per line, then diff the two files.

ash
  • 5,139
  • 2
  • 27
  • 39
  • `wc -w` spits out a number and a filename, which is causing this script to error. Do you know to fix this? Also: I'm assuing you should checkout your previous branch at the end of this script. – George May 12 '18 at 15:08
  • oops, that's what I get for not testing my answers before I post them :P I've updated the answer to fix that. Checking out the previous branch afterwards is harder than it sounds, but I've added that too. – ash May 12 '18 at 15:19