patches.py 896 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. import os
  3. def read_patch(patch_dir, patch_filename):
  4. """Read a patch from |patch_dir/filename| and amend the commit message with
  5. metadata about the patch file it came from."""
  6. ret = []
  7. added_filename_line = False
  8. with open(os.path.join(patch_dir, patch_filename)) as f:
  9. for l in f.readlines():
  10. if not added_filename_line and (l.startswith('diff -') or l.startswith('---')):
  11. ret.append('Patch-Filename: {}\n'.format(patch_filename))
  12. added_filename_line = True
  13. ret.append(l)
  14. return ''.join(ret)
  15. def patch_from_dir(patch_dir):
  16. """Read a directory of patches into a format suitable for passing to
  17. 'git am'"""
  18. with open(os.path.join(patch_dir, ".patches")) as f:
  19. patch_list = [l.rstrip('\n') for l in f.readlines()]
  20. return ''.join([
  21. read_patch(patch_dir, patch_filename)
  22. for patch_filename in patch_list
  23. ])