Have only a single command in each test_expect_failure
[stgit] / stgit / utils.py
index d04d077..fbfe748 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,27 @@ 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 top line of the string passed as argument, and is at most
+    30 characters long."""
+    if not msg:
+        return None
+
+    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'):
+    """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 unacceptable(patchname):
+        suffix = 0
+        while unacceptable('%s-%d' % (patchname, suffix)):
+            suffix += 1
+        patchname = '%s-%d' % (patchname, suffix)
+    return patchname