Skip to content

Commit

Permalink
Add ability to define template to copy-head-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liyanage committed Feb 4, 2017
1 parent 08f13f7 commit 5341019
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions githelper/githelper.py
Expand Up @@ -933,12 +933,43 @@ class SubcommandCopyHeadCommitHash(AbstractSubcommand):
"""Copy repository / branch / head hash to clipboard"""

def __call__(self, wc):
output = '{} {} {}'.format(wc.current_repository(), wc.current_branch(), wc.head_commit_hash())
self.write_string_to_clipboard(output)
print output
template_name = self.args.template
if template_name:
config_variable = 'githelper.copy-template-' + template_name
output = wc.output_for_git_command(['git', 'config', config_variable])
if not output:
print >> sys.stderr, 'No template found for git configuration variable "{}"'.format(config_variable)
return GitWorkingCopy.STOP_TRAVERSAL
else:
config_variable = 'githelper.copy-template'
output = wc.output_for_git_command(['git', 'config', config_variable])
if not output:
output = ['{repository} {branch} {commit}']

output = self.interpolate_data_into_template_lines(wc, output)
output_string = ''.join([l + '\n' for l in output])

self.write_string_to_clipboard(output_string)
print output_string,

return GitWorkingCopy.STOP_TRAVERSAL

def interpolate_data_into_template_lines(self, wc, template_lines):
repository, branch, commit = wc.current_repository(), wc.current_branch(), wc.head_commit_hash()
data = dict(zip('repository branch commit'.split(), (repository, branch, commit)))
output = []
for line in template_lines:
for key, value in data.items():
token = '{' + key + '}'
if token in line:
line = line.replace(token, value)
output.append(line)
return output

@classmethod
def configure_argument_parser(cls, parser):
parser.add_argument('template', nargs='?', default=None, help='Optional name of a template stored with "git config githelper.copy-template-<name>". If omitted, defaults to the value of githelper.copy-template, and if that is not set, defaults to a built-in template. You can use the placeholders "{branch}", "{repository}", and "{commit}"')


class SubcommandCheckoutBugfixBranch(AbstractSubcommand):
"""Check out a bugfix branch named user/<username>/name, with name based on the contents of the clipboard."""
Expand Down

0 comments on commit 5341019

Please sign in to comment.