summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-03-04 16:42:18 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-03-04 16:42:18 +0000
commit0f4db6b8f1cd2b2e94de0a5efeca7dc9be5fc12f (patch)
tree26433d1d56cd5f6eece19e5bc07d687ac03ef740
parentc11cdb16e2d10e29c96873db5796e911979012d8 (diff)
downloadmorph-0f4db6b8f1cd2b2e94de0a5efeca7dc9be5fc12f.tar.gz
B&M: Share error message formatting code
This makes the warning messages include how to fix the problem. morphlib.git.check_config_set will return the values of the keys which were set, so it can be used to get git config.
-rw-r--r--morphlib/git.py65
-rw-r--r--morphlib/plugins/branch_and_merge_plugin.py17
2 files changed, 54 insertions, 28 deletions
diff --git a/morphlib/git.py b/morphlib/git.py
index fffc05d0..c491991c 100644
--- a/morphlib/git.py
+++ b/morphlib/git.py
@@ -157,17 +157,45 @@ def update_submodules(app, repo_dir): # pragma: no cover
app.runcmd(['git', 'submodule', 'update'], cwd=repo_dir)
+class ConfigNotSetException(cliapp.AppException):
+
+ def __init__(self, missing, defaults):
+ self.missing = missing
+ self.defaults = defaults
+ if len(missing) == 1:
+ self.preamble = ('Git configuration for %s has not been set. '
+ 'Please set it with:' % missing[0])
+ else:
+ self.preamble = ('Git configuration for keys %s and %s '
+ 'have not been set. Please set them with:'
+ % (', '.join(missing[:-1]), missing[-1]))
+
+ def __str__(self):
+ lines = [self.preamble]
+ lines.extend('git config --global %s \'%s\'' % (k, self.defaults[k])
+ for k in self.missing)
+ return '\n '.join(lines)
+
+
+class IdentityNotSetException(ConfigNotSetException):
+
+ preamble = 'Git user info incomplete. Please set your identity, using:'
+
+ def __init__(self, missing):
+ self.defaults = {"user.name": "My Name",
+ "user.email": "me@example.com"}
+ self.missing = missing
+
+
def get_user_name(runcmd):
'''Get user.name configuration setting. Complain if none was found.'''
if 'GIT_AUTHOR_NAME' in os.environ:
return os.environ['GIT_AUTHOR_NAME'].strip()
try:
- return runcmd(['git', 'config', 'user.name']).strip()
- except cliapp.AppException:
- raise cliapp.AppException(
- 'No git user info found. Please set your identity, using: \n'
- ' git config --global user.name "My Name"\n'
- ' git config --global user.email "me@example.com"\n')
+ config = check_config_set(runcmd, keys={"user.name": "My Name"})
+ return config['user.name']
+ except ConfigNotSetException, e:
+ raise IdentityNotSetException(e.missing)
def get_user_email(runcmd):
@@ -175,29 +203,24 @@ def get_user_email(runcmd):
if 'GIT_AUTHOR_EMAIL' in os.environ:
return os.environ['GIT_AUTHOR_EMAIL'].strip()
try:
- return runcmd(['git', 'config', 'user.email']).strip()
- except cliapp.AppException:
- raise cliapp.AppException(
- 'No git user info found. Please set your identity, using: \n'
- ' git config --global user.email "My Name"\n'
- ' git config --global user.email "me@example.com"\n')
+ cfg = check_config_set(runcmd, keys={"user.email": "me@example.com"})
+ return cfg['user.email']
+ except ConfigNotSetException, e:
+ raise IdentityNotSetException(e.missing)
-
-def check_config_set(runcmd, keys=("user.name", "user.email"), cwd='.'):
+def check_config_set(runcmd, keys, cwd='.'):
''' Check whether the given keys have values in git config. '''
missing = []
+ found = {}
for key in keys:
try:
- runcmd(['git', 'config', key], cwd=cwd)
+ value = runcmd(['git', 'config', key], cwd=cwd).strip()
+ found[key] = value
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)
+ raise ConfigNotSetException(missing, keys)
+ return found
def set_remote(runcmd, gitdir, name, url):
diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py
index cc221e85..141c3187 100644
--- a/morphlib/plugins/branch_and_merge_plugin.py
+++ b/morphlib/plugins/branch_and_merge_plugin.py
@@ -30,7 +30,7 @@ import uuid
import morphlib
-def requires_git_config(*keys):
+def warns_git_config(keys):
def decorator(func):
@functools.wraps(func)
def check_config(self, *args, **kwargs):
@@ -38,13 +38,17 @@ def requires_git_config(*keys):
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)
+ message=str(e), error=True)
return func(self, *args, **kwargs)
return check_config
return decorator
+warns_git_identity = warns_git_config({'user.name': 'My Name',
+ 'user.email': 'me@example.com'})
+
+
class BranchAndMergePlugin(cliapp.Plugin):
def __init__(self):
@@ -588,7 +592,7 @@ class BranchAndMergePlugin(cliapp.Plugin):
self.remove_branch_dir_safe(workspace, branch_name)
raise
- @requires_git_config('user.name', 'user.email')
+ @warns_git_identity
def branch(self, args):
'''Create a new system branch.'''
@@ -609,7 +613,7 @@ class BranchAndMergePlugin(cliapp.Plugin):
workspace = self.deduce_workspace()
self._create_branch(workspace, new_branch, repo, commit)
- @requires_git_config('user.name', 'user.email')
+ @warns_git_identity
def checkout(self, args):
'''Check out an existing system branch.'''
@@ -681,7 +685,7 @@ class BranchAndMergePlugin(cliapp.Plugin):
branch_dir, spec['repo'], branch, parent_ref=spec['ref'])
return repo_dir
- @requires_git_config('user.name', 'user.email')
+ @warns_git_identity
def edit(self, args):
'''Edit a component in a system branch.'''
@@ -1002,7 +1006,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')
+ @warns_git_identity
def tag(self, args):
if len(args) < 1:
raise cliapp.AppException('morph tag expects a tag name')
@@ -1502,7 +1506,6 @@ 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'''