Patchwork D8135: phabricator: refactor `phabread` to write all patches at once

login
register
mail settings
Submitter phabricator
Date Feb. 21, 2020, 1:10 a.m.
Message ID <differential-rev-PHID-DREV-24axlmnbackxrnwpoeul-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/45274/
State Superseded
Headers show

Comments

phabricator - Feb. 21, 2020, 1:10 a.m.
mharbison72 created this revision.
Herald added subscribers: mercurial-devel, Kwan.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This will be necessary to create a first class `phabimport` command.  That
  command requires a transaction, and will import all named patches within a
  single transaction.  But if Phabricator queries also happen within the
  transaction, that leaves open the chance that an exception is raised, the
  transaction is abandoned, and the next command that is run will complain about
  needing to run `hg recover`.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  hgext/phabricator.py

CHANGE DETAILS




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

Patch

diff --git a/hgext/phabricator.py b/hgext/phabricator.py
--- a/hgext/phabricator.py
+++ b/hgext/phabricator.py
@@ -1608,13 +1608,17 @@ 
 def readpatch(repo, drevs, write):
     """generate plain-text patch readable by 'hg import'
 
-    write is usually ui.write. drevs is what "querydrev" returns, results of
+    write takes a list of (DREV, bytes), where DREV is the differential number
+    (as bytes, without the "D" prefix) and the bytes are the text of a patch
+    to be imported. drevs is what "querydrev" returns, results of
     "differential.query".
     """
     # Prefetch hg:meta property for all diffs
     diffids = sorted(set(max(int(v) for v in drev[b'diffs']) for drev in drevs))
     diffs = callconduit(repo.ui, b'differential.querydiffs', {b'ids': diffids})
 
+    patches = []
+
     # Generate patch for each drev
     for drev in drevs:
         repo.ui.note(_(b'reading D%s\n') % drev[b'id'])
@@ -1635,7 +1639,10 @@ 
                 header += b'# %s %s\n' % (_metanamemap[k], meta[k])
 
         content = b'%s%s\n%s' % (header, desc, body)
-        write(content)
+        patches.append((drev[b'id'], content))
+
+    # Write patches to the supplied callback
+    write(patches)
 
 
 @vcrcommand(
@@ -1667,7 +1674,12 @@ 
     if opts.get(b'stack'):
         spec = b':(%s)' % spec
     drevs = querydrev(repo, spec)
-    readpatch(repo, drevs, ui.write)
+
+    def _write(patches):
+        for drev, content in patches:
+            ui.write(content)
+
+    readpatch(repo, drevs, _write)
 
 
 @vcrcommand(