File indexing completed on 2024-04-21 04:39:50

0001 #!/usr/bin/env python3
0002 
0003 # stolen from releaseme/plasma, which was stolen from release-tools Applications/15.04 branch
0004 # ported to Python 3 and fixed the worst issues + removed Plasma-related bits --Kevin
0005 
0006 from __future__ import print_function
0007 
0008 import argparse
0009 import os
0010 import subprocess
0011 import sys
0012 import html
0013 
0014 
0015 def createLog(workingDir, fromVersion, toVersion, excludeBranch=None, repositoryName=None, showInterestingChangesOnly=True):
0016     if not repositoryName:
0017         # use cwd name as repository name
0018         repositoryName = os.path.split(workingDir)[1]
0019 
0020     p = subprocess.Popen('git fetch', shell=True, cwd=workingDir,
0021                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0022     if p.wait() != 0:
0023         raise NameError('git fetch failed')
0024 
0025     p = subprocess.Popen('git rev-parse ' + fromVersion + ' ' + toVersion, shell=True, cwd=workingDir,
0026                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0027     if p.wait() != 0:
0028         raise NameError("git rev-parse failed -- correct to/from version?")
0029 
0030     branchToExclude = ("^" + excludeBranch + " ") if excludeBranch else ""
0031     p = subprocess.Popen('git log ' + branchToExclude + fromVersion + '...' + toVersion, shell=True, cwd=workingDir,
0032                          stdout=subprocess.PIPE, universal_newlines=True)
0033     commit = []
0034     commits = []
0035     for line in p.stdout:
0036         if line.startswith("commit"):
0037             if len(commit) > 1 and not ignoreCommit:
0038                 commits.append(commit)
0039             commitHash = line[7:].strip()
0040             ignoreCommit = False
0041             commit = [commitHash]
0042         elif line.startswith("Author"):
0043             pass
0044         elif line.startswith("Date"):
0045             pass
0046         elif line.startswith("Merge"):
0047             pass
0048         else:
0049             line = line.strip()
0050             if line.startswith("Merge remote-tracking branch"):
0051                 ignoreCommit = True
0052             elif line.startswith("SVN_SILENT"):
0053                 ignoreCommit = True
0054             elif line.startswith("GIT_SILENT"):
0055                 ignoreCommit = True
0056             elif line.startswith("Merge branch"):
0057                 ignoreCommit = True
0058             elif line.startswith("Update version number for"):
0059                 ignoreCommit = True
0060             elif line:
0061                 commit.append(line)
0062     # Add the last commit
0063     if len(commit) > 1 and not ignoreCommit:
0064         commits.append(commit)
0065 
0066     commitLogEntries = []
0067     if len(commits) > 0:
0068         for commit in commits:
0069             extra = ""
0070             changelog = commit[1]
0071 
0072             for line in commit:
0073                 line = html.escape(line)
0074                 if line.startswith("BUGS:"):
0075                     bugNumbers = line[line.find(":") + 1:].strip()
0076                     for bugNumber in bugNumbers.split(","):
0077                         if bugNumber.isdigit():
0078                             if extra:
0079                                 extra += ". "
0080                             extra += "fixes bug <a href='https://bugs.kde.org/" + bugNumber + "'>#" + bugNumber + "</a>"
0081                 elif line.startswith("BUG:"):
0082                     bugNumber = line[line.find(":") + 1:].strip()
0083                     if bugNumber.isdigit():
0084                         if extra:
0085                             extra += ". "
0086                         extra += "fixes bug <a href='https://bugs.kde.org/" + bugNumber + "'>#" + bugNumber + "</a>"
0087                 elif line.startswith("REVIEW:"):
0088                     if extra:
0089                         extra += ". "
0090                     reviewNumber = line[line.find(":") + 1:].strip()
0091                     extra += "code review <a href='https://git.reviewboard.kde.org/r/" + reviewNumber + "'>#" + reviewNumber + "</a>"
0092                     # jr addition 2017-02 phab link
0093                 elif line.startswith("Differential Revision:"):
0094                     if extra:
0095                         extra += ". "
0096                     reviewNumber = line[line.find("org/") + 4:].strip()
0097                     extra += "code review <a href='https://phabricator.kde.org/" + reviewNumber + "'>" + reviewNumber + "</a>"
0098                 elif line.startswith("CCBUG:"):
0099                     if extra:
0100                         extra += ". "
0101                     bugNumber = line[line.find(":") + 1:].strip()
0102                     extra += "See bug <a href='https://bugs.kde.org/" + bugNumber + "'>#" + bugNumber + "</a>"
0103                 elif line.startswith("FEATURE:"):
0104                     feature = line[line.find(":") + 1:].strip()
0105                     if feature.isdigit():
0106                         if extra:
0107                             extra += ". "
0108                         extra += "Implements feature <a href='https://bugs.kde.org/" + feature + "'>#" + feature + "</a>"
0109                     else:
0110                         if feature:
0111                             changelog = feature
0112 
0113                 elif line.startswith("CHANGELOG:"):
0114                     changelog = line[11:]  # remove word "CHANGELOG: "
0115                 elif line.startswith("Merge Plasma"):
0116                     pass
0117 
0118             if showInterestingChangesOnly and not extra:
0119                 continue
0120 
0121             commitHash = commit[0]
0122             if not changelog.endswith("."):
0123                 changelog = changelog + "."
0124             if extra:
0125                 extra = ". " + extra
0126 
0127             capitalizedChangelog = changelog[0].capitalize() + changelog[1:]
0128             commitLogEntries.append("<li>" + capitalizedChangelog + " (<a href='https://commits.kde.org/" + repositoryName + "/" + commitHash + "'>commit</a>" + extra + ")</li>")
0129 
0130     # Print result to stdout
0131     print("<h3><a name='" + repositoryName + "' href='https://commits.kde.org/" + repositoryName + "'>" + repositoryName + "</a></h3>")
0132     if len(commitLogEntries) > 0:
0133         print("<ul id='ul" + repositoryName + "' style='display: block'>")
0134         for commitLogEntry in commitLogEntries:
0135             print(commitLogEntry)
0136         print("</ul>\n\n")
0137     else:
0138         print("<em>No changes</em>")
0139 
0140 
0141     if p.wait() != 0:
0142         raise NameError('git log failed', repositoryName, fromVersion, toVersion)
0143 
0144 
0145 if __name__ == "__main__":
0146     parser = argparse.ArgumentParser(description='Create HTML log based on Git history in the current working directory')
0147     parser.add_argument('--repositoryName', type=str, help='The path to the Git repositoryNamesitory (default: name of current working dir', default=None)
0148     parser.add_argument('from_version', type=str, help='The start of the revision range (e.g. "v5.5.0")')
0149     parser.add_argument('to_version', type=str, help='The end of the revision range (e.g. "v5.5.1")')
0150     parser.add_argument('--excludeBranch', type=str, help='The old branch to ignore (e.g. "5.4")', default=None)
0151     args = parser.parse_args()
0152 
0153     createLog(os.getcwd(), args.from_version, args.to_version, args.excludeBranch)