source: trunk/scripts/git-migration/git-migrate @ 1896

Last change on this file since 1896 was 1896, checked in by broder, 15 years ago

More cleanup.

  • Property svn:executable set to *
File size: 5.1 KB
Line 
1#!/usr/bin/python
2
3import glob
4import os
5import sys
6import subprocess
7import shutil
8
9def tagBase(pkg):
10    p = subprocess.Popen(['git', 'tag',
11                          '-l',
12                          'base'],
13                         cwd='%s.git' % pkg,
14                         stdout=subprocess.PIPE)
15    p.wait()
16    if p.stdout.read().strip() != '':
17        return
18   
19    p = subprocess.Popen(['git', 'rev-list',
20                          '--reverse',
21                          'master'],
22                         cwd='%s.git' % pkg,
23                         stdout=subprocess.PIPE)
24    p.wait()
25    base = p.stdout.read().split()[0]
26   
27    subprocess.check_call(['git', 'tag',
28                           'base',
29                           base],
30                          cwd='%s.git' % pkg)
31
32def clonePackage(base, pkg):
33    path = '%s/%s' % (base, pkg)
34    pkg = os.path.basename(pkg)
35   
36    if not os.path.isdir('%s.git' % pkg):
37        if os.path.isdir(pkg):
38            shutil.rmtree(pkg)
39        # Use --no-follow-parent because we're going to handle that with
40        # grafts.
41        subprocess.check_call(['git', 'svn', 'clone',
42                               '--no-follow-parent',
43                               '-Aauthors',
44                               '-q',
45                               '--no-metadata',
46                               '%s' % path],
47                              stdout=subprocess.PIPE)
48       
49        # Then make the repository bare, because git-svn can't do this
50        shutil.move('%s/.git' % pkg, '%s.git' % pkg)
51        shutil.rmtree(pkg)
52        subprocess.check_call(['git', 'config', 'core.bare', 'true'],
53                              cwd='%s.git' % pkg)
54       
55    # Some of these repos have a rev where everything was deleted
56    # as a result of the move. We don't want that rev to exist.
57    p = subprocess.Popen(['git', 'ls-tree', 'HEAD'],
58                         cwd='%s.git' % pkg,
59                         stdout=subprocess.PIPE)
60    p.wait()
61    if len(p.stdout.read()) == 0:
62        subprocess.check_call(['git', 'reset', '--soft', 'HEAD^'],
63                              cwd='%s.git' % pkg)
64   
65    tagBase(pkg)
66
67def cloneAllPackages(base):
68    for pkg in open('package-list'):
69        clonePackage(base, pkg.strip())
70
71def mergeHistory(old_pkg, new_pkg, n):
72    n = int(n)
73   
74    subprocess.check_call(['git', 'push',
75                           '../%s.git' % new_pkg,
76                           'master:refs/heads/%s' % old_pkg],
77                          cwd='%s.git' % old_pkg)
78   
79    # Find the merge commit
80    if n == 0:
81        p = subprocess.Popen(['git', 'rev-parse',
82                              'base'],
83                             cwd='%s.git' % new_pkg,
84                             stdout=subprocess.PIPE)
85    else:
86        p = subprocess.Popen(['git', 'rev-list',
87                              '--reverse',
88                              '--boundary',
89                              '--skip=%s' % (n - 1),
90                              'base..master'],
91                             cwd='%s.git' % new_pkg,
92                             stdout=subprocess.PIPE)
93    p.wait()
94    new_rev = p.stdout.read().split()[0].strip('-')
95   
96    # Find any other parents of the merge commit
97    p = subprocess.Popen(['git', 'log',
98                          '-1',
99                          '--pretty=format:%P',
100                          new_rev],
101                         cwd='%s.git' % new_pkg,
102                         stdout=subprocess.PIPE)
103    p.wait()
104    parents = p.stdout.read().split()
105   
106    # Find the additional parent we're adding
107    p = subprocess.Popen(['git', 'rev-parse',
108                          old_pkg],
109                         cwd='%s.git' % new_pkg,
110                         stdout=subprocess.PIPE)
111    p.wait()
112    parents.append(p.stdout.read().strip())
113   
114    # Write out the grafts file
115    f = open('%s.git/info/grafts' % new_pkg, 'a')
116    print >>f, '%s %s' % (new_rev, ' '.join(parents))
117    f.close()
118   
119    # Run filter-branch
120    subprocess.call(['git', 'filter-branch',
121                     '--tag-name-filter', 'cat',
122                     '--',
123                     '--all'],
124                    cwd='%s.git' % new_pkg)
125   
126    subprocess.call(['git', 'branch',
127                     '-D',
128                     old_pkg],
129                    cwd='%s.git' % new_pkg)
130    shutil.rmtree('%s.git/refs/original' % new_pkg, True)
131
132def mergeHistories():
133    merges = []
134    for line in open('merges'):
135        line = line.strip()
136        if line == '' or line[0] == '#':
137            continue
138       
139        merges.append(line.split())
140   
141    for merge in merges:
142        mergeHistory(*merge)
143   
144    for merge in merges:
145        shutil.rmtree('%s.git' % merge[0])
146
147def cleanupRepos():
148    for pkg in glob.glob('*.git'):
149        subprocess.check_call(['git', 'tag', '-d', 'base'],
150                              cwd='%s' % pkg)
151       
152        subprocess.check_call(['git', 'gc'],
153                              cwd='%s' % pkg)
154
155if __name__ == '__main__':
156    try:
157        base = sys.argv[1]
158    except:
159        base = 'svn://invirt.mit.edu/trunk'
160   
161    cloneAllPackages(base)
162    mergeHistories()
163    cleanupRepos()
Note: See TracBrowser for help on using the repository browser.