If you can send the output of git log
to a file, then you're halfway there already. I'm not sure what language you're most comfortable with, but I know it's easy to do in a bash script.
Calculate time between git commits
-
23-06-2023 - |
Question
I would like to calculate the time between commits in git, this (I hope) will give me a rough measure of my productivity. Is this possible?
Solution 3
OTHER TIPS
This will print the date of the newer commit followed by the date of the older commit:
git log HEAD~[newer_commit] -n 1 --format=%ad; \
git log HEAD~[older_commit] -n 1 --format=%ad
You'll end up with date strings. Use whatever language you like to calculate the difference.
Here is a more complete example of using bash
and date
for doing this.
First, as noted in Git: want to iterate through all commits on branch, and list files in each commit :
git rev-list will list all revisions reachable from a given commit in reverse chronological order, so you can pass it a branch name to get the list for that branch head backwards
So, basically, the idea is that I use rev-list
to iterate from HEAD backwards through the repo history, and at each revision, get the timestamp of current, and the timestamp of previous commit; get them as UNIX timestamps because then we can do integer arithmetic (difference) directly in bash
; and finally use date
to display the dates. So this bash
script would be:
for ix in `git rev-list master`; do
thists=`git log $ix -n 1 --format=%ct`;
prevts=`git log $ix~1 -n 1 --format=%ct 2>/dev/null`;
if [ ! -z "$prevts" ] ; then
delta=$(( $thists - $prevts ));
echo `date -d @$thists +'%Y-%m-%d %H:%M:%S'` "-->" \
`date -d @$prevts +'%Y-%m-%d %H:%M:%S'` " ;= " \
`date -d @$delta +'%H:%M:%S'`;
fi;
done
Note that when you're at the first commit, there is no previous/parent commit, and so git log
will fail with: fatal: ambiguous argument '45e95c7...~1': unknown revision or path not in the working tree.
; to avoid printing this message, I redirect the stderr
to /dev/null
in the prevts
command, and then do the time delta calculation only if prevts
is not empty. The output from this script is something like this:
2017-08-27 09:44:04 --> 2017-08-26 17:12:22 ;= 17:31:42
2017-08-26 17:12:22 --> 2017-08-26 16:48:24 ;= 01:23:58
2017-08-26 16:48:24 --> 2017-08-26 16:28:05 ;= 01:20:19
...
However, these deltas are one hour off - so for more correct display of the time delta, I used Python's datetime.timedelta
-- so in the above script, you may want to replace the `date -d @$delta +'%H:%M:%S'`;
line with:
`python -c "import datetime as dt; d = dt.timedelta(seconds=$delta); print(d)"`
... in which case this log is generated:
2017-08-27 09:44:04 --> 2017-08-26 17:12:22 ;= 16:31:42
2017-08-26 17:12:22 --> 2017-08-26 16:48:24 ;= 0:23:58
2017-08-26 16:48:24 --> 2017-08-26 16:28:05 ;= 0:20:19
...