chiark / gitweb /
Make the "name" argument to "stg new" optional
authorKarl Hasselström <kha@treskal.com>
Tue, 15 May 2007 17:09:59 +0000 (18:09 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Tue, 15 May 2007 21:13:14 +0000 (22:13 +0100)
If no name is given, one is generated from the commit message. This
can be very handy.

Signed-off-by: Karl Hasselström <kha@treskal.com>
Documentation/stg-new.txt
stgit/commands/common.py
stgit/commands/new.py
stgit/commands/pick.py
stgit/commands/uncommit.py
stgit/stack.py
stgit/utils.py
t/t1003-new.sh [new file with mode: 0644]

index 009659af82eeb5a08657e2f212233f2fd52b8621..fbf2f671e9fb5cc38dcd578f9e49e9204c247463 100644 (file)
@@ -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
index 674d8c10fb99878c2cc358537a3af4e249c9ada5..28026da94ac19aca330fd7a2652ba5761c988553 100644 (file)
@@ -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.
index 2c1e94b92d3afbe299348f3894907ec6f7ced1bf..f192e34ee19d5d4bec0818f9c91a3579795c8338 100644 (file)
@@ -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,
index 4ef9860f60278095a02e2a8594f11b4ffbef61f5..ea0756bcad0379b7e7f945e0d3a73ba54f3bc6bf 100644 (file)
@@ -76,7 +76,7 @@ def func(parser, options, args):
         elif len(patch_branch) == 2:
             patchname = patch_branch[0]
         else:
-            patchname = make_patch_name(commit.get_log(), crt_series.patch_exists)
+            patchname = None
 
     if options.parent:
         parent = git_id(options.parent)
index 462846cc2160379698e44297c90aeed996555e59..04c7e52e24b7a9cd5b08a9cb68f69efde732a987 100644 (file)
@@ -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,
index 65a7d83e741a87cb5c584282bb540769ba547d4c..ef3d44759bbc145df16552cef8e8b159e81597c6 100644 (file)
@@ -781,20 +781,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()
 
index d04d077d457f75d805ba64cdb069b1a4ddb32615..18198c00831999e75a59962307a02beafe72e731 100644 (file)
@@ -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
diff --git a/t/t1003-new.sh b/t/t1003-new.sh
new file mode 100644 (file)
index 0000000..0be5d9b
--- /dev/null
@@ -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