phabricator: add the phabchange data structure
authorIan Moody <moz-ian@perix.co.uk>
Sun, 06 Oct 2019 14:08:03 +0100
changeset 43184 99ee4afd352f
parent 43183 73d4bc60e389
child 43185 75e7628b488f
phabricator: add the phabchange data structure These store data about individual files in a commit. Differential Revision: https://phab.mercurial-scm.org/D7043
hgext/phabricator.py
--- a/hgext/phabricator.py	Sun Oct 06 13:55:04 2019 +0100
+++ b/hgext/phabricator.py	Sun Oct 06 14:08:03 2019 +0100
@@ -481,6 +481,47 @@
     delLines = attr.ib(default=0)  # camelcase-required
 
 
+@attr.s
+class phabchange(object):
+    """Represents a Differential change, owns Differential hunks and owned by a
+    Differential diff.  Each one represents one file in a diff.
+    """
+
+    currentPath = attr.ib(default=None)  # camelcase-required
+    oldPath = attr.ib(default=None)  # camelcase-required
+    awayPaths = attr.ib(default=attr.Factory(list))  # camelcase-required
+    metadata = attr.ib(default=attr.Factory(dict))
+    oldProperties = attr.ib(default=attr.Factory(dict))  # camelcase-required
+    newProperties = attr.ib(default=attr.Factory(dict))  # camelcase-required
+    type = attr.ib(default=DiffChangeType.CHANGE)
+    fileType = attr.ib(default=DiffFileType.TEXT)  # camelcase-required
+    commitHash = attr.ib(default=None)  # camelcase-required
+    addLines = attr.ib(default=0)  # camelcase-required
+    delLines = attr.ib(default=0)  # camelcase-required
+    hunks = attr.ib(default=attr.Factory(list))
+
+    def copynewmetadatatoold(self):
+        for key in list(self.metadata.keys()):
+            newkey = key.replace(b'new:', b'old:')
+            self.metadata[newkey] = self.metadata[key]
+
+    def addoldmode(self, value):
+        self.oldProperties[b'unix:filemode'] = value
+
+    def addnewmode(self, value):
+        self.newProperties[b'unix:filemode'] = value
+
+    def addhunk(self, hunk):
+        if not isinstance(hunk, phabhunk):
+            raise error.Abort(b'phabchange.addhunk only takes phabhunks')
+        self.hunks.append(hunk)
+        # It's useful to include these stats since the Phab web UI shows them,
+        # and uses them to estimate how large a change a Revision is. Also used
+        # in email subjects for the [+++--] bit.
+        self.addLines += hunk.addLines
+        self.delLines += hunk.delLines
+
+
 def creatediff(ctx):
     """create a Differential Diff"""
     repo = ctx.repo()