]> git.proxmox.com Git - mirror_frr.git/blob - git-reindent-branch.py
Merge pull request #908 from donaldsharp/safi
[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 revs.reverse()
36
37 srcdir = os.getcwd()
38 tmpdir = tempfile.mkdtemp('frrindent')
39 os.chdir(tmpdir)
40
41 sys.stderr.write('using temporary directory %s; %d revisions\n' % (tmpdir, len(revs)))
42 run(['git', 'clone', '-s', '-b', 'reindent-master-after', srcdir, 'repo'])
43 os.chdir('repo')
44
45 prev = beforeid
46 for rev in revs:
47 filestat = run(['git', 'diff', '-z', '--name-status', prev, rev]).rstrip('\0').split('\0')
48 changes = zip(filestat[0::2], filestat[1::2])
49 sys.stderr.write('%s: %d files\n' % (rev, len(changes)))
50
51 for typ, name in changes:
52 if typ == 'D':
53 run(['git', 'rm', name])
54 elif typ in ['A', 'M']:
55 run(['git', 'checkout', rev, '--', name])
56 if name.endswith('.c') or name.endswith('.h'):
57 for d in ['babeld/', 'ldpd/', 'nhrpd/']:
58 if name.startswith(d):
59 break
60 else:
61 sys.stderr.write('\t%s\n' % name)
62 indent.wrap_file(name)
63 run(['git', 'add', name])
64
65 run(['git', 'commit', '-C', rev])
66 prev = rev
67
68 run(['git', 'push', 'origin', 'HEAD:refs/heads/reindented-branch'])
69 sys.stderr.write('\n\n"reindented-branch" should now be OK.\n')
70 sys.stderr.write('you could use "git reset --hard reindented-branch" to set your current branch to the reindented output\n')
71 sys.stderr.write('\033[31;1mplease always double-check the output\033[m\n')
72