Skip to contentSkip to conversation

Generate saner automatic patch names

PATCH New Karl Hasselström 1 participant 3 messages Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

cover Generate saner automatic patch names

This series fixes some quirks in the automatic patch name generation. In particular, it limits the length of names, and makes sure that all callers generate unique names so that name clashes won't be a problem.

Signature · 1 line
Karl Hasselström, [email protected]
#1 lore raw

PATCH 1/2 Generate shorter patch names

From: Karl Hasselström <[email protected]>

Not all commits are blessed with a really short first-line summary in their commit messages. This means that we shouldn't blindly take the entire first line of the comment, since that sometimes results in truly spectacular patch names.

I chose 30 characters as a reasonable value, considering that we don't yet have any tab-completion on patch names. There's probably not much point in making it configurable.

---

 stgit/commands/common.py |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
stgit/commands/common.py +3 -3
index 88b1b94..0e1bb44 100644
@@ -286,10 +286,10 @@ def name_email_date(address):
 
 def make_patch_name(msg):
     """Return a string to be used as a patch name. This is generated
-    from the top line of the string passed as argument.
-    """
+    from the first 30 characters of the top line of the string passed
+    as argument."""
     if not msg:
         return None
 
-    subject_line = msg.lstrip().split('\n', 1)[0].lower()
+    subject_line = msg[:30].lstrip().split('\n', 1)[0].lower()
#2 lore raw

PATCH 2/2 Generate unique patch names

In reply to Karl Hasselström

From: Karl Hasselström <[email protected]>

"stg assimilate" was already making sure that automatically generated patch names were non-empty and unique, by suffixing them with a dash and a number if necessary. This patch moves that functionality into the autogeneration function itself, so that all its callers can benefit from it (and the user can benefit from uniform behavior from all stgit commands).

As an added bonus, this permits the removal of a number of checks that would abort with an error if the automatically generated name was empty.

---

 stgit/commands/assimilate.py |    9 +--------
 stgit/commands/common.py     |   16 +++++++++++++++-
 stgit/commands/imprt.py      |    4 +---
 stgit/commands/pick.py       |    4 +---
 stgit/commands/uncommit.py   |    5 ++---
 5 files changed, 20 insertions(+), 18 deletions(-)
stgit/commands/assimilate.py +1 -8
index 0821024..a8b3bfe 100644
@@ -73,14 +73,7 @@ def func(parser, options, args):
         return name in name2patch or crt_series.patch_exists(name)
 
     for victim in victims:
-        patchname = make_patch_name(victim.get_log())
-        if not patchname:
-            patchname = 'patch'
-        if name_taken(patchname):
-            suffix = 0
-            while name_taken('%s-%d' % (patchname, suffix)):
-                suffix += 1
-            patchname = '%s-%d' % (patchname, suffix)
+        patchname = make_patch_name(victim.get_log(), name_taken)
         patch2name[victim] = patchname
         name2patch[patchname] = victim
 
stgit/commands/common.py +15 -1
index 0e1bb44..d986711 100644
@@ -284,7 +284,7 @@ def name_email_date(address):
 
     return str_list[0]
 
-def make_patch_name(msg):
+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."""
@@ -293,3 +293,17 @@ def make_patch_name(msg):
 
     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'):
+    """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 = 'patch'
+    if unacceptable(patchname):
+        suffix = 0
+        while unacceptable('%s-%d' % (patchname, suffix)):
+            suffix += 1
+        patchname = '%s-%d' % (patchname, suffix)
+    return patchname
stgit/commands/imprt.py +1 -3
index c8cf42b..34cbf38 100644
@@ -240,9 +240,7 @@ def __import_patch(patch, filename, opti
                  __parse_patch(filename)
 
     if not patch:
-        patch = make_patch_name(message)
-        if not patch:
-            raise CmdException, 'Unknown patch name'
+        patch = make_patch_name(message, crt_series.patch_exists)
 
     # refresh_patch() will invoke the editor in this case, with correct
     # patch content
stgit/commands/pick.py +1 -3
index f8f3577..996e789 100644
@@ -71,9 +71,7 @@ def func(parser, options, args):
         elif len(patch_branch) == 2:
             patch = patch_branch[0]
         else:
-            patch = make_patch_name(commit.get_log())
-            if not patch:
-                raise CmdException, 'Unknown patch name'
+            patch = make_patch_name(commit.get_log(), crt_series.patch_exists)
 
     if options.parent:
         parent = git_id(options.parent)
stgit/commands/uncommit.py +2 -3
index 45a2087..9798f19 100644
@@ -97,9 +97,8 @@ def func(parser, options, args):
         if patchnames:
             patchname = patchnames[n]
         else:
-            patchname = make_patch_name(commit.get_log())
-        if not patchname:
-            raise CmdException, 'Unknown patch name for commit %s' % commit_id
+            patchname = make_patch_name(commit.get_log(),
+                                        crt_series.patch_exists)
 
         crt_series.new_patch(patchname,
                              can_edit = False, before_existing = T