2022-04-12 11:21:55 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-09-16 17:24:07 +00:00
|
|
|
|
2019-08-23 22:48:27 +00:00
|
|
|
import codecs
|
2018-09-14 05:02:16 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
2018-10-24 18:24:11 +00:00
|
|
|
def read_patch(patch_dir, patch_filename):
|
|
|
|
"""Read a patch from |patch_dir/filename| and amend the commit message with
|
|
|
|
metadata about the patch file it came from."""
|
|
|
|
ret = []
|
2020-04-13 16:46:15 +00:00
|
|
|
added_filename_line = False
|
2019-10-28 14:02:16 +00:00
|
|
|
patch_path = os.path.join(patch_dir, patch_filename)
|
|
|
|
with codecs.open(patch_path, encoding='utf-8') as f:
|
2018-10-24 18:24:11 +00:00
|
|
|
for l in f.readlines():
|
2020-10-19 11:55:27 +00:00
|
|
|
line_has_correct_start = l.startswith('diff -') or l.startswith('---')
|
|
|
|
if not added_filename_line and line_has_correct_start:
|
2018-10-24 18:24:11 +00:00
|
|
|
ret.append('Patch-Filename: {}\n'.format(patch_filename))
|
2020-04-13 16:46:15 +00:00
|
|
|
added_filename_line = True
|
2018-10-24 18:24:11 +00:00
|
|
|
ret.append(l)
|
|
|
|
return ''.join(ret)
|
|
|
|
|
|
|
|
|
|
|
|
def patch_from_dir(patch_dir):
|
|
|
|
"""Read a directory of patches into a format suitable for passing to
|
|
|
|
'git am'"""
|
|
|
|
with open(os.path.join(patch_dir, ".patches")) as f:
|
|
|
|
patch_list = [l.rstrip('\n') for l in f.readlines()]
|
|
|
|
|
|
|
|
return ''.join([
|
|
|
|
read_patch(patch_dir, patch_filename)
|
|
|
|
for patch_filename in patch_list
|
|
|
|
])
|