changeset 31183:8018b90f8307

patchbomb: factor out function that builds a prefix string to patch subject I'll add templating support.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 25 Feb 2017 18:16:41 +0900
parents 5660c45ecba6
children e3ab7c717129
files hgext/patchbomb.py
diffstat 1 files changed, 16 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/patchbomb.py	Sun Jan 18 18:04:44 2015 +0900
+++ b/hgext/patchbomb.py	Sat Feb 25 18:16:41 2017 +0900
@@ -135,6 +135,18 @@
         intro = 1 < number
     return intro
 
+def _formatprefix(flags, idx, total, numbered):
+    """build prefix to patch subject"""
+    flag = ' '.join(flags)
+    if flag:
+        flag = ' ' + flag
+
+    if not numbered:
+        return '[PATCH%s]' % flag
+    else:
+        tlen = len(str(total))
+        return '[PATCH %0*d of %d%s]' % (tlen, idx, total, flag)
+
 def makepatch(ui, repo, patchlines, opts, _charsets, idx, total, numbered,
               patchname=None):
 
@@ -202,16 +214,12 @@
     else:
         msg = mail.mimetextpatch(body, display=opts.get('test'))
 
-    flag = ' '.join(opts.get('flag'))
-    if flag:
-        flag = ' ' + flag
-
+    prefix = _formatprefix(opts.get('flag'), idx, total, numbered)
     subj = desc[0].strip().rstrip('. ')
     if not numbered:
-        subj = '[PATCH%s] %s' % (flag, opts.get('subject') or subj)
+        subj = ' '.join([prefix, opts.get('subject') or subj])
     else:
-        tlen = len(str(total))
-        subj = '[PATCH %0*d of %d%s] %s' % (tlen, idx, total, flag, subj)
+        subj = ' '.join([prefix, subj])
     msg['Subject'] = mail.headencode(ui, subj, _charsets, opts.get('test'))
     msg['X-Mercurial-Node'] = node
     msg['X-Mercurial-Series-Index'] = '%i' % idx
@@ -309,13 +317,8 @@
     email is returned as (subject, body, cumulative-diffstat)"""
     ui = repo.ui
     _charsets = mail._charsets(ui)
-    tlen = len(str(len(patches)))
 
-    flag = opts.get('flag') or ''
-    if flag:
-        flag = ' ' + ' '.join(flag)
-    prefix = '[PATCH %0*d of %d%s]' % (tlen, 0, len(patches), flag)
-
+    prefix = _formatprefix(opts.get('flag'), 0, len(patches), numbered=True)
     subj = (opts.get('subject') or
             prompt(ui, '(optional) Subject: ', rest=prefix, default=''))
     if not subj: