Git: Get all notes of a branch

3k views Asked by At

We'd like to annotate our commits with git notes add, which works fine. To get a list of all commits with notes we use this command

git notes | cut -d' ' -f2 | xargs -ihash git log hash -1

Now we are looking for the notes of a branch only. I'm at a loss here since the notes don't know anything about branches. May be there's a way to start with git log and ask git notes wheter there's a note for the commit. But I'm nut sure wheter this is to slow for big repositories.

Any ideas?

2

There are 2 answers

0
twalberg On

I would start with this:

git log --pretty=format:"%H %N" --show-notes <branch>

See git help log for additional things you can put in the format string to fix up the output the way you want it...

0
Craig Ringer On

finding notes

Show all abbreviated commits with notes for all commits on master that have notes:

git notes |\ 
  awk '{if (!system("git merge-base --is-ancestor "$2" master")) {print $2}}' |\
  xargs git show --abbrev-commit --oneline -s --notes

How does it work?

git notes lists a mapping of note blob to target commit:

$ git notes 
579c5b129af5662ca2ae6d08977fd5acfe4ca383 3c4f076572612726808d686d04acb2266401c2f9
63a79274ee3486213d58eef8a5a6f604f7c1fdb1 6546d605b21bb92f270af61f3cc00ff2978001e2

so you can filter the commits in your stream by testing each for branch membership.

But the cost of all those individual commands will really add up if you have a lot of notes.

Per-branch notes

In future what you might want to do here is use different notes refs for different branches. I don't see a way to bind the core.notesRef configuration option to the current branch though, so you'd have to

git notes --ref "my-branch-name" --edit

etc. But you could probably

alias gn git notes --ref "$(git rev-parse --abbrev-ref HEAD)"

to deal with that.

This won't sort your current notes by branch, but it'll help in future.

You'll need to configure the refs/notes/branchname" for each branchname to be fetched and pushed if you want to share them.