7

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?

3 Answers 3

6

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 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

You could search each note and only output the corresponding commit if the search matches.

#!/usr/bin/env bash git notes list | while read -r note_and_commit_raw; do note_and_commit=($note_and_commit_raw) git cat-file -p "${note_and_commit[0]}" | grep --quiet 'search string' && git --no-pager log -1 --format=format:%H ${note_and_commit[1]} done 

Notes:

  1. Use git log -1 (only output that commit) since it seems like you don’t want the ancestors of those commits
    • This is also the reason why I invoke git log for each commit
  2. Use --notes instead of --show-notes since the latter is deprecated
  3. Use of git --no-pager might be unnecessary with such a small format

Light analysis

I use Git Notes daily for my own manually-written notes. In other words there is no program that writes my notes, so I don’t end up with a huge amount of stuff to search through.

Apparently I only have 467 notes in refs/notes/commits. The command takes 1.220s to complete (probably with everything in disk cache etc.) if I provide a search string which matches none of the commits. So this might not really scale if you have a lot of notes.

A more efficient program could probably be implemented by using what Mark pointed out in his answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.