]> git.proxmox.com Git - mirror_frr.git/blob - tools/git-reindent-branch.py
bgpd: Allow 'no set community`
[mirror_frr.git] / tools / 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 clangfmt = run(['git', 'show', 'master:.clang-format'])
15
16 argp = argparse.ArgumentParser(description = 'git whitespace-fixing tool')
17 argp.add_argument('branch', metavar='BRANCH', type = str, nargs = '?', default = 'HEAD')
18 args = argp.parse_args()
19
20 branch = args.branch
21 commit = run(['git', 'rev-list', '-n', '1', branch, '--']).strip()
22
23 # frr-3.1-dev = first commit that is on master but not on stable/3.0
24 masterid = run(['git', 'rev-list', '-n', '1', 'frr-3.1-dev', '--']).strip()
25 masterbase = run(['git', 'merge-base', commit, masterid]).strip()
26
27 if masterbase == masterid:
28 refbranch = 'master'
29 else:
30 refbranch = '3.0'
31
32 sys.stderr.write('autodetected base: %s (can be 3.0 or master)\n' % refbranch)
33
34 beforeid = run(['git', 'rev-list', '-n', '1', 'reindent-%s-before' % refbranch, '--']).strip()
35 afterid = run(['git', 'rev-list', '-n', '1', 'reindent-%s-after' % refbranch, '--']).strip()
36
37 beforebase = run(['git', 'merge-base', commit, beforeid]).strip()
38 afterbase = run(['git', 'merge-base', commit, afterid]).strip()
39
40 if afterbase == afterid:
41 sys.stderr.write('this branch was already rebased\n')
42 sys.exit(1)
43
44 if beforebase != beforeid:
45 sys.stderr.write('you need to rebase your branch onto the tag "reindent-%s-before"\n' % refbranch)
46 sys.exit(1)
47
48 revs = run(['git', 'rev-list', 'reindent-%s-before..%s' % (refbranch, commit)]).strip().split('\n')
49 revs.reverse()
50
51 srcdir = os.getcwd()
52 tmpdir = tempfile.mkdtemp('frrindent')
53 os.chdir(tmpdir)
54
55 sys.stderr.write('using temporary directory %s; %d revisions\n' % (tmpdir, len(revs)))
56 run(['git', 'clone', '-s', '-b', 'reindent-%s-after' % refbranch, srcdir, 'repo'])
57 os.chdir('repo')
58
59 with open('.clang-format', 'w') as fd:
60 fd.write(clangfmt)
61
62 prev = beforeid
63 for rev in revs:
64 filestat = run(['git', 'diff', '-z', '--name-status', prev, rev]).rstrip('\0').split('\0')
65 changes = zip(filestat[0::2], filestat[1::2])
66 sys.stderr.write('%s: %d files\n' % (rev, len(changes)))
67
68 for typ, name in changes:
69 if typ == 'D':
70 run(['git', 'rm', name])
71 elif typ in ['A', 'M']:
72 run(['git', 'checkout', rev, '--', name])
73 if name.endswith('.c') or name.endswith('.h'):
74 for d in ['babeld/', 'ldpd/', 'nhrpd/']:
75 if name.startswith(d):
76 break
77 else:
78 sys.stderr.write('\t%s\n' % name)
79 indent.wrap_file(name)
80 run(['git', 'add', name])
81
82 run(['git', 'commit', '-C', rev])
83 prev = rev
84
85 run(['git', 'push', 'origin', 'HEAD:refs/heads/reindented-branch'])
86 sys.stderr.write('\n\n"reindented-branch" should now be OK.\n')
87 sys.stderr.write('you could use "git reset --hard reindented-branch" to set your current branch to the reindented output\n')
88 sys.stderr.write('\033[31;1mplease always double-check the output\033[m\n')
89