Mercurial > dotfiles
changeset 295:e40cdce29e5a
Hacky hg-git sync script.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 02 Apr 2015 08:00:57 -0700 |
parents | 8bc056d80c39 |
children | 875857ed8004 |
files | .hgsubstate lib/hg/hggit_sync.py |
diffstat | 2 files changed, 99 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgsubstate Thu Apr 02 08:00:31 2015 -0700 +++ b/.hgsubstate Thu Apr 02 08:00:57 2015 -0700 @@ -1,6 +1,6 @@ 17a51b56d88bd1f1826945413790c482032bb8d3 lib/fish/virtualfish f8703c9e2fb951ebb193b268faf7f0457abf8334 lib/hg/allpaths -d40de32ec5b1380ed20888c744d4f8f468fbf226 lib/hg/hg-git +93689e8f27ac657feed5332b71e66210c3a81cbf lib/hg/hg-git 1cb3af183d2f692622f84da88d5ca18bd4f91b1f lib/hg/hg-prompt 214cd3c6ab6f76b7aaed0b3f8f4300d09f7cb560 lib/hg/hgremotenames 66e2a34005b447d321a96838d7fe2b161e78b7a9 lib/hg/mercurial-cli-templates @@ -11,16 +11,16 @@ db3707cbd8706f4bb054959ecc5cee82ac45687b vim/bundle/badwolf 81c6dd7ce3169e5ad9ba92422ba6e1ce5b074e36 vim/bundle/colorschemes 9c685131a5facfa0d643feca3a61b41c007d8170 vim/bundle/commentary -faafe125db65c4a3b9537a60a968e723b88a6c8f vim/bundle/ctrlp -0b311aa53a49be0c74aa784274a70441dd40b2e7 vim/bundle/ctrlp-autoignore +8bf346cf9961d69114ab04b9d18346799a714440 vim/bundle/ctrlp +634d881b6ac6b1283e24229af3fc736faaa899ec vim/bundle/ctrlp-autoignore 9ceebf91fc137644cf0693561a386ef4071dbf87 vim/bundle/easymotion 2c8461db084d205903a792a23163faa546f143c9 vim/bundle/fugitive eb9fc8676b8959c3c2c95bf6b6e8f0f44317c5c0 vim/bundle/gundo -8b3c611a4d3b0f9ea62091925cc9b8137ddd5054 vim/bundle/gutentags +64de69ca71950cb30fef88fe817a7eff08854fcc vim/bundle/gutentags 204e32721154766e03e99ff857bc798aa5b741dc vim/bundle/haml 6e0ac033107bd12c1390cbdf49398393930f3fb6 vim/bundle/interestingwords 8a8f0ed97c1751d304cf5b7241f2fe27b0e61f81 vim/bundle/jinja -7da11e02977391513169d166f5ebbe9c1d030627 vim/bundle/lawrencium +7cfe9ba2c7dd99c25bbf2992fe928447e0b3de4a vim/bundle/lawrencium 940a8defa0576385dee1ad177bab0b34738540aa vim/bundle/less b69e54f4bf0a0ee26f6582ee8764b25529610c88 vim/bundle/linediff 409c37b205afa2f9d590e23de8171482d66770e2 vim/bundle/markdown
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/hg/hggit_sync.py Thu Apr 02 08:00:57 2015 -0700 @@ -0,0 +1,94 @@ +import os +import os.path +import sys +import subprocess + + +class CommitInfo(object): + def __init__(self): + self.nodeid = None + self.timestamp = None + self.description = None + + +def parse_commits(text): + commits = [] + cur_commit = None + for line in text.split('\n'): + if line == '': + if cur_commit: + commits.append(cur_commit) + cur_commit = None + continue + if cur_commit is None: + cur_commit = CommitInfo() + if cur_commit.nodeid is None: + id_and_date = line.split(' ', 1) + cur_commit.nodeid = id_and_date[0] + cur_commit.timestamp = int(id_and_date[1].split('.')[0]) + else: + cur_commit.description = line + return commits + + +def build_commit_map(commits1, commits2): + commits1 = sorted(commits1, key=lambda c: c.timestamp) + commits2 = sorted(commits2, key=lambda c: c.timestamp) + commit_map = dict(map(lambda c: (c.timestamp, (c, None)), commits1)) + for c in commits2: + entry = commit_map.get(c.timestamp, (None, None)) + entry = (entry[0], c) + commit_map[c.timestamp] = entry + return commit_map + + +def main(): + hg_repo = os.getcwd() + if not os.path.exists(os.path.join(hg_repo, '.hg')): + print "You must run this in the root of a Mercurial repository." + return 1 + + git_repo = os.path.join(hg_repo, '.hg', 'git') + if not os.path.exists(git_repo): + print ("This Mercurial repository doesn't seem to have any Git mirror " + "to sync with.") + return 1 + + hg_output = subprocess.check_output([ + 'hg', 'log', + '--template', "{node} {date}\n{firstline(desc)}\n\n"]) + hg_commits = parse_commits(hg_output) + + os.chdir(git_repo) + git_output = subprocess.check_output([ + 'git', 'log', '--format=%H %ct%n%s%n%n']) + git_commits = parse_commits(git_output) + os.chdir(hg_repo) + + commit_map = build_commit_map(git_commits, hg_commits) + for key, val in commit_map.iteritems(): + if val[0] is None: + print ("Mercurial commit '%s' (%s) has no Git mirror yet: %s" % + (val[1].nodeid, val[1].timestamp, val[1].description)) + if val[1] is None: + print ("Git commit '%s' (%s) is new: %s" % + (val[0].nodeid, val[0].timestamp, val[0].description)) + + map_file = os.path.join(hg_repo, '.hg', 'git-mapfile') + if len(sys.argv) > 1: + map_file = sys.argv[1] + print "Saving map file: %s" % map_file + with open(map_file, 'w') as fp: + for key, val in commit_map.iteritems(): + if val[0] is None or val[1] is None: + continue + fp.write(val[0].nodeid) + fp.write(' ') + fp.write(val[1].nodeid) + fp.write('\n') + + +if __name__ == '__main__': + res = main() + sys.exit(res) +