Mercurial > hg
comparison hgext/phabricator.py @ 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 |
comparison
equal
deleted
inserted
replaced
43188:24e8aac7c630 | 43189:9f802243a42e |
---|---|
44 import base64 | 44 import base64 |
45 import contextlib | 45 import contextlib |
46 import hashlib | 46 import hashlib |
47 import itertools | 47 import itertools |
48 import json | 48 import json |
49 import mimetypes | |
49 import operator | 50 import operator |
50 import re | 51 import re |
51 | 52 |
52 from mercurial.node import bin, nullid | 53 from mercurial.node import bin, nullid |
53 from mercurial.i18n import _ | 54 from mercurial.i18n import _ |
642 | 643 |
643 if not fphid: | 644 if not fphid: |
644 raise error.Abort(b'Upload of %s failed.' % bytes(fctx)) | 645 raise error.Abort(b'Upload of %s failed.' % bytes(fctx)) |
645 | 646 |
646 return fphid | 647 return fphid |
648 | |
649 | |
650 def addoldbinary(pchange, fctx, originalfname): | |
651 """add the metadata for the previous version of a binary file to the | |
652 phabchange for the new version | |
653 """ | |
654 oldfctx = fctx.p1()[originalfname] | |
655 if fctx.cmp(oldfctx): | |
656 # Files differ, add the old one | |
657 pchange.metadata[b'old:file:size'] = oldfctx.size() | |
658 mimeguess, _enc = mimetypes.guess_type( | |
659 encoding.unifromlocal(oldfctx.path()) | |
660 ) | |
661 if mimeguess: | |
662 pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr( | |
663 mimeguess | |
664 ) | |
665 fphid = uploadfile(oldfctx) | |
666 pchange.metadata[b'old:binary-phid'] = fphid | |
667 else: | |
668 # If it's left as IMAGE/BINARY web UI might try to display it | |
669 pchange.fileType = DiffFileType.TEXT | |
670 pchange.copynewmetadatatoold() | |
671 | |
672 | |
673 def makebinary(pchange, fctx): | |
674 """populate the phabchange for a binary file""" | |
675 pchange.fileType = DiffFileType.BINARY | |
676 fphid = uploadfile(fctx) | |
677 pchange.metadata[b'new:binary-phid'] = fphid | |
678 pchange.metadata[b'new:file:size'] = fctx.size() | |
679 mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path())) | |
680 if mimeguess: | |
681 mimeguess = pycompat.bytestr(mimeguess) | |
682 pchange.metadata[b'new:file:mime-type'] = mimeguess | |
683 if mimeguess.startswith(b'image/'): | |
684 pchange.fileType = DiffFileType.IMAGE | |
647 | 685 |
648 | 686 |
649 def creatediff(ctx): | 687 def creatediff(ctx): |
650 """create a Differential Diff""" | 688 """create a Differential Diff""" |
651 repo = ctx.repo() | 689 repo = ctx.repo() |