Skip to content

Instantly share code, notes, and snippets.

@zqqf16
Created October 22, 2013 03:10
Show Gist options
  • Save zqqf16/7094628 to your computer and use it in GitHub Desktop.
Save zqqf16/7094628 to your computer and use it in GitHub Desktop.
Get changed files from diff
#!/usr/bin/env python
import re
import sys
import argparse
FILE_RE = re.compile(r'^Index: (.*)$')
def get_files_from_diff(diff):
res = []
for line in diff:
m = FILE_RE.match(line)
if m:
res.append(m.group(1))
return res
def main():
parser = argparse.ArgumentParser(description='Get changed files from diff')
parser.add_argument('file', nargs='?', help='Diff file')
parser.add_argument('-i', dest='input', action='store_true',
help='Read diff string from stdin')
parser.add_argument('-l', dest="list", action='store_true',
help='List all files')
args = parser.parse_args()
if args.input:
ret = get_files_from_diff(sys.stdin)
elif args.file:
try:
with open(args.file, 'r') as f:
ret = get_files_from_diff(f)
except:
print('\033[;31m[ERROR!] \033[0mFail to open "{0}"'.format(args.file))
exit(-1)
else:
parser.print_help()
exit(2)
if args.list:
for index, text in enumerate(ret):
print('\033[;33m[{0}] \033[;32m{1}\033[0m'.format(index, text))
else:
print(' '.join(ret))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment