0

I want to add version information to a file that is compiled into my executable. Based on here and here, I've used git describe to update the build process to add a version number and I can detect if there are uncommitted sources.

What I want to add is an indication if the current branch is out of sync with the upstream repository. That is, it's great that the developer has committed to their local repo, but a build from sources that haven't been pushed should be marked as such.

Basically, I want the equivalent to --dirty with respect to the remote.

jwm
  • 1,504
  • 1
  • 14
  • 29

1 Answers1

1

Add a git fetch and test whether git rev-parse HEAD HEAD@{upstream} spits the same word twice:

git fetch
read mine theirs <<<`git rev-parse HEAD HEAD@{upstream}`
test $mine = $theirs || echo 'not synced'
jthill
  • 55,082
  • 5
  • 77
  • 137
  • This is helpful. Is there a way to figure out if we are ahead or behind upstream? – jwm Jan 29 '20 at 21:25
  • Yes, use `git merge-base --is-ancestor HEAD HEAD@{upstream}`, nonzero return code says you're behind, so if they're not equal and you're not behind, you're ahead or you're a cousin. – jthill Jan 30 '20 at 01:02
  • final question: is the `git fetch` just pedantic goodness, or is it essential? I'm thinking possibly the latter, since without it you might not know if upstream has changed, but I just want to be sure. – jwm Jan 30 '20 at 01:04
  • That's it: the fetch updates the local repository's tracking refs from the remote's branch tips (as with all things Git, you can configure the mapping and selection, the defaults are almost always what you want) and gets any new content. – jthill Jan 30 '20 at 03:39