''' Created on 2 Mar 2017 @author: Dan ''' import subprocess import os import sys import re GIT_EXECUTABLE = os.getenv("GIT_EXECUTABLE") if GIT_EXECUTABLE == None: print("Please define the GIT_EXECUTABLE environment variable to the git executable path") sys.exit(1) WORKING_PATH = os.getenv("WORKING_PATH") if WORKING_PATH == None: print("Please define the WORKING_PATH environment variable to the git repository path to use") sys.exit(1) CURRENT_RELEASE = os.getenv("CURRENT_RELEASE") if CURRENT_RELEASE == None: print("Please define the CURRENT_RELEASE version") sys.exit(1) PREVIOUS_RELEASE = os.getenv("PREVIOUS_RELEASE") if PREVIOUS_RELEASE == None: print("Please define the PREVIOUS_RELEASE version") sys.exit(1) DIFFERENCE_OPTIONS = os.getenv("DIFFERENCE_OPTIONS") if DIFFERENCE_OPTIONS == None: DIFFERENCE_OPTIONS = "" class GitException(Exception): def __init__(self, message): self.message = message def __repr__(self): return self.message def printDivider(times = 1, length = 100): for _ in range(times): print("*" * length) TABSIZE = 4 def printTabbed(string, tabs = 0): print("{0}{1}".format(" " * (TABSIZE * tabs), string)) def getGitLogDifferences(workingPath, previousRelease, currentRelease, tripledot = False): gitExecutable = GIT_EXECUTABLE if tripledot: releaseExpression = previousRelease + "..." + currentRelease else: releaseExpression = previousRelease + ".." + currentRelease currentdir = os.getcwd() os.chdir(workingPath) gitlog = [] printTabbed("Executing: {0} {1} {2} {3}".format(gitExecutable, "log", "--oneline", releaseExpression)) proc = subprocess.Popen([gitExecutable, "log", "--oneline", releaseExpression], stdout=subprocess.PIPE) while True: line = proc.stdout.readline() if line: linestring = line.decode('utf-8').rstrip() gitlog.append(linestring) #printTabbed(linestring, 1) else: break proc.wait() if proc.returncode > 0: raise GitException("Git executable returned an error") os.chdir(currentdir) return gitlog expectedpattern = re.compile("^.* +(?:[gG][iI][sS][ _-]*|[tT]rac \#*|\#\s*)(\d+):*(.*)$") revertpattern = re.compile('^(\S*) Revert " *GIS-(\d+):* (.*)"$') mergepattern = re.compile("^(.*)((?:Merge branch '.*'|Merge remote-tracking branch '.*'|Merge commit '.*'|Add '.*' from commit).*)$") def processLog(gitlog): reverts = [] merges = {} other = [] numbered = {} for line in gitlog: expectedmatch = expectedpattern.match(line) revertmatch = revertpattern.match(line) mergematch = mergepattern.match(line) if expectedmatch == None and revertmatch == None and mergematch == None: other.append(line) if revertmatch: gisnumber = "GIS-" + revertmatch.group(2) description = revertmatch.group(3) reverts.append("Revert: {0} {1}".format(gisnumber, description)) if mergematch: merge = mergematch.group(2) if not merge in merges: merges[merge] = 1 else: merges[merge] += 1 if expectedmatch: gisnumber = "GIS-" + expectedmatch.group(1) description = expectedmatch.group(2) if not gisnumber in numbered: numbered[gisnumber] = [] numbered[gisnumber].append(description) return (reverts, merges, other, numbered) def showDifferences(differences): (revertslog, mergeslog, otherlog, numberedlog) = differences printDivider() printTabbed("GIS-Numbers Log:") for (gisnumber, descriptions) in sorted(numberedlog.items()): printTabbed(gisnumber, 1) descriptions = numberedlog[gisnumber]; for description in descriptions: printTabbed(description, 2) printDivider() printTabbed("Revert log:") for reversion in sorted(revertslog): printTabbed(reversion, 1) printDivider() printTabbed("Merges log:") for merge in sorted(mergeslog): printTabbed(merge, 1) printDivider() printTabbed("Other log:") for other in sorted(otherlog): printTabbed(other, 1) def main(): workingPath = WORKING_PATH previous = PREVIOUS_RELEASE now = CURRENT_RELEASE printDivider(times = 4) printTabbed("Looking for changes in {2} that are not in {1} of the repository at path: {0}".format(workingPath, previous, now)) gitlog = getGitLogDifferences(workingPath, previous, now) showDifferences(processLog(gitlog)) if 'opposite' in DIFFERENCE_OPTIONS.lower(): printDivider(times = 4) printTabbed("Looking for changes in {2} that are not in {1} of the repository at path: {0}".format(workingPath, now, previous)) gitlog = getGitLogDifferences(workingPath, now, previous) showDifferences(processLog(gitlog)) if 'notcommon' in DIFFERENCE_OPTIONS.lower(): printDivider(times = 4) printTabbed("Looking for changes that are not common to {1} or {2} at path: {0}".format(workingPath, now, previous)) gitlog = getGitLogDifferences(workingPath, now, previous, tripledot = True) showDifferences(processLog(gitlog)) sys.exit(0) if __name__ == '__main__': main()