From 55fba0e0dd15832ad65155aa2edcb160e126a67f Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 27 Sep 2013 20:54:53 +0800 Subject: [PATCH] Tracker update smartly resolves the head commit --- config-dist.json | 8 ++++++ lib/commands/push.py | 11 +++++--- lib/moodle.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/config-dist.json b/config-dist.json index 0fb4aa8..4734fad 100644 --- a/config-dist.json +++ b/config-dist.json @@ -158,6 +158,14 @@ // Debug level of MDK. 'debug', 'info', 'warning', 'error' or 'critical'. "debug": "info", + // When enabled, MDK will try to be smart to identify the parent commit of the current branch. Instead of + // using the stable branch it will match the branch name with an issue number and compare it with the commit + // messages. The first commit message that does not match the pattern MDL-12345 will be used as the headcommit. + "smartHeadCommitSearch": true, + + // Limit the search of the head commit to the last n commits. + "smartHeadCommitLimit": 50, + // Name of the symlink pointing to the data directory to create in the www directory // during an instance creation. If false, the symlink won't be created. "symlinkToData": false, diff --git a/lib/commands/push.py b/lib/commands/push.py index 1077568..0f1d398 100644 --- a/lib/commands/push.py +++ b/lib/commands/push.py @@ -59,9 +59,11 @@ class PushCommand(Command): ( ['-t', '--update-tracker'], { - 'action': 'store_true', + 'const': True, 'dest': 'updatetracker', - 'help': 'also add the diff information to the tracker issue.' + 'help': 'also add the diff information to the tracker issue. If gitref is passed, it is used as a starting point for the diff URL.', + 'metavar': 'gitref', + 'nargs': '?' } ), ( @@ -132,8 +134,9 @@ class PushCommand(Command): raise Exception(result[2]) # Update the tracker - if args.updatetracker: - M.updateTrackerGitInfo(branch=branch) + if args.updatetracker != None: + ref = None if args.updatetracker == True else args.updatetracker + M.updateTrackerGitInfo(branch=branch, ref=ref) # Pushing stable branch if args.includestable: diff --git a/lib/moodle.py b/lib/moodle.py index 13056a3..81d3b4c 100644 --- a/lib/moodle.py +++ b/lib/moodle.py @@ -30,7 +30,7 @@ import shutil from tools import mkdir, process, parseBranch from db import DB from config import Conf -from git import Git +from git import Git, GitException from exceptions import InstallException from jira import Jira from scripts import Scripts @@ -231,6 +231,55 @@ class Moodle(object): raise Exception('Could not find the Git repository') return self._git + def headcommit(self, branch=None): + """Try to resolve the head commit of branch of this instance""" + + if branch == None: + branch = self.currentBranch() + if branch == 'HEAD': + raise Exception('Cannot update the tracker when on detached branch') + + smartSearch = C.get('smartHeadCommitSearch') + + # Parsing the branch + parsedbranch = parseBranch(branch, C.get('wording.branchRegex')) + if parsedbranch: + issue = 'MDL-%s' % (parsedbranch['issue']) + else: + logging.debug('Cannot smart resolve using the branch %s' % (branch)) + smartSearch = False + + headcommit = None + try: + # Trying to smart guess the last commit needed + if smartSearch: + commits = self.git().log(since=branch, count=C.get('smartHeadCommitLimit'), format='%s_____%h').split('\n')[:-1] + + # Looping over the last commits to find the commit messages that match the MDL-12345. + candidate = None + for commit in commits: + match = commit.strip().lower().startswith(issue.lower()) + if not candidate and not match: + # The first commit does not match a hash, let's ignore this method. + break + candidate = commit.split('_____')[-1] + if not match: + # The commit does not match any more, we found it! + headcommit = candidate + break + + # We could not smart find the last commit, let's use the default mechanism. + if not headcommit: + upstreamremote = C.get('upstreamRemote') + stablebranch = self.get('stablebranch') + headcommit = self.git().hashes(ref='%s/%s' % (upstreamremote, stablebranch), limit=1, format='%h')[0] + + except GitException: + logging.warning('Could not resolve the head commit') + headcommit = False + + return headcommit + def initPHPUnit(self, force=False): """Initialise the PHPUnit environment""" @@ -578,7 +627,7 @@ class Moodle(object): self.removeConfig(name) self.addConfig(name, value) - def updateTrackerGitInfo(self, branch=None): + def updateTrackerGitInfo(self, branch=None, ref=None): """Updates the git info on the tracker issue""" if branch == None: @@ -597,11 +646,27 @@ class Moodle(object): repositoryurl = C.get('repositoryUrl') diffurltemplate = C.get('diffUrlTemplate') stablebranch = self.get('stablebranch') - upstreamremote = C.get('upstreamRemote') # Get the hash of the last upstream commit - ref = '%s/%s' % (upstreamremote, stablebranch) - headcommit = self.git().hashes(ref=ref, limit=1, format='%h')[0] + headcommit = None + logging.info('Searching for the head commit...') + if ref: + try: + headcommit = self.git().hashes(ref=ref, limit=1, format='%h')[0] + except GitException: + logging.warning('Could not resolve a head commit using the reference: %s' % (ref)) + headcommit = None + + # No reference was passed, or it was invalid. + if not headcommit: + headcommit = self.headcommit(branch) + + # Head commit not resolved + if not headcommit: + logging.error('Head commit not resolved, aborting update of tracker fields') + return False + + logging.debug('Head commit resolved to %s' % (headcommit)) J = Jira() diffurl = diffurltemplate.replace('%branch%', branch).replace('%stablebranch%', stablebranch).replace('%headcommit%', headcommit) -- 2.11.0