OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import re | |
5 import subprocess | |
6 import sys | |
7 import time | |
8 | |
9 """Script to archive heads under ignore/foo. | |
10 | |
11 This deletes the refs passed to the script and adds the branch ignore/foo as | |
tandrii(chromium)
2016/08/08 15:29:39
swap "deletes" and "adds" part, because a) emotion
| |
12 parent to them. | |
13 | |
14 Assumes merge access to ignore/foo and force push access to the branches. | |
tandrii(chromium)
2016/08/08 15:29:39
s/merge access/push merge access
| |
15 | |
16 Assumes a workdir checkout with all required refs fetched. | |
17 | |
18 archive_branches.py path/to/checkout ref1 [ref2 ...] | |
19 """ | |
20 | |
21 | |
22 assert len(sys.argv) > 2 | |
23 | |
24 | |
25 def git(*args, **kwargs): | |
26 stdin_data = None | |
27 stdin_handle = None | |
28 if kwargs.get('stdin'): | |
29 stdin_data = kwargs.pop('stdin') | |
30 stdin_handle = subprocess.PIPE | |
31 proc = subprocess.Popen(('git',) + args, | |
32 stdout=subprocess.PIPE, | |
33 stderr=subprocess.STDOUT, | |
34 stdin=stdin_handle, | |
35 **kwargs) | |
36 output, _ = proc.communicate(stdin_data) | |
37 assert proc.returncode == 0, output.strip() | |
38 return output.strip() | |
39 | |
40 | |
41 def synthesize(refs): | |
42 commits = [] | |
43 for ref in refs: | |
44 commits.append(git('rev-parse', ref)) | |
45 | |
46 name = git('config', 'user.name') | |
47 email = git('config', 'user.email') | |
48 | |
49 # Hardcoded the tree object of the ignore/foo README file. | |
50 data = ['tree c9c91bce52cd3003b6a9b0b4ae29d46210c40d51'] | |
51 for commit in commits: | |
52 data.append('parent %s' % commit) | |
53 | |
54 timestamp = int(time.time()) | |
55 data.append('author %s <%s> %s +0000' % (name, email, timestamp)) | |
56 data.append('committer %s <%s> %s +0000' % (name, email, timestamp)) | |
57 data.append('') | |
58 | |
59 # We don't really care about the commit message. | |
60 data.append('Placeholder commit\n') | |
tandrii(chromium)
2016/08/08 15:29:39
actually you can add here all the refs merged:
('r
| |
61 | |
62 return git('hash-object', '-t', 'commit', '-w', '--stdin', | |
63 stdin='\n'.join(data)) | |
64 | |
65 | |
66 assert os.path.exists(sys.argv[1]) | |
67 os.chdir(sys.argv[1]) | |
68 | |
69 refs = sys.argv[2:] | |
tandrii(chromium)
2016/08/08 15:29:39
assert refs, "provide at least one ref"
| |
70 | |
71 # Make sure we only archive roll branches. | |
72 for ref in refs: | |
73 assert re.match(r'\d*\.\d*\.\d*', ref) | |
74 | |
75 # Merge into ignore/foo. | |
76 git('fetch', 'origin', 'refs/heads/ignore/foo') | |
77 merge = synthesize(['FETCH_HEAD'] + refs) | |
78 git('push', 'origin', '%s:refs/heads/ignore/foo' % merge) | |
79 | |
80 # Delete refs. | |
81 del_specs = ( | |
82 ['push', 'origin', '--force'] + | |
83 [':refs/heads/%s' % ref for ref in refs] | |
84 ) | |
85 git(*del_specs) | |
OLD | NEW |