changeset 43189:9f802243a42e

phabricator: add makebinary and addoldbinary functions These populate the phabchange with the data for a binary file, much as maketext does for text files. Differential Revision: https://phab.mercurial-scm.org/D7048
author Ian Moody <moz-ian@perix.co.uk>
date Sun, 06 Oct 2019 15:37:13 +0100
parents 24e8aac7c630
children c19b327017b9
files hgext/phabricator.py
diffstat 1 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/phabricator.py	Sun Oct 06 15:16:47 2019 +0100
+++ b/hgext/phabricator.py	Sun Oct 06 15:37:13 2019 +0100
@@ -46,6 +46,7 @@
 import hashlib
 import itertools
 import json
+import mimetypes
 import operator
 import re
 
@@ -646,6 +647,43 @@
     return fphid
 
 
+def addoldbinary(pchange, fctx, originalfname):
+    """add the metadata for the previous version of a binary file to the
+    phabchange for the new version
+    """
+    oldfctx = fctx.p1()[originalfname]
+    if fctx.cmp(oldfctx):
+        # Files differ, add the old one
+        pchange.metadata[b'old:file:size'] = oldfctx.size()
+        mimeguess, _enc = mimetypes.guess_type(
+            encoding.unifromlocal(oldfctx.path())
+        )
+        if mimeguess:
+            pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr(
+                mimeguess
+            )
+        fphid = uploadfile(oldfctx)
+        pchange.metadata[b'old:binary-phid'] = fphid
+    else:
+        # If it's left as IMAGE/BINARY web UI might try to display it
+        pchange.fileType = DiffFileType.TEXT
+        pchange.copynewmetadatatoold()
+
+
+def makebinary(pchange, fctx):
+    """populate the phabchange for a binary file"""
+    pchange.fileType = DiffFileType.BINARY
+    fphid = uploadfile(fctx)
+    pchange.metadata[b'new:binary-phid'] = fphid
+    pchange.metadata[b'new:file:size'] = fctx.size()
+    mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path()))
+    if mimeguess:
+        mimeguess = pycompat.bytestr(mimeguess)
+        pchange.metadata[b'new:file:mime-type'] = mimeguess
+        if mimeguess.startswith(b'image/'):
+            pchange.fileType = DiffFileType.IMAGE
+
+
 def creatediff(ctx):
     """create a Differential Diff"""
     repo = ctx.repo()