So since GitHub removed the insights tab for non-premium accounts, I'm trying to locally list insertions and deletions to my git repository per day.
I figured out this way of printing what I want:
git log --pretty="@%ad" --date=short --shortstat | tr "\n" " " | tr "@" "\n"
That produces this kind of output:
2024-06-13 7 files changed, 400 insertions(+), 406 deletions(-)
2024-06-12 3 files changed, 145 insertions(+)
2024-06-12 5 files changed, 638 deletions(-)
2024-06-12 1 file changed, 1 insertion(+), 1 deletion(-)
Notice the plurals in file(s), insertion(s) and deletion(s). Another problem is that a commit might not have insertions or deletions (or both, but let's ignore this case).
So I'm almost there, I just need to extract the date, insertions and deletions and group by date. That will produce some sort of "work done per day" graph.
I made this regex to capture the fields dealing with all the optionals:
/^([0-9]{4}-[0-9]{2}-[0-9]{2})\s{3}[0-9]+\sfile(s)?\schanged,\s(([0-9]+)\sinsertion(s)?\(\+\))?(,\s)?(([0-9]+)\sdeletion(s)?\(\-\))?\s$/gm
Now I need to get the 1st, 4th and 8th groups, for instance with sed
:
echo "2024-06-13 7 files changed, 400 insertions(+), 406 deletions(-) " |
sed -r 's/^([0-9]{4}-[0-9]{2}-[0-9]{2})\s{3}[0-9]+\sfile(s)?\schanged,\s(([0-9]+)\sinsertion(s)?\(\+\))?(,\s)?(([0-9]+)\sdeletion(s)?\(\-\))?\s$/\1 \4 \8/gm'
That produces the correct output:
2024-06-13 400 406
But if the input string doesn't have insertions or deletions, sed just prints nothing for that captured group. Eg.:
2024-06-13 400
And I have no way to tell if the single number are insertions or deletions.
Is there anyway to extract the groups from each line, but print a "0" as placeholder if the group doesn't exist? (not necessarily with sed alone, and not necessarily in a single command).
-r
is the option in old versions of GNU sed only to enable EREs. In current versions of GNU sed, BSD sed, and in the latest POSIX standard forsed
the option to enable EREs is-E
, same as forgrep
, not `-r.tr "\n" " "
turns your data into something that's no longer a valid text file per POSIX so, in general, don't do that. If you post the output fromgit log --pretty="@%ad" --date=short --shortstat
and the final output you want given that input then we can help you.--pretty=%ad --date=short
is the same as--pretty=%as
, so just use the latter.