Created
September 13, 2019 10:01
-
-
Save flavio-fernandes/71aebcac7cc2a1a902a5e899bdd76b62 to your computer and use it in GitHub Desktop.
given a patch file, extract the files that get changed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# usage: filesInPatch.py foo.patch | |
import sys | |
import re | |
# Initialize defaults | |
debug = 0 | |
fileNames = {} | |
# ====================================================================== | |
def printError(msg): | |
sys.stderr.write(msg) | |
# ====================================================================== | |
def parseInput(inputFile): | |
global debug, fileNames | |
while 1: | |
lineBuffer = inputFile.readline() | |
if not lineBuffer: break | |
# print lineBuffer, | |
lineMatch = re.search("^\s*---\s+([^\s@]+)[\s@]+", lineBuffer) | |
if not lineMatch: | |
lineMatch = re.search("^\s*\+\+\+\s+([^\s@]+)[\s@]+", lineBuffer) | |
if lineMatch: | |
currFileName = lineMatch.group(1) | |
# trim off 'a/' and 'b/' that you will normally see in git output | |
# | |
if len(currFileName) > 2 and currFileName[1] == '/' and \ | |
(currFileName[0] == 'a' or currFileName[0] == 'b'): | |
currFileName = currFileName[2:] | |
# ignore funny files that git can produce | |
# | |
if currFileName == '/dev/null': | |
continue | |
if not currFileName in fileNames: | |
# print currFileName | |
fileNames[currFileName] = 1 | |
else: | |
fileNames[currFileName] += 1 | |
# ====================================================================== | |
def pruneUnwantedNames(): | |
global debug, fileNames | |
names = fileNames.keys() | |
unwantedNames = [] | |
for currName in names: | |
# ignore files that end in '.orig' as long as non-.orig exists | |
lineMatch = re.search("^(.+)\.[oO][Rr][iI][gG]$", currName) | |
if lineMatch and lineMatch.group(1) in fileNames: | |
unwantedNames.append( currName ) | |
for currName in unwantedNames: | |
del fileNames[ currName ] | |
if debug: | |
print("unwanted name: {}".format(currName)) | |
# ====================================================================== | |
def printFileNames(): | |
global debug, fileNames | |
names = list(fileNames.keys()) | |
names.sort() | |
if debug: | |
for currName in names: | |
print("{} ==> {}".format(currName, fileNames[currName])) | |
else: | |
for currName in names: | |
print(currName) | |
# ====================================================================== | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
parseInput(sys.stdin) | |
else: | |
for currInputName in sys.argv[1:]: | |
try: | |
# print currInputName | |
currInputFile = open(currInputName, 'r') | |
parseInput(currInputFile) | |
currInputFile.close() | |
except IOError as eStr: | |
printError("Cannot open %s: %s\n" % (currInputName, eStr)) | |
sys.exit(255) | |
pruneUnwantedNames() | |
printFileNames() | |
sys.exit(0) | |
# ====================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment