Preparing for release of 3.2.0pre2
[rsync.git] / packaging / patch-update
1 #!/usr/bin/python3 -B
2
3 # This script is used to turn one or more of the "patch/BASE/*" branches
4 # into one or more diffs in the "patches" directory.  Pass the option
5 # --gen if you want generated files in the diffs.  Pass the name of
6 # one or more diffs if you want to just update a subset of all the
7 # diffs.
8
9 import os, sys, re, argparse, time, shutil
10
11 sys.path = ['packaging'] + sys.path
12
13 from pkglib import *
14
15 MAKE_GEN_CMDS = [
16         'make -f prepare-source.mak conf'.split(),
17         './config.status'.split(),
18         'make gen'.split(),
19         ]
20 TMP_DIR = "patches.gen"
21
22 os.environ['GIT_MERGE_AUTOEDIT'] = 'no'
23
24 def main():
25     global master_commit, parent_patch, description, completed, last_touch
26
27     if not os.path.isdir(args.patches_dir):
28         die(f'No "{args.patches_dir}" directory was found.')
29     if not os.path.isdir('.git'):
30         die('No ".git" directory present in the current dir.')
31
32     starting_branch, args.base_branch = check_git_state(args.base_branch, not args.skip_check, args.patches_dir)
33
34     master_commit = latest_git_hash(args.base_branch)
35
36     if args.gen:
37         if os.path.lexists(TMP_DIR):
38             die(f'"{TMP_DIR}" must not exist in the current directory.')
39         gen_files = get_gen_files()
40         os.mkdir(TMP_DIR, 0o700)
41         for cmd in MAKE_GEN_CMDS:
42             cmd_chk(cmd)
43         cmd_chk(['rsync', '-a', *gen_files, f'{TMP_DIR}/master/'])
44
45     last_touch = time.time()
46
47     # Start by finding all patches so that we can load all possible parents.
48     patches = sorted(list(get_patch_branches(args.base_branch)))
49
50     parent_patch = { }
51     description = { }
52
53     for patch in patches:
54         branch = f"patch/{args.base_branch}/{patch}"
55         desc = ''
56         proc = cmd_pipe(['git', 'diff', '-U1000', f"{args.base_branch}...{branch}", '--', f"PATCH.{patch}"])
57         in_diff = False
58         for line in proc.stdout:
59             if in_diff:
60                 if not re.match(r'^[ +]', line):
61                     continue
62                 line = line[1:]
63                 m = re.search(r'patch -p1 <patches/(\S+)\.diff', line)
64                 if m and m[1] != patch:
65                     parpat = parent_patch[patch] = m[1]
66                     if not parpat in patches:
67                         die(f"Parent of {patch} is not a local branch: {parpat}")
68                 desc += line
69             elif re.match(r'^@@ ', line):
70                 in_diff = True
71         description[patch] = desc
72         proc.communicate()
73
74     if args.patch_files: # Limit the list of patches to actually process
75         valid_patches = patches
76         patches = [ ]
77         for fn in args.patch_files:
78             name = re.sub(r'\.diff$', '', re.sub(r'.+/', '', fn))
79             if name not in valid_patches:
80                 die(f"Local branch not available for patch: {name}")
81             patches.append(name)
82
83     completed = set()
84
85     for patch in patches:
86         if patch in completed:
87             continue
88         if not update_patch(patch):
89             break
90
91     if args.gen:
92         shutil.rmtree(TMP_DIR)
93
94     while last_touch >= time.time():
95         time.sleep(1)
96     cmd_chk(['git', 'checkout', starting_branch])
97
98
99 def update_patch(patch):
100     global last_touch
101
102     completed.add(patch) # Mark it as completed early to short-circuit any (bogus) dependency loops.
103
104     parent = parent_patch.get(patch, None)
105     if parent:
106         if parent not in completed:
107             if not update_patch(parent):
108                 return 0
109         based_on = parent = f"patch/{args.base_branch}/{parent}"
110     else:
111         parent = args.base_branch
112         based_on = master_commit
113
114     print(f"======== {patch} ========")
115
116     while args.gen and last_touch >= time.time():
117         time.sleep(1)
118     s = cmd_run(f"git checkout patch/{args.base_branch}/{patch}".split())
119     if s.returncode != 0:
120         return 0
121
122     s = cmd_run(['git', 'merge', based_on])
123     ok = s.returncode == 0
124     if not ok or args.shell:
125         m = re.search(r'([^/]+)$', parent)
126         parent_dir = m[1]
127         if not ok:
128             print(f'"git merge {based_on}" incomplete -- please fix.')
129         os.environ['PS1'] = f"[{parent_dir}] {patch}: "
130         while True:
131             s = cmd_run([os.environ.get('SHELL', '/bin/sh')])
132             if s.returncode != 0:
133                 ans = input("Abort? [n/y] ")
134                 if re.match(r'^y', ans, flags=re.I):
135                     return 0
136                 continue
137             cur_branch, is_clean, status_txt = check_git_status(0)
138             if is_clean:
139                 break
140             print(status_txt, end='')
141
142     with open(f"{args.patches_dir}/{patch}.diff", 'w', encoding='utf-8') as fh:
143         fh.write(description[patch])
144         fh.write(f"\nbased-on: {based_on}\n")
145
146         if args.gen:
147             gen_files = get_gen_files()
148             for cmd in MAKE_GEN_CMDS:
149                 cmd_chk(cmd)
150             cmd_chk(['rsync', '-a', *gen_files, f"{TMP_DIR}/{patch}/"])
151         else:
152             gen_files = [ ]
153         last_touch = time.time()
154
155         proc = cmd_pipe(['git', 'diff', based_on])
156         skipping = False
157         for line in proc.stdout:
158             if skipping:
159                 if not re.match(r'^diff --git a/', line):
160                     continue
161                 skipping = False
162             elif re.match(r'^diff --git a/PATCH', line):
163                 skipping = True
164                 continue
165             if not re.match(r'^index ', line):
166                 fh.write(line)
167         proc.communicate()
168
169         if args.gen:
170             e_tmp_dir = re.escape(TMP_DIR)
171             diff_re  = re.compile(r'^(diff -Nurp) %s/[^/]+/(.*?) %s/[^/]+/(.*)' % (e_tmp_dir, e_tmp_dir))
172             minus_re = re.compile(r'^\-\-\- %s/[^/]+/([^\t]+)\t.*' % e_tmp_dir)
173             plus_re  = re.compile(r'^\+\+\+ %s/[^/]+/([^\t]+)\t.*' % e_tmp_dir)
174
175             if parent == args.base_branch:
176                 parent_dir = 'master'
177             else:
178                 m = re.search(r'([^/]+)$', parent)
179                 parent_dir = m[1]
180
181             proc = cmd_pipe(['diff', '-Nurp', f"{TMP_DIR}/{parent_dir}", f"{TMP_DIR}/{patch}"])
182             for line in proc.stdout:
183                 line = diff_re.sub(r'\1 a/\2 b/\3', line)
184                 line = minus_re.sub(r'--- a/\1', line)
185                 line =  plus_re.sub(r'+++ b/\1', line)
186                 fh.write(line)
187             proc.communicate()
188             for fn in gen_files:
189                 os.unlink(fn)
190
191     return 1
192
193
194 if __name__ == '__main__':
195     parser = argparse.ArgumentParser(description="Turn a git branch back into a diff files in the patches dir.", add_help=False)
196     parser.add_argument('--branch', '-b', dest='base_branch', metavar='BASE_BRANCH', default='master', help="The branch the patch is based on. Default: master.")
197     parser.add_argument('--skip-check', action='store_true', help="Skip the check that ensures starting with a clean branch.")
198     parser.add_argument('--shell', '-s', action='store_true', help="Launch a shell for every patch/BASE/* branch updated, not just when a conflict occurs.")
199     parser.add_argument('--gen', metavar='DIR', nargs='?', const='', help='Include generated files. Optional DIR value overrides the default of using the "patches" dir.')
200     parser.add_argument('--patches-dir', '-p', metavar='DIR', default='patches', help="Override the location of the rsync-patches dir. Default: patches.")
201     parser.add_argument('patch_files', metavar='patches/DIFF_FILE', nargs='*', help="Specify what patch diff files to process. Default: all of them.")
202     parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
203     args = parser.parse_args()
204     if args.gen == '':
205         args.gen = args.patches_dir
206     elif args.gen is not None:
207         args.patches_dir = args.gen
208     main()
209
210 # vim: sw=4 et