Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git get --source information in --format

Tags:

git

git-log

I am trying to format my git logs in a very specific format.

I was originally using git log --format="%H,%an,%ae,%ad,%p" which would produce the following output (for each commit):

b05f827b41856e6f4bcfba20c32f58434ce3a5a6,Kevin Jalbert,[email protected],Fri Sep 7 14:43:16 2012 -0400,206f23d

Now I am trying to get the ref (i.e., tag/branch) information for each commit. I am able to view this information using the git log --source command and this shows exactly what I want (i.e., the ref which is located after the commit SHA):

commit 84deec66f94085ee3a0e6f6204f06296d7a1a903 refs/remotes/origin/HEAD
Author: Kevin Jalbert <[email protected]>
Date:   Fri Sep 21 17:02:33 2012 -0400

    commit message

commit f1e1b8d11defc48839557db5e54a5a6f7ffe6cad refs/heads/issue_5
Author: Kevin Jalbert <[email protected]>
Date:   Thu Sep 13 15:34:36 2012 -0400

    commit message

commit d7acdbd957d9b477f8849fd5a37882cdd78d8e1f refs/tags/v0.3.0
Author: Kevin Jalbert <[email protected]>
Date:   Wed Sep 12 16:48:46 2012 -0400

    commit message

What I am trying to do is to include this information at the end of my original --format="..." command so I would have output that looks like:

 b05f827b41856e6f4bcfba20c32f58434ce3a5a6,Kevin Jalbert,[email protected],Fri Sep 7 14:43:16 2012 -0400,206f23d,refs/remotes/origin/HEAD

I cannot seem to find any format placeholder that references the branch/tag/ref of a commit based on the --source flag. Am I just missing the correct placeholder? Or is there an alternative way to format/display the ref along with the custom information I desire?

like image 519
Kevin Jalbert Avatar asked Nov 03 '22 14:11

Kevin Jalbert


2 Answers

With Git 2.21 (Q1 2019), the custom userformat "log --format" learned %S atom that stands for the tip the traversal reached the commit from, i.e. --source.
I mentioned it at the time in "Git log placeholder for branch".

See commit ad6f028 (11 Jan 2019) by Issac Trotts (ijt).
(Merged by Junio C Hamano -- gitster -- in commit a562a11, 29 Jan 2019)

log: add %S option (like --source) to log --format

Signed-off-by: Issac Trotts

Make it possible to write for example

git log --format="%H,%S"

where the %S at the end is a new placeholder that prints out the ref (tag/branch) for each commit.

Using %d might seem like an alternative but it only shows the ref for the last commit in the branch.

Example:

C:\Users\VonC\git\git>git log --format="%H,%S" origin/maint
53a06cf39b756eddfe4a2a34da93e3d04eb7b728,origin/maint
67af91c47a6672b99e1f742123415f96cbafd19a,origin/maint
a7312d1a28ff3ab0a5a5427b35f01d943103cba8,origin/maint

And, using your format (with an additional %S) --format="%H,%an,%ae,%ad,%p,%S":

C:\Users\VonC\git\git>git log --format="%H,%an,%ae,%ad,%p,%S" origin/maint
53a06cf39b756eddfe4a2a34da93e3d04eb7b728,Johannes Schindelin,[email protected],Wed Dec 4 23:10:12 2019 +0100,67af91c47a,origin/maint
67af91c47a6672b99e1f742123415f96cbafd19a,Johannes Schindelin,[email protected],Wed Dec 4 23:09:11 2019 +0100,da72936f54 a7312d1a28,origin/maint
a7312d1a28ff3ab0a5a5427b35f01d943103cba8,Johannes Schindelin,[email protected],Wed Dec 4 23:07:46 2019 +0100,7fd9fd94fb,origin/maint
like image 67
VonC Avatar answered Nov 07 '22 20:11

VonC


I was looking for this also. I wasn't able to find a flag as part of git-log but the following will work, adding a column to the end of your CSV row:

$ git log --no-color --source --oneline --all | while read sha1 srcref subject
> do echo "$(git log -1 --format="%H,%an,%ae,%ad,%p" $sha1),$source"
> done

The format of --oneline is %h %<our missing format character> %s when --source is provided, so that gets us part way there. Then feeding the SHA1 back into git log -1 with the rest of the format specifiers gets us the rest of the way.

like image 39
quornian Avatar answered Nov 07 '22 20:11

quornian