patches.py 778 B

123456789101112131415161718192021222324252627
  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. with open(os.path.join(patch_dir, patch_filename)) as f:
  8. for l in f.readlines():
  9. if l.startswith('diff -'):
  10. ret.append('Patch-Filename: {}\n'.format(patch_filename))
  11. ret.append(l)
  12. return ''.join(ret)
  13. def patch_from_dir(patch_dir):
  14. """Read a directory of patches into a format suitable for passing to
  15. 'git am'"""
  16. with open(os.path.join(patch_dir, ".patches")) as f:
  17. patch_list = [l.rstrip('\n') for l in f.readlines()]
  18. return ''.join([
  19. read_patch(patch_dir, patch_filename)
  20. for patch_filename in patch_list
  21. ])