git » git-arr » commit cbb36e0

Implement a "patch" view

author Alberto Bertogli
2018-10-01 20:25:11 UTC
committer Alberto Bertogli
2018-10-01 20:39:57 UTC
parent 722d765973ab368a313455d2179d7b3d67ec25cb

Implement a "patch" view

This commit implements a "patch" view, with a simple plain-text
representation of a commit, that can be used as a patch file.

git-arr +17 -0
views/patch.tpl +8 -0

diff --git a/git-arr b/git-arr
index ca0ccfa..5c4e7db 100755
--- a/git-arr
+++ b/git-arr
@@ -61,6 +61,7 @@ def load_config(path):
         'embed_markdown': 'yes',
         'embed_images': 'no',
         'ignore': '',
+        'generate_patch': 'yes',
     }
 
     config = configparser.SafeConfigParser(defaults)
@@ -120,6 +121,7 @@ def load_config(path):
             r.info.max_pages = sys.maxint
         r.info.generate_tree = config.getboolean(s, 'tree')
         r.info.root_diff = config.getboolean(s, 'rootdiff')
+        r.info.generate_patch = config.getboolean(s, 'generate_patch')
 
         r.info.web_url = config.get(s, 'web_url')
         web_url_file = fullpath + '/' + config.get(s, 'web_url_file')
@@ -236,6 +238,19 @@ def commit(repo, cid):
 
     return dict(repo = repo, c=c)
 
+@bottle.route('/r/<repo:repo>/c/<cid:re:[0-9a-f]{5,40}>.patch')
+@bottle.view('patch',
+        # Output is text/plain, don't do HTML escaping.
+        template_settings={"noescape": True})
+def patch(repo, cid):
+    c = repo.commit(cid)
+    if not c:
+        bottle.abort(404, 'Commit not found')
+
+    bottle.response.content_type = 'text/plain; charset=utf8'
+
+    return dict(repo = repo, c=c)
+
 @bottle.route('/r/<repo:repo>/b/<bname:path>/t/f=<fname:path>.html')
 @bottle.route('/r/<repo:repo>/b/<bname:path>/t/<dirname:path>/f=<fname:path>.html')
 @bottle.view('blob')
@@ -396,6 +411,8 @@ def generate(output, only = None):
             for cid in commit_ids:
                 write_to('r/%s/c/%s/index.html' % (r.name, cid),
                         commit, (r, cid))
+                if r.info.generate_patch:
+                    write_to('r/%s/c/%s.patch' % (r.name, cid), patch, (r, cid))
                 commit_count += 1
 
             # To avoid regenerating files that have not changed, we will
diff --git a/views/patch.tpl b/views/patch.tpl
new file mode 100644
index 0000000..ab3fca1
--- /dev/null
+++ b/views/patch.tpl
@@ -0,0 +1,8 @@
+From: {{c.author_name}} <{{c.author_email}}>
+Date: {{c.author_date}}
+Subject: {{c.subject}}
+
+{{c.body.strip()}}
+---
+
+{{c.diff.body}}