annotate hggit_sync.py @ 1:e9f44d2deb94

Add some lame improvements to the map building.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 19 Feb 2016 16:01:24 -0800
parents 6da45bb59fd0
children 19156ccdc3e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import sys
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import codecs
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import shutil
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import argparse
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import subprocess
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 class CommitInfo(object):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 def __init__(self):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 self.nodeid = None
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 self.timestamp = None
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 self.description = None
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 def parse_commits(text):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 commits = []
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 cur_commit = None
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 for line in text.split('\n'):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 if line == '':
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 if cur_commit:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 commits.append(cur_commit)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 cur_commit = None
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 continue
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 if cur_commit is None:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 cur_commit = CommitInfo()
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 if cur_commit.nodeid is None:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 id_and_date = line.split(' ', 1)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 cur_commit.nodeid = id_and_date[0]
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 cur_commit.timestamp = int(id_and_date[1].split('.')[0])
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 else:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 cur_commit.description = line
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 return commits
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 def build_commit_map(commits1, commits2):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 commits1 = sorted(commits1, key=lambda c: c.timestamp)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 commits2 = sorted(commits2, key=lambda c: c.timestamp)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 commit_map = dict(map(lambda c: (c.timestamp, (c, None)), commits1))
1
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
41 orphan_commits2 = []
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
42 print("Building commit map...")
0
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 for c in commits2:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 entry = commit_map.get(c.timestamp, (None, None))
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 entry = (entry[0], c)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 commit_map[c.timestamp] = entry
1
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
47 if entry[0] is None:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
48 orphan_commits2.append(c)
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
49
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
50 if orphan_commits2:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
51 print("Fixing orphaned commits...")
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
52 did_fix = True
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
53 orphan_commits1 = [e[0] for e in commit_map.values() if e[1] is None]
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
54 while did_fix:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
55 did_fix = False
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
56 for c2 in orphan_commits2:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
57 for c1 in orphan_commits1:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
58 if c1.description.strip() == c2.description.strip():
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
59 print("Mapping '%s' to '%s'" % (c1.nodeid, c2.nodeid))
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
60 print(" Similar description: %s" % c1.description)
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
61 print(" Timestamp difference: %d" % (c2.timestamp - c1.timestamp))
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
62 commit_map[c1.timestamp] = (c1, c2)
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
63 orphan_commits1.remove(c1)
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
64 orphan_commits2.remove(c2)
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
65 did_fix = True
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
66 break
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
67 if did_fix:
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
68 break
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
69
0
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 return commit_map
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 def main():
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 parser = argparse.ArgumentParser(
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 description="Helps you fix problems with hg-git. Maybe.",
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 epilog="Don't trust scripts you found on the web! Backup your stuff!")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 parser.add_argument(
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 '--rebuild',
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 nargs=1,
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 metavar='REMOTE',
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 help="Rebuild the Git repo from the given remote URL.")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 parser.add_argument(
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 'mapfile',
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 metavar='MAPFILE',
1
e9f44d2deb94 Add some lame improvements to the map building.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
85 nargs='?',
0
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 help="The path to the mapfile to generate.")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 res = parser.parse_args()
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 hg_repo = os.getcwd()
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 if not os.path.exists(os.path.join(hg_repo, '.hg')):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 print("You must run this in the root of a Mercurial repository.")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 return 1
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 git_repo = os.path.join(hg_repo, '.hg', 'git')
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 if res.rebuild:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 print("Removing existing Git repo...")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 shutil.rmtree(git_repo)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 print("Syncing it again into: %s" % git_repo)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 git_output = subprocess.check_output([
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 'git', 'clone', '--bare', res.rebuild, git_repo])
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 if not os.path.exists(git_repo):
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 print("This Mercurial repository doesn't seem to have any Git mirror "
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 "to sync with.")
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 return 1
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 hg_output = subprocess.check_output([
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 'hg', 'log',
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 '--template', "{node} {date}\n{firstline(desc)}\n\n"])
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 hg_commits = parse_commits(hg_output)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 os.chdir(git_repo)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 git_output = subprocess.check_output([
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 'git', 'log', '--format=%H %ct%n%s%n%n'])
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 git_commits = parse_commits(git_output)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 os.chdir(hg_repo)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 commit_map = build_commit_map(git_commits, hg_commits)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 for key, val in commit_map.iteritems():
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 if val[0] is None:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 print("Mercurial commit '%s' (%s) has no Git mirror yet: %s" %
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 (val[1].nodeid, val[1].timestamp, val[1].description))
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 if val[1] is None:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 print("Git commit '%s' (%s) is new: %s" %
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 (val[0].nodeid, val[0].timestamp, val[0].description))
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 map_file = res.mapfile or os.path.join(hg_repo, '.hg', 'git-mapfile')
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 print("Saving map file: %s" % map_file)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 with codecs.open(map_file, 'w', encoding='utf8') as fp:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 for key, val in commit_map.iteritems():
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 if val[0] is None or val[1] is None:
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 continue
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 fp.write(val[0].nodeid)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 fp.write(' ')
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 fp.write(val[1].nodeid)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 fp.write('\n')
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 if __name__ == '__main__':
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 res = main()
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 sys.exit(res)
6da45bb59fd0 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142