precommit mercurial hook to stop commits to the wrong branch

Faheem Mitha faheem at faheem.info
Wed Oct 9 09:09:04 UTC 2013


Hi,

I posted the following question to Stack Overflow.

http://stackoverflow.com/q/19234473/350713

The text of that question is below.

Ry4an kindly replied with a bash solution. I think I would prefer a Python 
version, but that would use the Mercurial "not-an-api", as Ry4an put it. 
Does this look practical? If anyone feels like taking the time to sketch 
an approach, I'd appreciate it. Regardless, any comments would be 
appreciated, Thanks.

                                                            Regards, Faheem

#######################################################################

I have a piece of software in a Mercurial repository.

I'm packaging my software project as a Debian package. It seems the 
standard way to do this is to have a separate branch for the Debian 
package files, which live in the debian sub-directory.

One problem I keep having is that I forget which branch I am on and 
accidentally commit to the wrong branch. This happens frequently, and is 
really annoying. When this happens I usually push to remote before 
realising the problem, and then have to fix up the local and remote 
repositories manually, which is a pain.

The only option I can think of is to have a pre-commit hook that aborts if 
I am trying to make commits to the wrong branch.

To be concrete, let's say the main branch is called default and the branch 
containing Debian files is called debian. Then I want commits to the 
default branch to succeed only if none of the files in the commit are from 
the debian directory. I want commits to the debian directory to succeed 
only if all the files in the commit are in the debian directory.

I spent some time reading the chapter on Mercurial Hooks and going through 
the examples in the Hg Book, but still have no idea how to go about this. 
I did get the strong impression that for something like this I should be 
calling out to an external Python script, probably in .hg/

##########################################################################



More information about the Mercurial mailing list