Mercurial > dotfiles
changeset 344:2d5974d70482
Move hg-git sync script to its own subrepo.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 18 Feb 2016 17:08:53 -0800 |
parents | f5847cdffc21 |
children | 7605d04d5306 |
files | .hgsub .hgsubstate lib/hg/hggit_sync.py |
diffstat | 3 files changed, 2 insertions(+), 95 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgsub Mon Feb 01 20:46:13 2016 -0800 +++ b/.hgsub Thu Feb 18 17:08:53 2016 -0800 @@ -35,6 +35,7 @@ mutt/mutt-colors-solarized = [git]https://github.com/altercation/mutt-colors-solarized lib/hg/hg-git = https://bitbucket.org/durin42/hg-git +lib/hg/hg-git-sync = https://bitbucket.org/ludovicchabant/hg-git-sync lib/hg/onsub = https://bitbucket.org/ludovicchabant/onsub lib/hg/allpaths = https://bitbucket.org/ludovicchabant/allpaths lib/hg/mercurial-cli-templates = https://bitbucket.org/sjl/mercurial-cli-templates
--- a/.hgsubstate Mon Feb 01 20:46:13 2016 -0800 +++ b/.hgsubstate Thu Feb 18 17:08:53 2016 -0800 @@ -1,6 +1,7 @@ 2d21e58f057de95244f1a68298c22d9d63cec052 lib/fish/virtualfish 3f8c4b4a5cc6e81c02974bd11126eae102eecb0a lib/hg/allpaths bcf3d04d6585aa12a3a64bd2411a653fb3f7e2b3 lib/hg/hg-git +6da45bb59fd0e3ac0e96472c0949e3fe2a8b807f lib/hg/hg-git-sync 5dc059aeb5f47dc86ef30821b8a8cdfbf10a4080 lib/hg/hg-prompt 66e2a34005b447d321a96838d7fe2b161e78b7a9 lib/hg/mercurial-cli-templates 99077dbb83e008fdf6485ff0edc0993a6769f2a0 lib/hg/mutable-history
--- a/lib/hg/hggit_sync.py Mon Feb 01 20:46:13 2016 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -import os -import os.path -import sys -import codecs -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 codecs.open(map_file, 'w', encoding='utf8') 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) -