From c417076fb338ff544129a8211d588aa06fe20e46 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Thu, 28 Feb 2013 15:14:34 +0000 Subject: B&M: Commit to build branches with user's email While it may be useful to have the username and machine the commit was made on, it's more useful to have the committer's email address, and the email field is for email addresses. --- morphlib/plugins/branch_and_merge_plugin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index cbc41acf..cb109676 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -1629,8 +1629,7 @@ class BranchAndMergePlugin(cliapp.Plugin): # Define the committer. committer_name = 'Morph (on behalf of %s)' % \ (morphlib.git.get_user_name(self.app.runcmd)) - committer_email = '%s@%s' % \ - (os.environ.get('LOGNAME'), socket.gethostname()) + committer_email = morphlib.git.get_user_email(self.app.runcmd) for repo, info in build_repos.iteritems(): repo_dir = info['dirname'] -- cgit v1.2.1 From d884a94133ae4b78138cb0a1d9f1ccfcc0061b1e Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Thu, 28 Feb 2013 15:20:51 +0000 Subject: B&M: Check git config before some subcommands Some subcommands use git to create commits, in which case user config needs to be set. Others imply commits may be created, e.g. by cloning a repository, at which point it is useful to have a reminder that the configuration needs to be set. --- morphlib/git.py | 17 +++++++++++++++++ morphlib/plugins/branch_and_merge_plugin.py | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/morphlib/git.py b/morphlib/git.py index c63c21b2..fffc05d0 100644 --- a/morphlib/git.py +++ b/morphlib/git.py @@ -183,6 +183,23 @@ def get_user_email(runcmd): ' git config --global user.email "me@example.com"\n') +def check_config_set(runcmd, keys=("user.name", "user.email"), cwd='.'): + ''' Check whether the given keys have values in git config. ''' + missing = [] + for key in keys: + try: + runcmd(['git', 'config', key], cwd=cwd) + except cliapp.AppException: + missing.append(key) + if missing: + if len(missing) == 1: + emesg = 'Git configuration for %s has not been set' % missing[0] + else: + emesg = ('Git configuration for keys %s and %s have not been set' + % (', '.join(missing[:-1]), missing[-1])) + raise cliapp.AppException(emesg) + + def set_remote(runcmd, gitdir, name, url): '''Set remote with name 'name' use a given url at gitdir''' return runcmd(['git', 'remote', 'set-url', name, url], cwd=gitdir) diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index cb109676..cc221e85 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -16,6 +16,7 @@ import cliapp import copy +import functools import glob import logging import os @@ -28,6 +29,22 @@ import uuid import morphlib + +def requires_git_config(*keys): + def decorator(func): + @functools.wraps(func) + def check_config(self, *args, **kwargs): + try: + morphlib.git.check_config_set(self.app.runcmd, keys) + except cliapp.AppException, e: + self.app.status(msg="WARNING: %(message)s", + message=e.msg, error=True) + return func(self, *args, **kwargs) + return check_config + + return decorator + + class BranchAndMergePlugin(cliapp.Plugin): def __init__(self): @@ -571,6 +588,7 @@ class BranchAndMergePlugin(cliapp.Plugin): self.remove_branch_dir_safe(workspace, branch_name) raise + @requires_git_config('user.name', 'user.email') def branch(self, args): '''Create a new system branch.''' @@ -591,6 +609,7 @@ class BranchAndMergePlugin(cliapp.Plugin): workspace = self.deduce_workspace() self._create_branch(workspace, new_branch, repo, commit) + @requires_git_config('user.name', 'user.email') def checkout(self, args): '''Check out an existing system branch.''' @@ -662,6 +681,7 @@ class BranchAndMergePlugin(cliapp.Plugin): branch_dir, spec['repo'], branch, parent_ref=spec['ref']) return repo_dir + @requires_git_config('user.name', 'user.email') def edit(self, args): '''Edit a component in a system branch.''' @@ -982,6 +1002,7 @@ class BranchAndMergePlugin(cliapp.Plugin): self.print_changelog('The following changes were made but have not ' 'been committed') + @requires_git_config('user.name', 'user.email') def tag(self, args): if len(args) < 1: raise cliapp.AppException('morph tag expects a tag name') @@ -1481,6 +1502,7 @@ class BranchAndMergePlugin(cliapp.Plugin): self.reset_work_tree_safe(repo_dir) raise + @requires_git_config('user.name', 'user.email') def build(self, args): '''Build a system from the current system branch''' -- cgit v1.2.1