]> git.proxmox.com Git - mirror_frr.git/blob - git-reindent-branch.py
*: add git-reindent-branch.py
[mirror_frr.git] / git-reindent-branch.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import sys, os
5 import subprocess, argparse, tempfile
6 import indent
7
8 def run(cmd):
9 proc = subprocess.Popen(cmd, stdout = subprocess.PIPE)
10 rv = proc.communicate('')[0].decode('UTF-8')
11 proc.wait()
12 return rv
13
14 argp = argparse.ArgumentParser(description = 'git whitespace-fixing tool')
15 argp.add_argument('branch', metavar='BRANCH', type = str, nargs = '?', default = 'HEAD')
16 args = argp.parse_args()
17
18 branch = args.branch
19 commit = run(['git', 'rev-list', '-n', '1', branch, '--']).strip()
20 beforeid = run(['git', 'rev-list', '-n', '1', 'reindent-master-before', '--']).strip()
21 afterid = run(['git', 'rev-list', '-n', '1', 'reindent-master-after', '--']).strip()
22
23 beforebase = run(['git', 'merge-base', commit, beforeid]).strip()
24 afterbase = run(['git', 'merge-base', commit, afterid]).strip()
25
26 if afterbase == afterid:
27 sys.stderr.write('this branch was already rebased\n')
28 sys.exit(1)
29
30 if beforebase != beforeid:
31 sys.stderr.write('you need to rebase your branch onto the tag "reindent-master-before"\n')
32 sys.exit(1)
33
34 revs = run(['git', 'rev-list', 'reindent-master-before..%s' % commit]).strip().split('\n')
35
36 srcdir = os.getcwd()
37 tmpdir = tempfile.mkdtemp('frrindent')
38 os.chdir(tmpdir)
39
40 sys.stderr.write('using temporary directory %s; %d revisions\n' % (tmpdir, len(revs)))
41 run(['git', 'clone', '-s', '-b', 'reindent-master-after', srcdir, 'repo'])
42 os.chdir('repo')
43
44 prev = beforeid
45 for rev in revs:
46 filestat = run(['git', 'diff', '-z', '--name-status', prev, rev]).rstrip('\0').split('\0')
47 changes = zip(filestat[0::2], filestat[1::2])
48 sys.stderr.write('%s: %d files\n' % (rev, len(changes)))
49
50 for typ, name in changes:
51 if typ == 'D':
52 run(['git', 'rm', name])
53 elif typ in ['A', 'M']:
54 run(['git', 'checkout', rev, '--', name])
55 if name.endswith('.c') or name.endswith('.h'):
56 for d in ['babeld/', 'ldpd/', 'nhrpd/']:
57 if name.startswith(d):
58 break
59 else:
60 sys.stderr.write('\t%s\n' % name)
61 indent.wrap_file(name)
62 run(['git', 'add', name])
63
64 run(['git', 'commit', '-C', rev])
65 prev = rev
66
67 run(['git', 'push', 'origin', 'HEAD:refs/heads/reindented-branch'])
68 sys.stderr.write('\n\n"reindented-branch" should now be OK.\n')
69 sys.stderr.write('you could use "git reset --hard reindented-branch" to set your current branch to the reindented output\n')
70 sys.stderr.write('\033[31;1mplease always double-check the output\033[m\n')
71