comparison mercurial/exchange.py @ 44545:bd7b2c8d06cc

pull: add `--confirm` flag to confirm before writing changes This introduces a new flag to pull command `--confirm` and also a config option named `pull.confirm` which if used will prompt user describing changes which are pulled and asking whether to accept them or not. Differential Revision: https://phab.mercurial-scm.org/D8200
author Pulkit Goyal <7895pulkit@gmail.com>
date Sat, 29 Feb 2020 12:58:38 +0530
parents 9d2b2df2c2ba
children 72feaeb510b3
comparison
equal deleted inserted replaced
44544:13da36d77a3f 44545:bd7b2c8d06cc
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import collections 10 import collections
11 import weakref
11 12
12 from .i18n import _ 13 from .i18n import _
13 from .node import ( 14 from .node import (
14 hex, 15 hex,
15 nullid, 16 nullid,
1703 new_heads = headsofdiff(unficl.heads(), old_heads) 1704 new_heads = headsofdiff(unficl.heads(), old_heads)
1704 pullop.common = headsofunion(new_heads, pullop.common) 1705 pullop.common = headsofunion(new_heads, pullop.common)
1705 pullop.rheads = set(pullop.rheads) - pullop.common 1706 pullop.rheads = set(pullop.rheads) - pullop.common
1706 1707
1707 1708
1709 def add_confirm_callback(repo, pullop):
1710 """ adds a finalize callback to transaction which can be used to show stats
1711 to user and confirm the pull before committing transaction """
1712
1713 tr = pullop.trmanager.transaction()
1714 scmutil.registersummarycallback(
1715 repo, tr, txnname=b'pull', as_validator=True
1716 )
1717 reporef = weakref.ref(repo.unfiltered())
1718
1719 def prompt(tr):
1720 repo = reporef()
1721 cm = _(b'accept incoming changes (yn)?$$ &Yes $$ &No')
1722 if repo.ui.promptchoice(cm):
1723 raise error.Abort("user aborted")
1724
1725 tr.addvalidator(b'900-pull-prompt', prompt)
1726
1727
1708 def pull( 1728 def pull(
1709 repo, 1729 repo,
1710 remote, 1730 remote,
1711 heads=None, 1731 heads=None,
1712 force=False, 1732 force=False,
1714 opargs=None, 1734 opargs=None,
1715 streamclonerequested=None, 1735 streamclonerequested=None,
1716 includepats=None, 1736 includepats=None,
1717 excludepats=None, 1737 excludepats=None,
1718 depth=None, 1738 depth=None,
1739 confirm=None,
1719 ): 1740 ):
1720 """Fetch repository data from a remote. 1741 """Fetch repository data from a remote.
1721 1742
1722 This is the main function used to retrieve data from a remote repository. 1743 This is the main function used to retrieve data from a remote repository.
1723 1744
1738 include and exclude in storage, respectively. If not defined, narrow 1759 include and exclude in storage, respectively. If not defined, narrow
1739 patterns from the repo instance are used, if available. 1760 patterns from the repo instance are used, if available.
1740 ``depth`` is an integer indicating the DAG depth of history we're 1761 ``depth`` is an integer indicating the DAG depth of history we're
1741 interested in. If defined, for each revision specified in ``heads``, we 1762 interested in. If defined, for each revision specified in ``heads``, we
1742 will fetch up to this many of its ancestors and data associated with them. 1763 will fetch up to this many of its ancestors and data associated with them.
1764 ``confirm`` is a boolean indicating whether the pull should be confirmed
1765 before committing the transaction. This overrides HGPLAIN.
1743 1766
1744 Returns the ``pulloperation`` created for this pull. 1767 Returns the ``pulloperation`` created for this pull.
1745 """ 1768 """
1746 if opargs is None: 1769 if opargs is None:
1747 opargs = {} 1770 opargs = {}
1784 pullop.trmanager = transactionmanager(repo, b'pull', remote.url()) 1807 pullop.trmanager = transactionmanager(repo, b'pull', remote.url())
1785 wlock = util.nullcontextmanager() 1808 wlock = util.nullcontextmanager()
1786 if not bookmod.bookmarksinstore(repo): 1809 if not bookmod.bookmarksinstore(repo):
1787 wlock = repo.wlock() 1810 wlock = repo.wlock()
1788 with wlock, repo.lock(), pullop.trmanager: 1811 with wlock, repo.lock(), pullop.trmanager:
1812 if confirm or (
1813 repo.ui.configbool(b"pull", b"confirm") and not repo.ui.plain()
1814 ):
1815 add_confirm_callback(repo, pullop)
1816
1789 # Use the modern wire protocol, if available. 1817 # Use the modern wire protocol, if available.
1790 if remote.capable(b'command-changesetdata'): 1818 if remote.capable(b'command-changesetdata'):
1791 exchangev2.pull(pullop) 1819 exchangev2.pull(pullop)
1792 else: 1820 else:
1793 # This should ideally be in _pullbundle2(). However, it needs to run 1821 # This should ideally be in _pullbundle2(). However, it needs to run