Patchwork D7048: phabricator: add makebinary and addoldbinary functions

login
register
mail settings
Submitter phabricator
Date Oct. 11, 2019, 5:55 p.m.
Message ID <74718ff791365573c7f21d5e28a6a58d@localhost.localdomain>
Download mbox | patch
Permalink /patch/42231/
State Not Applicable
Headers show

Comments

phabricator - Oct. 11, 2019, 5:55 p.m.
Kwan added a comment.
Kwan updated this revision to Diff 17079.


  Fix some test-check-code issues, and one test-check-pyflakes unused local.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7048?vs=17054&id=17079

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7048/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7048

AFFECTED FILES
  hgext/phabricator.py

CHANGE DETAILS




To: Kwan, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/phabricator.py b/hgext/phabricator.py
--- a/hgext/phabricator.py
+++ b/hgext/phabricator.py
@@ -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()