Filter git log to show only commits with notes
Asked Answered
W

4

8

How can I dump a log of my commits showing only the ones with notes of a given namespace?

Commits without notes, or notes not belonging to a given namespace should be filtered out

In the text dump I do not want just the note, also the commit info.

I have played with: git show refs/notes/<namespace> and I believe the solution might be there rather than with "git log". However I am still having some problems to find the right command showing also all commits.

Weidner answered 29/10, 2012 at 8:29 Comment(1)
have you tried something with git notes list?Margalit
D
9

git notes will give you the id of each note and what object it applies to. So the second column is what you want.

$ git notes
f5ac8874676de3029ffc8c31935644ff7c4deae0 07ca160c58cf259fe8bb5e87b9d9a7cbf8845f87
62ecfc95355587d6d1f779fcaca6e4f53d088ccc eb6c60b9dcb56219d9d882759c0bf928f6d6c3fa

Grab that last column using cut and pass them into git show.

[ "$(git notes)" = "" ] || git notes \
| cut -d' ' -f2 \
| xargs git show

To pick a specific namespace, add a --ref=namespace to git notes.

[ "$(git notes --ref=namespace)" = "" ] || git notes --ref=namespace \
| cut -d' ' -f2 
| xargs git show

The initial test, [...], prevents a slight problem: git show will show the current checkout if passed no arguments. So if there's no notes you're going to get misleading output. With the initial test that's not a problem: if the test fails, i.e., if there's no notes, then git show won't be called.

Duwe answered 29/10, 2012 at 8:40 Comment(1)
The -r flag (or --no-run-if-empty) will fix the "slight problem" in case of GNU xargs.Marcellamarcelle
S
1

You can add a test to Schwern answer:

[ "$(git notes)" = "" ] || git notes \
| cut -d' ' -f2 \
| xargs git show

or to pick up a particular namespace:

[ "$(git notes --ref=namespace)" = "" ] || git notes --ref=namespace \
| cut -d' ' -f2 \
| xargs git show

Then, if the initial test [...] fails, i.e., if there's no notes, git show won't be called.

Shanonshanta answered 25/10, 2016 at 15:29 Comment(0)
C
1

If you want a git log-style report of only the commits with attached notes, then you can use the --no-walk argument to tell git log to limit the report the only the commits listed on the command line. e.g.,

git notes list \
| cut -d' ' -f2 \
| xargs --no-run-if-empty git log --no-walk=sorted

I added --no-walk=sorted to order the output by commit time, rather than the arbitrary order output by git notes.

You can of course add any of the other git log options to format the output as you prefer.

Crenation answered 21/5, 2021 at 17:20 Comment(0)
C
0

I wanted a single git-log(1) invocation. Thanks to Tino for the neat initial test. [1]

ref=commits
[ "$(git notes --ref="$ref" | head -1)" ] \
    && git notes --ref="$ref" list \
    | cut -d' ' -f 2 \
    | git log --no-walk=sorted --ignore-missing --stdin

--no-walk so that ancestors aren’t walked/searched. --stdin allows the commits to be read from standard in. It seems necessary for this option to be last. sorted is probably what you want (or doesn’t hurt).

--ignore-missing might be needed here in case you have gotten the notes from some other repository. They might have made notes on commits that you are missing. If this isn’t given and a non-existing object is hit:

fatal: bad object 00096aa60de8dbcea2109f671f67fdc48428ffec

Notes

  1. A different problem than git-show(1): git log will default to HEAD if there happens to be no input (no commits).
Catlaina answered 19/9, 2024 at 18:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.