Following command outputs following lines of text on console
git log --pretty=format:"%h;%ai;%s" --shortstat
ed6e0ab;2014-01-07 16:32:39 +0530;Foo
3 files changed, 14 insertions(+), 13 deletions(-)
cdfbb10;2014-01-07 14:59:48 +0530;Bar
1 file changed, 21 insertions(+)
5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz
772b277;2014-01-06 17:09:42 +0530;Qux
7 files changed, 72 insertions(+), 7 deletions(-)
I'm interested in having above format to be displayed like this
ed6e0ab;2014-01-07 16:32:39 +0530;Foo;3;14;13
cdfbb10;2014-01-07 14:59:48 +0530;Bar;1;21;0
5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz;0;0;0
772b277;2014-01-06 17:09:42 +0530;Qux;7;72;7
This will be consumed in some report which can parse semicolon separated values.
The thing is the text "\n 3 files changed, 14 insertions(+), 13 deletions(-)"
(new line included) gets converted to 3;14;13
(without new line)
One possible corner case is text like "5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz"
which doesn't have such line. In that case I want ;0;0;0
Overall the goal is to analyze file change stats over a period of time. I read the git log documentation but couldn't find any format which will help me to render in this format. The best I came up was the above command mentioned.
So any command or shell script which can generate the expected format would be of great help.
Thanks!
This is one approach with
awk
.For the given input it returns:
Still not working for lines like
5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz
that do not have a3 files changed, 14 insertions(+), 13 deletions(-)
after it.This is, unfortunately, impossible to achieve using only
git log
. One has to use other scripts to compensate for something most people aren't aware of: some commits don't have stats, even if they are not merges.I have been working on a project that converts
git log
toJSON
and to get it done I had to do what you need: get each commit, with stats, in one line. The project is called Gitlogg and you're welcome to tweak it to your needs: https://github.com/dreamyguy/gitloggBelow is the relevant part of Gitlogg, that will get you close to what you'd like:
Note that I've used the tab character (
\t
) to separate fields as;
could have been used on the commit message.Another important part of this script is that each line must begin with an unique string (in this case it's commits). That's because our script needs to know where the line begins. In fact, whatever comes after the
git log
command is there to compensate for the fact that some commits might not have stats.But it strikes me that what you want to achieve is to have commits neatly outputted in a format you can reliably consume. Gitlogg is perfect for that! Some of its features are:
git log
of multiple repositories into oneJSON
file.repository
key/value.files changed
,insertions
anddeletions
keys/values.impact
key/value, which represents the cumulative changes for the commit (insertions
-deletions
)."
by converting them to single quotes'
on all values that allow or are created by user input, likesubject
.pretty=format:
placeholders are available.JSON
by commenting out/uncommenting the available ones.Success, the JSON was parsed and saved.
Error 001: path to repositories does not exist.
Error 002: path to repositories exists, but is empty.
I put something like this in my
~/.bashrc
:Where
git-lgs
's argument is the filename for which you want to display the log.git doesn't support stat info with plain --format, which is shame :( but it's easy to script it away, here's my quick and dirty solution, should be quite readable:
I'm sure, that it can be scripted better, but hey - it's both quick AND dirty ;)
usage:
Please note, that format, that you specified is not bulletproof. Semicolon can appear in commit summary, which will break number of fields in such line - you can either move summary to end of line or escape it somehow - how do you want to do it?
Following up @user2461539 to parse it into columns. Works with more complex cols like "Subject" too. Hack away to choose your own suitable delimiters. Currently need to cut subject line as it'll truncate other columns when it overflows.
This will show something like this: