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 adds the refs passed to the script as parents to ignore/foo and on |
| 12 success deletes them. |
| 13 |
| 14 Assumes push merge access to ignore/foo and force push access to the branches. |
| 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) > 1 |
| 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') |
| 61 data.append('Refs merged:\n' + '\n '.join(refs) + '\n') |
| 62 |
| 63 return git('hash-object', '-t', 'commit', '-w', '--stdin', |
| 64 stdin='\n'.join(data)) |
| 65 |
| 66 |
| 67 assert os.path.exists(sys.argv[1]) |
| 68 os.chdir(sys.argv[1]) |
| 69 |
| 70 refs = sys.argv[2:] |
| 71 assert refs, 'Provide at least one ref' |
| 72 |
| 73 # Make sure we only archive roll branches. |
| 74 for ref in refs: |
| 75 assert re.match(r'\d*\.\d*\.\d*', ref) |
| 76 |
| 77 # Merge into ignore/foo. |
| 78 git('fetch', 'origin', 'refs/heads/ignore/foo') |
| 79 merge = synthesize(['FETCH_HEAD'] + refs) |
| 80 git('push', 'origin', '%s:refs/heads/ignore/foo' % merge) |
| 81 |
| 82 # Delete refs. |
| 83 del_specs = ( |
| 84 ['push', 'origin', '--force'] + |
| 85 [':refs/heads/%s' % ref for ref in refs] |
| 86 ) |
| 87 git(*del_specs) |
OLD | NEW |