| 1 |
# fetch.py - pull and merge remote changes |
| 2 |
# |
| 3 |
# Copyright 2006 Vadim Gelfer <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
'''pull, update and merge in one command (DEPRECATED)''' |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
from mercurial.i18n import _ |
| 13 |
from mercurial.node import short |
| 14 |
from mercurial import ( |
| 15 |
cmdutil, |
| 16 |
error, |
| 17 |
exchange, |
| 18 |
lock, |
| 19 |
pycompat, |
| 20 |
registrar, |
| 21 |
scmutil, |
| 22 |
) |
| 23 |
from mercurial.cmd_impls import ( |
| 24 |
update as up_impl, |
| 25 |
) |
| 26 |
from mercurial.repo import ( |
| 27 |
factory as repo_factory, |
| 28 |
) |
| 29 |
from mercurial.utils import ( |
| 30 |
dateutil, |
| 31 |
urlutil, |
| 32 |
) |
| 33 |
|
| 34 |
release = lock.release |
| 35 |
cmdtable = {} |
| 36 |
command = registrar.command(cmdtable) |
| 37 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 38 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 39 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 40 |
# leave the attribute unspecified. |
| 41 |
testedwith = b'ships-with-hg-core' |
| 42 |
|
| 43 |
|
| 44 |
@command( |
| 45 |
b'fetch', |
| 46 |
[ |
| 47 |
( |
| 48 |
b'r', |
| 49 |
b'rev', |
| 50 |
[], |
| 51 |
_(b'a specific revision you would like to pull'), |
| 52 |
_(b'REV'), |
| 53 |
), |
| 54 |
(b'', b'edit', None, _(b'invoke editor on commit messages')), |
| 55 |
(b'', b'force-editor', None, _(b'edit commit message (DEPRECATED)')), |
| 56 |
(b'', b'switch-parent', None, _(b'switch parents when merging')), |
| 57 |
] |
| 58 |
+ cmdutil.commitopts |
| 59 |
+ cmdutil.commitopts2 |
| 60 |
+ cmdutil.remoteopts, |
| 61 |
_(b'hg fetch [SOURCE]'), |
| 62 |
helpcategory=command.CATEGORY_REMOTE_REPO_MANAGEMENT, |
| 63 |
) |
| 64 |
def fetch(ui, repo, source=b'default', **opts): |
| 65 |
"""pull changes from a remote repository, merge new changes if needed. |
| 66 |
|
| 67 |
This finds all changes from the repository at the specified path |
| 68 |
or URL and adds them to the local repository. |
| 69 |
|
| 70 |
If the pulled changes add a new branch head, the head is |
| 71 |
automatically merged, and the result of the merge is committed. |
| 72 |
Otherwise, the working directory is updated to include the new |
| 73 |
changes. |
| 74 |
|
| 75 |
When a merge is needed, the working directory is first updated to |
| 76 |
the newly pulled changes. Local changes are then merged into the |
| 77 |
pulled changes. To switch the merge order, use --switch-parent. |
| 78 |
|
| 79 |
See :hg:`help dates` for a list of formats valid for -d/--date. |
| 80 |
|
| 81 |
Returns 0 on success. |
| 82 |
""" |
| 83 |
|
| 84 |
date = opts.get('date') |
| 85 |
if date: |
| 86 |
opts['date'] = dateutil.parsedate(date) |
| 87 |
|
| 88 |
parent = repo.dirstate.p1() |
| 89 |
branch = repo.dirstate.branch() |
| 90 |
try: |
| 91 |
branchnode = repo.branchtip(branch) |
| 92 |
except error.RepoLookupError: |
| 93 |
branchnode = None |
| 94 |
if parent != branchnode: |
| 95 |
raise error.Abort( |
| 96 |
_(b'working directory not at branch tip'), |
| 97 |
hint=_(b"use 'hg update' to check out branch tip"), |
| 98 |
) |
| 99 |
|
| 100 |
wlock = lock = None |
| 101 |
try: |
| 102 |
wlock = repo.wlock() |
| 103 |
lock = repo.lock() |
| 104 |
|
| 105 |
scmutil.bail_if_changed(repo) |
| 106 |
|
| 107 |
# fetch apparently checks for "active" head only, since this is |
| 108 |
# mostly a dead extension, I did not alter this, not optimized the |
| 109 |
# code much. |
| 110 |
bheads = repo.branchmap().branchheads(branch) |
| 111 |
bheads = [head for head in bheads if len(repo[head].children()) == 0] |
| 112 |
if len(bheads) > 1: |
| 113 |
raise error.Abort( |
| 114 |
_( |
| 115 |
b'multiple heads in this branch ' |
| 116 |
b'(use "hg heads ." and "hg merge" to merge)' |
| 117 |
) |
| 118 |
) |
| 119 |
|
| 120 |
path = urlutil.get_unique_pull_path_obj(b'fetch', ui, source) |
| 121 |
other = repo_factory.peer(repo, pycompat.byteskwargs(opts), path) |
| 122 |
ui.status(_(b'pulling from %s\n') % urlutil.hidepassword(path.loc)) |
| 123 |
revs = None |
| 124 |
if opts['rev']: |
| 125 |
try: |
| 126 |
revs = [other.lookup(rev) for rev in opts['rev']] |
| 127 |
except error.CapabilityError: |
| 128 |
err = _( |
| 129 |
b"other repository doesn't support revision lookup, " |
| 130 |
b"so a rev cannot be specified." |
| 131 |
) |
| 132 |
raise error.Abort(err) |
| 133 |
|
| 134 |
# Are there any changes at all? |
| 135 |
modheads = exchange.pull(repo, other, heads=revs).cgresult |
| 136 |
if modheads == 0: |
| 137 |
return 0 |
| 138 |
|
| 139 |
# Is this a simple fast-forward along the current branch? |
| 140 |
newheads = repo.branchmap().branchheads(branch) |
| 141 |
newchildren = repo.changelog.nodesbetween([parent], newheads)[2] |
| 142 |
if len(newheads) == 1 and len(newchildren): |
| 143 |
if newchildren[0] != parent: |
| 144 |
return up_impl.update(repo, newchildren[0]) |
| 145 |
else: |
| 146 |
return 0 |
| 147 |
|
| 148 |
# Are there more than one additional branch heads? |
| 149 |
newchildren = [n for n in newchildren if n != parent] |
| 150 |
newparent = parent |
| 151 |
if newchildren: |
| 152 |
newparent = newchildren[0] |
| 153 |
up_impl.clean(repo, newparent) |
| 154 |
newheads = [n for n in newheads if n != newparent] |
| 155 |
if len(newheads) > 1: |
| 156 |
ui.status( |
| 157 |
_( |
| 158 |
b'not merging with %d other new branch heads ' |
| 159 |
b'(use "hg heads ." and "hg merge" to merge them)\n' |
| 160 |
) |
| 161 |
% (len(newheads) - 1) |
| 162 |
) |
| 163 |
return 1 |
| 164 |
|
| 165 |
if not newheads: |
| 166 |
return 0 |
| 167 |
|
| 168 |
# Otherwise, let's merge. |
| 169 |
err = False |
| 170 |
if newheads: |
| 171 |
# By default, we consider the repository we're pulling |
| 172 |
# *from* as authoritative, so we merge our changes into |
| 173 |
# theirs. |
| 174 |
if opts['switch_parent']: |
| 175 |
firstparent, secondparent = newparent, newheads[0] |
| 176 |
else: |
| 177 |
firstparent, secondparent = newheads[0], newparent |
| 178 |
ui.status( |
| 179 |
_(b'updating to %d:%s\n') |
| 180 |
% (repo.changelog.rev(firstparent), short(firstparent)) |
| 181 |
) |
| 182 |
up_impl.clean(repo, firstparent) |
| 183 |
p2ctx = repo[secondparent] |
| 184 |
ui.status( |
| 185 |
_(b'merging with %d:%s\n') % (p2ctx.rev(), short(secondparent)) |
| 186 |
) |
| 187 |
err = up_impl.merge(p2ctx, remind=False) |
| 188 |
|
| 189 |
if not err: |
| 190 |
# we don't translate commit messages |
| 191 |
message = cmdutil.logmessage(ui, pycompat.byteskwargs(opts)) or ( |
| 192 |
b'Automated merge with %s' % urlutil.removeauth(other.url()) |
| 193 |
) |
| 194 |
editopt = opts.get('edit') or opts.get('force_editor') |
| 195 |
editor = cmdutil.getcommiteditor(edit=editopt, editform=b'fetch') |
| 196 |
n = repo.commit(message, opts['user'], opts['date'], editor=editor) |
| 197 |
ui.status( |
| 198 |
_(b'new changeset %d:%s merges remote changes with local\n') |
| 199 |
% (repo.changelog.rev(n), short(n)) |
| 200 |
) |
| 201 |
|
| 202 |
return err |
| 203 |
|
| 204 |
finally: |
| 205 |
release(lock, wlock) |