Find commit with specific git note

Viewed 561

I use git notes in my repository. Sometimes I need to find a commit with note that includes given string. So far I was using this command:

git log --show-notes=* --grep="PATTERN" --format=format:%H

The problem here is that this prints every commit SHA with PATTERN, even if it's not in notes only in commit message. Is there a better way for that?

2 Answers

Notes are stored in a COMMITed TREE that's "hidden" off to the side under a notes ref (refs/notes/commits by default). That means you can process them just like content.

$ git grep Testing refs/notes/commits
refs/notes/commits:fad066950ba73c309e80451d0d0f706e45adf5a8:This is a test - Testing

$ git show fad0669
commit fad066950ba73c309e80451d0d0f706e45adf5a8
Author: Mark Adelsberger <adelsbergerm@xxx>
Date:   Thu Sep 6 07:51:15 2018 -0500

    1

Notes:
    This is a test - Testing

diff --git a/file1 b/file1
index e69de29..038d718 100644
--- a/file1
+++ b/file1
@@ -0,0 +1 @@
+testing

There is a placeholder for notes in the format string, %N. I don't know how to print the notes in one line, so I use a loop to test notes of all reachable commits one by one.

Try

git log --format=%H | while read commit;do
    git log -1 $commit --format=%N | if grep -q "PATTERN";then echo $commit;fi;
done

You can change echo $commit to git log -1 --show-notes $commit.

Related