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(+) (limited to 'morphlib') 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