1

Is it possible to detect if a commit creates a new bookmark or branch via hooks in .hgrc?

I've tried to see if I can find out using hg log, but it just shows on what branch/bookmark the commit has been created: http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html

There don't seem to be hooks for it: http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html

It would make sense I suppose that there isn't a hook for it, because it is also not possible to make a commit which is 'just' the creation of the branch indicating branches/bookmarks only exists when added to a specific commit.

I figured I could check hg branches and hg bookmarks before and after each commit and determine which are removed and added, but is there a cleaner way for detecting branch/bookmark adds/removes?

Richard Deurwaarder
  • 2,023
  • 1
  • 26
  • 40

2 Answers2

2

The pushkey and prepushkey hooks can detect the addition, deletion, and moves of bookmarks.

In hgrc:

[hooks]
prepushkey=echo %HG_NAMESPACE% %HG_KEY% %HG_OLD% %HG_NEW%\n >> out.txt

HG_NAMESPACE will contain "bookmark" and HG_KEY will contain the name of the bookmark.

HG_OLD will contain the hash of the commit the bookmark was before the operation. It won't be set if the bookmark is being created.

HG_NEW will contain the hash of the commit the bookmark will be after the operation. It won't be set if the bookmark is being deleted.

barjak
  • 10,842
  • 3
  • 33
  • 47
0
  1. Bookmarks
    • Bookmarks-handling does not require commit
    • Bookmark can be created|modified for any changeset in history, not only for current
    • Bookmark(s) can appear as result of data-exchange (pull|push), not local user's actions
    • Only part of all possible cases can be covered by hook
  2. Branches
    • Changing branch always reflected in commit
    • Branch in changeset may differ from parent's branch not only as result of hg branch (see "merge branches" and "mergesets") - and I haven't nice and easy and ready to use solution for this case
  3. Common notes
    • You can use standard precommit hook (executed before commit and can enable|disable commit) for testing different conditions in commit or or pretxncommit
    • Mercurial template-engine has keywords for branch and bookmark for using in hg log -T
    • In pretxncommit hook commit already exist, but not finalized - it means you can manipulate with commit's data using -r tip and tip's parent in any Mercurial command

Dirty skeleton for hook's body

hg log -T "{KEYWORD}\n" -r "tip + tip^" | ....

where KEYWORD may be branch or bookmarks. Result of log is two-strings output, which can be identical of different (not sure for bookmark, TBT!!), which you have to compare (as you want and can)

PS: Idea inspired by EnsureCommitPolicy Wiki and Mercurial pre commit hook topic here

Community
  • 1
  • 1
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110