Search code examples
gitgit-log

Git get --source information in --format


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,kevin.j.jalbert@gmail.com,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 <kevin.j.jalbert@gmail.com>
Date:   Fri Sep 21 17:02:33 2012 -0400

    commit message

commit f1e1b8d11defc48839557db5e54a5a6f7ffe6cad refs/heads/issue_5
Author: Kevin Jalbert <kevin.j.jalbert@gmail.com>
Date:   Thu Sep 13 15:34:36 2012 -0400

    commit message

commit d7acdbd957d9b477f8849fd5a37882cdd78d8e1f refs/tags/v0.3.0
Author: Kevin Jalbert <kevin.j.jalbert@gmail.com>
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,kevin.j.jalbert@gmail.com,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?


Solution

  • 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,johannes.schindelin@gmx.de,Wed Dec 4 23:10:12 2019 +0100,67af91c47a,origin/maint
    67af91c47a6672b99e1f742123415f96cbafd19a,Johannes Schindelin,johannes.schindelin@gmx.de,Wed Dec 4 23:09:11 2019 +0100,da72936f54 a7312d1a28,origin/maint
    a7312d1a28ff3ab0a5a5427b35f01d943103cba8,Johannes Schindelin,johannes.schindelin@gmx.de,Wed Dec 4 23:07:46 2019 +0100,7fd9fd94fb,origin/maint