Add hg branch detection

Add hg branch detection (first check for git then hg).
(the design may be wrong)