0

I'm developing a git-hook that runs on pre-push. It's purpose is to prevent you from pushing the branch, if there are un-pushed git notes. In order to achieve this, I need to be able to run some logic during the pre-push hook that can detect if there are unpushed refs/notes/commits.

Unfortunately - I am terrible at git.

I tried this approach here: Check if local git repo is ahead/behind remote However when I do git merge-base refs/notes/commits origin/refs/notes/commits git just tells me:

fatal: Not a valid object name origin/refs/notes/commits

Here is my refspec:

[remote "origin"]
    url = https://github.com/dazinator/GitHookExperiment.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    fetch = +refs/notes/*:refs/notes/*

I have added some notes locally with git notes add so my local repo - I know it has notes that haven't been pushed. I just don't know what git commands to run to detect that fact whilst I am on another branch (the branch currently being pushed).

I am seeking some git commands that I can run to yield an output that I can parse to give me a definitive Yes or No that the notes branch is behind or not..

Darrell
  • 1,905
  • 23
  • 31

1 Answers1

0

This line:

fetch = +refs/notes/*:refs/notes/*

tells your Git: Take their refs/notes/* names and copy them to my refs/notes/* names. You never make any origin/refs/notes/commits name at all, you just copy the hash ID in their refs/notes/commits to your own refs/notes/commits.

This means that if you add your own commit notes, the next git fetch wipes them out, because it takes their latest notes, throwing away (force-updating) your own refs/notes/commits.

If you want to maintain your own notes, you'll need to put their notes in a different place or store your notes in a different place (either one). The git notes system isn't terribly good at all this—it's not really user-friendly at all here.

I'd suggest storing your notes under your own name, and using the standard name for their notes. I think you can do this with:

git config core.notesRef refs/notes/mynotes

for instance. Make sure you don't overwrite this notes ref using:

fetch = +refs/notes/commits:refs/notes/commits

so that you only overwrite your own refs/notes/commits from their refs/notes/commits.

You can now tell whether your notes and their notes don't match using:

git rev-parse refs/notes/commits
git rev-parse refs/notes/mynotes

These two commands will produce two hash IDs. If the hash IDs don't match, you have different commits as the tipmost notes commit.

(Note that, as the documentation shows, you can temporarily override the notes name in multiple ways, whether or not you've overridden the default-default refs/notes/commits name using core.notesRef as above. The term "default-default" here means that refs/notes/commits is the default if core.notesRef is not set. Setting core.notesRef sets a new default.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thank you. I realised the refspec was an issue when fetching it was forcing overwrite of my notes like you mentioned! – Darrell May 08 '20 at 07:10
  • Some additional info: Hoping I can use ordinary git merge commands to contribute new mynotes to the remote notes like a normal branch? - will try that out (found that some git commands that work with branches ignore notes because its under refs/notes not refs/heads). I think the rev parse stuff will be doing a local comparison, so perhaps won't definitively tell me if I have notes not pushed up to the server. Should be able to do some git fetch or push with -dry-run arg perhaps to check whether my remote is up to date before I do the local comparison. Thanks – Darrell May 08 '20 at 07:52
  • Once you're using separate ref names for your local notes and your copy of their local notes (however you arrange this), always run `git fetch` first and then do the local rev-parse based comparison. As for merging notes, I think this gets ugly fast because of the way notes are stored. `git notes` has its own `merge` sub-command to make things easier. I haven't actually used it in practice to see how well it works. – torek May 08 '20 at 15:23