[PATCH 06/10] add patch version function

WEN Pingbo wengpingbo at gmail.com
Tue Aug 23 19:03:40 AEST 2016


Added a OneToOneField in submission to create a single list of patch
version. And if two patches have the same raw subject(without prefixes),
we treat it as the same patch with different version.

Signed-off-by: WEN Pingbo <wengpingbo at gmail.com>
---
 patchwork/bin/parsemail.py                    | 25 +++++++++++++++++++--
 patchwork/models.py                           |  1 +
 patchwork/templates/patchwork/submission.html | 13 ++++++++++-
 patchwork/views/patch.py                      | 32 +++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/patchwork/bin/parsemail.py b/patchwork/bin/parsemail.py
index bcc7982..1cc6e8c 100755
--- a/patchwork/bin/parsemail.py
+++ b/patchwork/bin/parsemail.py
@@ -40,6 +40,7 @@ from django.contrib.auth.models import User
 from django.utils.log import AdminEmailHandler
 from django.utils import six
 from django.utils.six.moves import map
+from django.db import IntegrityError
 
 from patchwork.models import (Patch, Project, Person, Comment, State,
                               DelegationRule, Submission, CoverLetter,
@@ -383,10 +384,12 @@ def clean_subject(subject, drop_prefixes=None):
     subject = normalise_space(subject)
 
     subject = subject.strip()
+    raw_subject = subject
+
     if prefixes:
         subject = '[%s] %s' % (','.join(prefixes), subject)
 
-    return (is_comment, subject, prefixes)
+    return (is_comment, raw_subject, subject, prefixes)
 
 
 def clean_content(content):
@@ -447,6 +450,22 @@ def find_delegate(mail):
             pass
     return None
 
+def update_patch_version(project, raw_name):
+    raw_name = re.escape(raw_name)
+    raw_name = raw_name + r'$'
+    patches = Patch.objects.filter(project=project, name__regex=raw_name)
+
+    cnt = len(patches)
+    if cnt < 2:
+        return
+
+    for i in reversed(range(1, cnt)):
+        patches[i].old_version = patches[i - 1]
+        try:
+            patches[i].save()
+        except IntegrityError:
+            LOGGER.error('Failed to update patch old_version %s', patches[i].name)
+            pass
 
 def parse_mail(mail, list_id=None):
     """Parse a mail and add to the database.
@@ -491,7 +510,7 @@ def parse_mail(mail, list_id=None):
 
     msgid = mail.get('Message-Id').strip()
     author = find_author(mail)
-    is_comment, name, prefixes = clean_subject(mail.get('Subject'), [project.linkname])
+    is_comment, raw_name, name, prefixes = clean_subject(mail.get('Subject'), [project.linkname])
     x, n = parse_series_marker(prefixes)
     refs = find_references(mail)
     date = find_date(mail)
@@ -526,6 +545,8 @@ def parse_mail(mail, list_id=None):
         patch.save()
         LOGGER.debug('Patch saved')
 
+        update_patch_version(project, raw_name)
+
         return patch
     elif x == 0:  # (potential) cover letters
         # if refs are empty, it's implicitly a cover letter. If not,
diff --git a/patchwork/models.py b/patchwork/models.py
index bcc8f90..4765997 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -299,6 +299,7 @@ class Submission(EmailMixin, models.Model):
 
     name = models.CharField(max_length=255)
     parent = models.ForeignKey('self', null=True, blank=True, related_name="children")
+    old_version = models.OneToOneField('self', null=True, blank=True, related_name="new_version")
 
     # patchwork metadata
 
diff --git a/patchwork/templates/patchwork/submission.html b/patchwork/templates/patchwork/submission.html
index 8f1386d..306bf72 100644
--- a/patchwork/templates/patchwork/submission.html
+++ b/patchwork/templates/patchwork/submission.html
@@ -108,15 +108,26 @@ function toggle_headers(link_id, headers_id)
  </div>
 {% endif %}
 
-{% if patchseries %}
+{% if patchversion or patchseries %}
 <div class="patchform patchform-series">
 <h3>Patch Series</h3>
 
+{% if patchversion %}
+<strong>Versions: </strong>
+<select id=patch_version name="version" onChange="window.location.replace(this.options[this.selectedIndex].value)">
+{% for patch in patchversion %}
+    <option value="{% url 'patch-detail' patch_id=patch.id %}" {% if submission.id = patch.id %}selected{% endif %}>{{ patch.name }}</option>
+{% endfor %}
+</select>
+{% endif %}
+
+{% if patchseries %}
 <ul>
 {% for patch in patchseries %}
     <li><a href="{% url 'patch-detail' patch_id=patch.id %}">{{ patch.name }}</a></li>
 {% endfor %}
 </ul>
+{% endif %}
 
 </div>
 {% endif %}
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index cd73d8b..29ea76b 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -62,6 +62,37 @@ def find_patch_series(patch):
     # url will automaticly switch to cover if a patch is not found
     return [{'id':parent.id, 'name':parent.name}] + patches
 
+def find_patch_version(patch):
+    series = []
+
+    # find new version
+    p = patch
+    while p:
+        tmp = {}
+        tmp['id'] = p.id
+        tmp['name'] = p.name
+        series.append(tmp)
+
+        try:
+            p = p.new_version
+        except Submission.DoesNotExist:
+            p = None
+
+    # find old version
+    p = patch.old_version
+    while p:
+        tmp = {}
+        tmp['id'] = p.id
+        tmp['name'] = p.name
+        series.append(tmp)
+
+        p = p.old_version
+
+    if len(series) > 1:
+        return series
+    else:
+        return None
+
 def patch(request, patch_id):
     # redirect to cover letters where necessary
     try:
@@ -134,6 +165,7 @@ def patch(request, patch_id):
     context['createbundleform'] = createbundleform
     context['project'] = patch.project
     context['patchseries'] = find_patch_series(patch)
+    context['patchversion'] = find_patch_version(patch)
 
     return render(request, 'patchwork/submission.html', context)
 
-- 
1.9.1



More information about the Patchwork mailing list