Some tweaks and enhancements
cover Some tweaks and enhancements
The first and last patches of this series implements the things Petr asked for (automatic patch naming with "stg new", and uncommitting to a specified committish). Number 2 and 3 are small cleanups; 4 is a refactoring that's useful in its own right, but also makes it easier to do 5.
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
PATCH 1/5 Make the "name" argument to "stg new" optional
From: Karl Hasselström <[email protected]>
If no name is given, one is generated from the commit message. This can be very handy.
---
Documentation/stg-new.txt | 5 +++-- stgit/commands/common.py | 25 ------------------------- stgit/commands/new.py | 15 +++++++++++---- stgit/commands/pick.py | 2 +- stgit/commands/uncommit.py | 3 +-- stgit/stack.py | 17 +++++++++++------ stgit/utils.py | 27 ++++++++++++++++++++++++++- t/t1003-new.sh | 29 +++++++++++++++++++++++++++++ 8 files changed, 82 insertions(+), 41 deletions(-)
Documentation/stg-new.txt +3 -2
@@ -10,7 +10,7 @@ stg-new - stgdesc:new[] SYNOPSIS -------- [verse] -'stg' new [OPTIONS] <name> +'stg' new [OPTIONS] [name] DESCRIPTION -----------
@@ -22,7 +22,8 @@ tree are not included in the patch. A stglink:refresh[] command is needed for this. The given <name> must be unique in the stack, and may only contain -alphanumeric characters, dashes and underscores. +alphanumeric characters, dashes and underscores. If no name is given, +one is generated from the first line of the commit message. An editor will be launched to edit the commit message to be used for the patch, unless the '--message' flag already specified one. The
stgit/commands/common.py +0 -25
@@ -323,31 +323,6 @@ def address_or_alias(addr_str): for addr in addr_str.split(',')] return ', '.join([addr for addr in addr_list if addr]) -def patch_name_from_msg(msg): - """Return a string to be used as a patch name. This is generated - from the first 30 characters of the top line of the string passed - as argument.""" - if not msg: - return None - - subject_line = msg[:30].lstrip().split('\n', 1)[0].lower() - return re.sub('[\W]+', '-', subject_line).strip('-') - -def make_patch_name(msg, unacceptable, default_name = 'patch', - alternative = True): - """Return a patch name generated from the given commit message, - guaranteed to make unacceptable(name) be false. If the commit - message is empty, base the name on default_name instead.""" - patchname = patch_name_from_msg(msg) - if not patchname: - patchname = default_name - if alternative and unacceptable(patchname): - suffix = 0 - while unacceptable('%s-%d' % (patchname, suffix)): - suffix += 1 - patchname = '%s-%d' % (patchname, suffix) - return patchname - def prepare_rebase(real_rebase, force=None): if not force: # Be sure we won't loose results of stg-(un)commit by error.
stgit/commands/new.py +11 -4
@@ -25,7 +25,7 @@ from stgit import stack, git help = 'create a new patch and make it the topmost one' -usage = """%prog [options] <name> +usage = """%prog [options] [name] Create a new, empty patch and make it the topmost one. If the '--message' option is not passed, an editor is invoked with the
@@ -33,7 +33,10 @@ Create a new, empty patch and make it the topmost one. If the /usr/share/stgit/templates/patchdescr.tmpl file used a as template, together with generated lines. By default, the local changes in the working tree are not included in the patch. A 'refresh' command is -needed for this.""" +needed for this. + +If no name is given for the new patch, one is generated from the first +line of the commit message.""" options = [make_option('-m', '--message', help = 'use MESSAGE as the patch description'),
@@ -57,7 +60,11 @@ options = [make_option('-m', '--message', def func(parser, options, args): """Creates a new patch """ - if len(args) != 1: + if len(args) == 0: + name = None # autogenerate a name + elif len(args) == 1: + name = args[0] + else: parser.error('incorrect number of arguments') check_conflicts()
@@ -66,7 +73,7 @@ def func(parser, options, args): if options.author: options.authname, options.authemail = name_email(options.author) - crt_series.new_patch(args[0], message = options.message, + crt_series.new_patch(name, message = options.message, show_patch = options.showpatch, author_name = options.authname, author_email = options.authemail,
stgit/commands/pick.py +1 -1
@@ -75,7 +75,7 @@ def func(parser, options, args): elif len(patch_branch) == 2: patch = patch_branch[0] else: - patch = make_patch_name(commit.get_log(), crt_series.patch_exists) + patch = None if options.parent: parent = git_id(options.parent)
stgit/commands/uncommit.py +1 -2
@@ -95,8 +95,7 @@ def func(parser, options, args): if patchnames: patchname = patchnames[n] else: - patchname = make_patch_name(commit.get_log(), - crt_series.patch_exists) + patchname = None crt_series.new_patch(patchname, can_edit = False, before_existing = True,
stgit/stack.py +11 -6
@@ -777,20 +777,25 @@ class Series(StgitObject): before_existing = False, refresh = True): """Creates a new patch """ - self.__patch_name_valid(name) - if self.patch_applied(name) or self.patch_unapplied(name): - raise StackException, 'Patch "%s" already exists' % name + if name != None: + self.__patch_name_valid(name) + if self.patch_applied(name) or self.patch_unapplied(name): + raise StackException, 'Patch "%s" already exists' % name if not message and can_edit: - descr = edit_file(self, None, \ - 'Please enter the description for patch "%s" ' \ - 'above.' % name, show_patch) + descr = edit_file( + self, None, + 'Please enter the description for the patch above.', + show_patch) else: descr = message head = git.get_head() + if name == None: + name = make_patch_name(descr, self.patch_exists) + patch = Patch(name, self.__patch_dir, self.__refs_dir) patch.create()
stgit/utils.py +26 -1
@@ -1,7 +1,7 @@ """Common utility functions """ -import errno, os, os.path, sys +import errno, os, os.path, re, sys from stgit.config import config __copyright__ = """
@@ -172,3 +172,28 @@ def call_editor(filename): if err: raise EditorException, 'editor failed, exit code: %d' % err print 'done' + +def patch_name_from_msg(msg): + """Return a string to be used as a patch name. This is generated + from the first 30 characters of the top line of the string passed + as argument.""" + if not msg: + return None + + subject_line = msg[:30].lstrip().split('\n', 1)[0].lower() + return re.sub('[\W]+', '-', subject_line).strip('-') + +def make_patch_name(msg, unacceptable, default_name = 'patch', + alternative = True): + """Return a patch name generated from the given commit message, + guaranteed to make unacceptable(name) be false. If the commit + message is empty, base the name on default_name instead.""" + patchname = patch_name_from_msg(msg) + if not patchname: + patchname = default_name + if alternative and unacceptable(patchname): + suffix = 0 + while unacceptable('%s-%d' % (patchname, suffix)): + suffix += 1 + patchname = '%s-%d' % (patchname, suffix) + return patchname
t/t1003-new.sh new file +29 -0
@@ -0,0 +1,29 @@ +#!/bin/sh +# +# Copyright (c) 2007 Karl Hasselström +# + +test_description='Test the new command. + +' + +. ./test-lib.sh + +test_expect_success \ + 'Initialize the StGIT repository' ' + stg init +' + +test_expect_success \ + 'Create a named patch' ' + stg new foo -m foobar && + [ $(stg applied -c) -eq 1 ] +' + +test_expect_success \ + 'Create a patch without giving a name' ' + stg new -m yo && + [ $(stg applied -c) -eq 2 ] +' + +test_done
PATCH 2/5 Generate patch names of more uniform length
In reply to Karl HasselströmFrom: Karl Hasselström <[email protected]>
Cut the generated patch name at 30 characters after getting rid of unwanted characters, not before. This gives patch names of more uniform length, since consecutive unwanted characters were replaced by a single dash.
---
stgit/utils.py | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
stgit/utils.py +4 -4
@@ -175,13 +175,13 @@ def call_editor(filename): def patch_name_from_msg(msg): """Return a string to be used as a patch name. This is generated - from the first 30 characters of the top line of the string passed - as argument.""" + from the top line of the string passed as argument, and is at most + 30 characters long.""" if not msg: return None - subject_line = msg[:30].lstrip().split('\n', 1)[0].lower() - return re.sub('[\W]+', '-', subject_line).strip('-') + subject_line = msg.split('\n', 1)[0].lstrip().lower() + return re.sub('[\W]+', '-', subject_line).strip('-')[:30] def make_patch_name(msg, unacceptable, default_name = 'patch', alternative = True):
PATCH 3/5 Remove an unnecessary parameter to make_patch_name
In reply to Karl HasselströmFrom: Karl Hasselström <[email protected]>
The "alternative" parameter was a boolean that indicated whether we were interested in testing if the first generated name was acceptable, or if we would just always accept it. But that can be accomplished by giving an "unacceptable" function that always returns False, so there's no need for an additional parameter.
---
stgit/commands/imprt.py | 8 +++++--- stgit/utils.py | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-)
stgit/commands/imprt.py +5 -3
@@ -242,9 +242,11 @@ def __create_patch(filename, message, author_name, author_email, patch = __strip_patch_name(patch) if not patch: - patch = make_patch_name(message, crt_series.patch_exists, - alternative = not (options.ignore - or options.replace)) + if options.ignore or options.replace: + unacceptable_name = lambda name: False + else: + unacceptable_name = crt_series.patch_exists + patch = make_patch_name(message, unacceptable_name) else: # fix possible invalid characters in the patch name patch = re.sub('[^\w.]+', '-', patch).strip('-')
stgit/utils.py +2 -3
@@ -183,15 +183,14 @@ def patch_name_from_msg(msg): subject_line = msg.split('\n', 1)[0].lstrip().lower() return re.sub('[\W]+', '-', subject_line).strip('-')[:30] -def make_patch_name(msg, unacceptable, default_name = 'patch', - alternative = True): +def make_patch_name(msg, unacceptable, default_name = 'patch'): """Return a patch name generated from the given commit message, guaranteed to make unacceptable(name) be false. If the commit message is empty, base the name on default_name instead.""" patchname = patch_name_from_msg(msg) if not patchname: patchname = default_name - if alternative and unacceptable(patchname): + if unacceptable(patchname): suffix = 0 while unacceptable('%s-%d' % (patchname, suffix)): suffix += 1
PATCH 4/5 If any uncommit would fail, don't uncommit anything
In reply to Karl HasselströmFrom: Karl Hasselström <[email protected]>
---
stgit/commands/uncommit.py | 26 +++++++++++++++----------- 1 files changed, 15 insertions(+), 11 deletions(-)
stgit/commands/uncommit.py +15 -11
@@ -80,23 +80,26 @@ def func(parser, options, args): print 'Uncommitting %d patches...' % patch_nr, sys.stdout.flush() - for n in xrange(0, patch_nr): - # retrieve the commit (only commits with a single parent are allowed) - commit_id = crt_series.get_base() + def get_commit(commit_id): commit = git.Commit(commit_id) try: parent, = commit.get_parents() except ValueError: - raise CmdException, 'Commit %s does not have exactly one parent' \ - % commit_id + raise CmdException('Commit %s does not have exactly one parent' + % commit_id) + return (commit, commit_id, parent) + + commits = [] + next_commit = crt_series.get_base() + for i in xrange(patch_nr): + commit, commit_id, parent = get_commit(next_commit) + commits.append((commit, commit_id, parent)) + next_commit = parent + + for (commit, commit_id, parent), patchname in \ + zip(commits, patchnames or (None for i in xrange(len(commits)))): author_name, author_email, author_date = \ name_email_date(commit.get_author()) - - if patchnames: - patchname = patchnames[n] - else: - patchname = None - crt_series.new_patch(patchname, can_edit = False, before_existing = True, top = commit_id, bottom = parent,
@@ -105,4 +108,5 @@ def func(parser, options, args): author_email = author_email, author_date = author_date) + print 'done'
PATCH 5/5 Uncommit to a named commit
In reply to Karl HasselströmFrom: Karl Hasselström <[email protected]>
Add a new flag to "stg uncommit": --to. This flag takes a committish parameter, and uncommits everything up until that commit.
---
stgit/commands/uncommit.py | 49 +++++++++++++++++++++++++++++++------------- t/t1300-uncommit.sh | 8 +++++++ 2 files changed, 42 insertions(+), 15 deletions(-)
stgit/commands/uncommit.py +34 -15
@@ -25,7 +25,7 @@ from stgit.utils import * from stgit import stack, git help = 'turn regular GIT commits into StGIT patches' -usage = """%prog [<patchname1> [<patchname2> ... ] | -n [<prefix>]] +usage = """%prog [<patchnames>] | -n NUM [<prefix>]] | -t <committish> Take one or more git commits at the base of the current stack and turn them into StGIT patches. The new patches are created as applied patches
@@ -35,24 +35,34 @@ By default, the number of patches to uncommit is determined by the number of patch names provided on the command line. First name is used for the first patch to uncommit, i.e. for the newest patch. -The --number option specifies the number of patches to uncommit. In +The -n/--number option specifies the number of patches to uncommit. In this case, at most one patch name may be specified. It is used as -prefix to which the patch number is appended. +prefix to which the patch number is appended. If no patch names are +provided on the command line, StGIT automatically generates them based +on the first line of the patch description. -If no patch names are provided on the command line, StGIT -automatically generates them based on the first line of the patch -description. +The -t/--to option specifies that all commits up to and including the +given commit should be uncommitted. Only commits with exactly one parent can be uncommitted; in other words, you can't uncommit a merge.""" options = [make_option('-n', '--number', type = 'int', - help = 'uncommit the specified number of commits')] + help = 'uncommit the specified number of commits'), + make_option('-t', '--to', + help = 'uncommit to the specified commit')] def func(parser, options, args): """Uncommit a number of patches. """ - if options.number: + if options.to: + if options.number: + parser.error('cannot give both --to and --number') + if len(args) != 0: + parser.error('cannot specify patch name with --to') + patch_nr = patchnames = None + to_commit = git.rev_parse(options.to) + elif options.number: if options.number <= 0: parser.error('invalid value passed to --number')
@@ -77,9 +87,6 @@ def func(parser, options, args): raise CmdException, \ 'This branch is protected. Uncommit is not permitted' - print 'Uncommitting %d patches...' % patch_nr, - sys.stdout.flush() - def get_commit(commit_id): commit = git.Commit(commit_id) try:
@@ -91,11 +98,23 @@ def func(parser, options, args): commits = [] next_commit = crt_series.get_base() - for i in xrange(patch_nr): - commit, commit_id, parent = get_commit(next_commit) - commits.append((commit, commit_id, parent)) - next_commit = parent + if patch_nr: + print 'Uncommitting %d patches...' % patch_nr, + for i in xrange(patch_nr): + commit, commit_id, parent = get_commit(next_commit) + commits.append((commit, commit_id, parent)) + next_commit = parent + else: + print 'Uncommitting to %s...' % to_commit + while True: + commit, commit_id, parent = get_commit(next_commit) + commits.append((commit, commit_id, parent)) + if commit_id == to_commit: + break + next_commit = parent + patch_nr = len(commits) + sys.stdout.flush() for (commit, commit_id, parent), patchname in \ zip(commits, patchnames or (None for i in xrange(len(commits)))): author_name, author_email, author_date = \
t/t1300-uncommit.sh +8 -0
@@ -70,4 +70,12 @@ test_expect_success \ [ "$(stg id foo-patch//top)" = "$(stg id bar-patch//bottom)" ] && stg commit ' + +test_expect_success \ + 'Uncommit the patches with --to' ' + stg uncommit --to HEAD^ && + [ "$(stg id foo-patch//top)" = "$(stg id bar-patch//bottom)" ] && + stg commit +' + test_done