Reputation: 7317
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".
Upvotes: 2
Views: 103
Reputation: 5539
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.
Upvotes: 1