2017-06-14 03:09:34 +03:00
|
|
|
# restack.py - rebase to make a stack connected again
|
|
|
|
#
|
|
|
|
# Copyright 2013 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.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-11-02 21:54:31 +03:00
|
|
|
from hgext import rebase
|
2018-07-06 03:45:27 +03:00
|
|
|
from mercurial import commands, revsetlang
|
|
|
|
from mercurial.i18n import _
|
2017-06-14 03:09:34 +03:00
|
|
|
|
2018-05-30 12:16:33 +03:00
|
|
|
|
2017-06-14 03:09:34 +03:00
|
|
|
def restack(ui, repo, rebaseopts=None):
|
2018-10-23 21:38:42 +03:00
|
|
|
"""Repair a situation in which one or more commits in a stack
|
2017-06-14 03:09:34 +03:00
|
|
|
have been obsoleted (thereby leaving their descendants in the stack
|
2018-10-23 21:38:42 +03:00
|
|
|
orphaned) by finding any such commits and rebasing their descendants
|
|
|
|
onto the latest version of each respective commit.
|
2017-06-14 03:09:34 +03:00
|
|
|
"""
|
2017-11-02 21:54:31 +03:00
|
|
|
rebaseopts = (rebaseopts or {}).copy()
|
|
|
|
|
|
|
|
# TODO: Remove config override after https://phab.mercurial-scm.org/D1063
|
2018-05-30 12:16:33 +03:00
|
|
|
config = {("experimental", "rebase.multidest"): True}
|
2017-11-02 21:54:31 +03:00
|
|
|
|
|
|
|
with ui.configoverride(config), repo.wlock(), repo.lock():
|
|
|
|
# Find drafts connected to the current stack via either changelog or
|
|
|
|
# obsolete graph. Note: "draft() & ::." is optimized by D441.
|
|
|
|
|
|
|
|
# 1. Connect drafts via changelog
|
2018-05-30 12:16:33 +03:00
|
|
|
revs = list(repo.revs("(draft() & ::.)::"))
|
2017-11-02 21:54:31 +03:00
|
|
|
if not revs:
|
|
|
|
# "." is probably public. Check its direct children.
|
2018-05-30 12:16:33 +03:00
|
|
|
revs = repo.revs("draft() & children(.)")
|
2017-11-02 21:54:31 +03:00
|
|
|
if not revs:
|
2018-05-30 12:16:33 +03:00
|
|
|
ui.status(_("nothing to restack\n"))
|
2017-11-02 21:54:31 +03:00
|
|
|
return 1
|
|
|
|
# 2. Connect revs via obsolete graph
|
2018-10-03 23:11:06 +03:00
|
|
|
revs = list(repo.revs("successors(%ld)+predecessors(%ld)", revs, revs))
|
2017-11-02 21:54:31 +03:00
|
|
|
# 3. Connect revs via changelog again to cover missing revs
|
2018-05-30 12:16:33 +03:00
|
|
|
revs = list(repo.revs("(draft() & ::%ld)::", revs))
|
2017-11-02 21:54:31 +03:00
|
|
|
|
2018-05-30 12:16:33 +03:00
|
|
|
rebaseopts["rev"] = [revsetlang.formatspec("%ld", revs)]
|
|
|
|
rebaseopts["dest"] = "_destrestack(SRC)"
|
2017-11-02 21:54:31 +03:00
|
|
|
|
|
|
|
rebase.rebase(ui, repo, **rebaseopts)
|
|
|
|
|
|
|
|
# Ensure that we always end up on the latest version of the
|
|
|
|
# current changeset. Usually, this will be taken care of
|
|
|
|
# by the rebase operation. However, in some cases (such as
|
|
|
|
# if we are on the precursor of the base changeset) the
|
|
|
|
# rebase will not update to the latest version, so we need
|
|
|
|
# to do this manually.
|
2018-10-03 23:11:06 +03:00
|
|
|
successor = repo.revs("successors(.) - .").last()
|
2017-11-02 21:54:31 +03:00
|
|
|
if successor is not None:
|
|
|
|
commands.update(ui, repo, rev=successor)
|