Created
September 23, 2011 14:51
-
-
Save ringods/1237544 to your computer and use it in GitHub Desktop.
Pre-update hook to remove subrepositories for branch switching to work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Removes .hgsub and any subrepos when switching branches in the parent repository | |
To activate this hook, add this to your ~/.hgrc file: | |
[extensions] | |
switch_branch_with_subrepo = | |
[hooks] | |
preupdate.switch_branch_with_subrepo = python:switch_branch_with_subrepo.preupdate | |
''' | |
from mercurial.node import short | |
from mercurial import util | |
import os | |
import shutil | |
def preupdate(ui, repo, hooktype, parent1, parent2): | |
""" | |
Remove subrepositories and .hgsub file before updating to a revision that spans branches | |
""" | |
if parent2 == None or parent2 == '': | |
# If the second parent is set, this is a merge, so don't remove files!! | |
# http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#sec:hook:preupdate | |
return False | |
#pass | |
# repo[None] is the change context of the working copy and could be dirty, so has no revision. | |
# Retrieve the parent to refer to the last commited revision. | |
sourceRevision = repo[None].parents()[0] | |
sourceBranch = sourceRevision.branch() | |
ui.write("Current revision %s on branch %s" % (sourceRevision, sourceBranch)) | |
ui.write('\n') | |
targetRevision = repo[parent1] | |
targetBranch = targetRevision.branch() | |
ui.write("Switching to %s on branch %s" % (targetRevision, targetBranch)) | |
ui.write('\n') | |
ui.write(repo.root) | |
ui.write('\n') | |
hgsub = os.path.join(repo.root, '.hgsub') | |
if os.path.exists(hgsub): | |
ui.write('Source branch has subrepositories\n') | |
hgsubfile = open(hgsub, 'r') | |
for subrepo in hgsubfile: | |
parts = subrepo.split('=') | |
shutil.rmtree(os.path.join(repo.root,parts[0].strip())) | |
hgsubfile.close() | |
os.remove(hgsub) | |
else: | |
ui.write('Source branch has no subrepositories\n') | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment