I need to get the Date and commit results of a set of github in this format:
Date Commit
19 Mar 2015 b6959eafe0df6031485509c539e22eaf2780919c
1 Apr 2015 9a1f13339cc7d43930440c2350ea3060b72c8503
1 Apr 2015 1e76036421ca4d72c371182fc494af514911b099
I am able to get the date with this command:
git log | grep Date: | awk '{print 4ドル " " 3ドル " " 6ドル}'
and the commit with
git log | grep commit | awk '{print 2ドル}'
How can I combine the commands, so as to print both results in the same line?
Here is an example of the git log:
commit 1e76036421ca4d72c371182fc494af514911b099
Author: xxxx
Date: Wed Apr 1 17:35:36 2015 +0200
First web app commit
First web app commit
commit 9a1f13339cc7d43930440c2350ea3060b72c8503
Author: xxxx
Date: Wed Apr 1 17:28:42 2015 +0200
change from app to website
commit b6959eafe0df6031485509c539e22eaf2780919c
Author: xxx
Date: Thu Mar 19 18:58:33 2015 +0100
First remote commit: hello world
scheleton of the project now available
commit a41d419de46a59e72dbc52e5f39c9d8a8a9af72a
Author: xxxx
Date: Thu Mar 19 17:31:16 2015 +0100
Initial commit
5 Answers 5
The "git log" command is powerful enough to handle this on its own. Play around with variations on this:
git log --date=iso --pretty=format:"date={%ad}, commit={%H}"
For me this outputs:
date={2014年12月12日 14:14:23 -0800}, commit={75390af8bcd42c4dde6d841dc53f66a9ca91f460}
date={2014年12月09日 18:53:32 -0800}, commit={8dcf9eb10611e6ac778e518cf37efb041c69f5b5}
date={2014年12月11日 17:17:26 -0800}, commit={c3c1120b6dda6b317e1de5c7fe4eed7c8741ea1a}
To achieve the exact format you specified (though with the date formatted a bit more reasonably), try this:
git log --date=short --pretty=format:"%ad %H"
Which outputs things like:
2015年08月18日 1d0ca5a596f02958c4ba8ff269def3f235c64495
2015年08月18日 d92fc3ebb62d2ca3e24322db2b669fdaebde7ea1
2015年08月18日 6ba3970620e19699c2969b1eed5c479b07b8908a
1 Comment
You more-or-less never need grep | awk as awk can do the matching for you.
So grep Date: | awk '{print 4ドル " " 3ドル " " 6ドル}' can be rewritten as awk '/Date:/ {print 4ドル " " 3ドル " " 6ドル}' and grep commit | awk '{print 2ドル}' can be rewritten as awk '/commit/ {print 2ドル}'.
That should be enough to give you the first inklings of how to solve your problem.
Combine the awk scripts.
git log | awk '
# Print the header line (remove if you do not want it).
BEGIN {print "Date Commit"}
/^commit/ {
# Save the current commit
commit=2ドル
# Skip to the next line
next
}
/^Date:/ {
# Print the fields from the current line and the saved commit
print 4,ドル 3,ドル 6,ドル commit
# Clear the saved commit Just in Case
commit=""
}
'
That being said you can get almost the format you want from git log itself:
$ git log --format='%ad %H' --date=short
2015年03月19日 b6959eafe0df6031485509c539e22eaf2780919c
2015年04月1日 9a1f13339cc7d43930440c2350ea3060b72c8503
2015年04月1日 1e76036421ca4d72c371182fc494af514911b099
Which you could convert to your desired output with some awk if you wanted. (Transposing the date fields is trivial, converting to the month names would require some more code/work.)
2 Comments
git checkout %H; sonar-sunner -Dsonar.projectDate=%ad or whatever.You may be find your solution with pretty format ?
Comments
How about:
git log | awk '/^commit / { commit = 2ドル }
/^Date: / { printf "%s %s %s %s\n", 4,ドル 3,ドル 6,ドル commit }'
Capture the commit information in the variable commit. When a date comes along, print the date and commit information.
It is often a bad idea to pipe the output of grep to awk; awk is perfectly capable of doing the filtering that grep does.
Comments
This would do
git log -5 --pretty=format:"%ad %H" | awk '{print 3,ドル 2,ドル 5,ドル 7ドル}'
10 Aug 2015 9fb4b1b5e7bcbc7f55e40ceae1758a7288673a51
10 Aug 2015 3d613862ca53787c97a42e8e2c37e09c8752b637
7 Aug 2015 727af2863dbe9f7b4da9189bc12f97a99aac5705
7 Aug 2015 1438b43b5755079bb3e62c819bfef2629d8336e9
7 Aug 2015 34303c19fb11482d66fc3c1102bc8d8b433af21a