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:
- 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
- Use
--notes
instead of --show-notes
since the latter is deprecated
- 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.