sapling/hgext/pullcreatemarkers.py
Phil Cohen 82285d3577 phabricator: move to extlib
Summary: Update import statements and code to match core linters.

Test Plan:
`run-tests.py -j8`

Source these versions of phabdiff.py, arcdiff.py, and phabstatus.py in my hgrc. Run hg st, diff, ssl and observe no crashes.

Reviewers: durham, rmcelroy, phillco, #mercurial

Reviewed By: rmcelroy

Subscribers: rmcelroy, quark, awestern

Differential Revision: https://phabricator.intern.facebook.com/D6680961

Signature: 6680961:1515578477:ea5f1591e390f6ca8a94f652daeb1db9de819fea
2018-01-10 08:39:19 -08:00

63 lines
2.1 KiB
Python

# pullcreatemarkers.py - create obsolescence markers on pull for better rebases
#
# Copyright 2015 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# The goal of this extensions is to create obsolescence markers locally for
# commits previously landed.
# It uses the phabricator revision number in the commit message to detect the
# relationship between a draft commit and its landed counterpart.
# Thanks to these markers, less information is displayed and rebases can have
# less irrelevant conflicts.
from mercurial import commands
from mercurial import obsolete
from mercurial import phases
from mercurial import extensions
from hgext.extlib.phabricator import diffprops
def getdiff(rev):
phabrev = diffprops.parserevfromcommitmsg(rev.description())
return int(phabrev) if phabrev else None
def extsetup(ui):
extensions.wrapcommand(commands.table, 'pull', _pull)
def _pull(orig, ui, repo, *args, **opts):
if not obsolete.isenabled(repo, obsolete.createmarkersopt):
return orig(ui, repo, *args, **opts)
maxrevbeforepull = len(repo.changelog)
r = orig(ui, repo, *args, **opts)
maxrevafterpull = len(repo.changelog)
# Collect the diff number of the landed diffs
landeddiffs = {}
for rev in range(maxrevbeforepull, maxrevafterpull):
n = repo[rev]
if n.phase() == phases.public:
diff = getdiff(n)
if diff is not None:
landeddiffs[diff] = n
if not landeddiffs:
return r
# Try to find match with the drafts
tocreate = []
unfiltered = repo.unfiltered()
for rev in unfiltered.revs("draft() - obsolete()"):
n = unfiltered[rev]
diff = getdiff(n)
if diff in landeddiffs and landeddiffs[diff].rev() != n.rev():
tocreate.append((n, (landeddiffs[diff],)))
if not tocreate:
return r
with unfiltered.lock(), unfiltered.transaction('pullcreatemarkers'):
obsolete.createmarkers(unfiltered, tocreate)
return r