fetch.py
142 lines
| 5.5 KiB
| text/x-python
|
PythonLexer
/ hgext / fetch.py
|
r2800 | # fetch.py - pull and merge remote changes | ||
# | ||||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r6666 | '''pulling, updating and merging in one command''' | ||
|
r2800 | |||
|
r3891 | from mercurial.i18n import _ | ||
|
r6211 | from mercurial.node import nullid, short | ||
|
r7270 | from mercurial import commands, cmdutil, hg, util, url | ||
|
r2800 | |||
def fetch(ui, repo, source='default', **opts): | ||||
|
r7598 | '''pull changes from a remote repository, merge new changes if needed. | ||
|
r2800 | |||
This finds all changes from the repository at the specified path | ||||
or URL and adds them to the local repository. | ||||
|
r7007 | If the pulled changes add a new branch head, the head is automatically | ||
|
r2800 | merged, and the result of the merge is committed. Otherwise, the | ||
|
r6206 | working directory is updated to include the new changes. | ||
When a merge occurs, the newly pulled changes are assumed to be | ||||
"authoritative". The head of the new changes is used as the first | ||||
parent, with local changes as the second. To switch the merge | ||||
order, use --switch-parent. | ||||
|
r6163 | |||
See 'hg help dates' for a list of formats valid for -d/--date. | ||||
''' | ||||
|
r2800 | |||
|
r6941 | date = opts.get('date') | ||
if date: | ||||
opts['date'] = util.parsedate(date) | ||||
parent, p2 = repo.dirstate.parents() | ||||
|
r7049 | branch = repo.dirstate.branch() | ||
branchnode = repo.branchtags().get(branch) | ||||
if parent != branchnode: | ||||
|
r7007 | raise util.Abort(_('working dir not at branch tip ' | ||
'(use "hg update" to check out branch tip)')) | ||||
|
r6941 | |||
if p2 != nullid: | ||||
raise util.Abort(_('outstanding uncommitted merge')) | ||||
wlock = lock = None | ||||
try: | ||||
wlock = repo.wlock() | ||||
lock = repo.lock() | ||||
mod, add, rem, del_ = repo.status()[:4] | ||||
if mod or add or rem: | ||||
raise util.Abort(_('outstanding uncommitted changes')) | ||||
if del_: | ||||
raise util.Abort(_('working directory is missing some files')) | ||||
|
r7007 | if len(repo.branchheads(branch)) > 1: | ||
raise util.Abort(_('multiple heads in this branch ' | ||||
'(use "hg heads ." and "hg merge" to merge)')) | ||||
|
r6941 | |||
cmdutil.setremoteconfig(ui, opts) | ||||
other = hg.repository(ui, ui.expandpath(source)) | ||||
ui.status(_('pulling from %s\n') % | ||||
|
r7270 | url.hidepassword(ui.expandpath(source))) | ||
|
r6941 | revs = None | ||
if opts['rev']: | ||||
if not other.local(): | ||||
raise util.Abort(_("fetch -r doesn't work for remote " | ||||
"repositories yet")) | ||||
else: | ||||
revs = [other.lookup(rev) for rev in opts['rev']] | ||||
|
r7007 | # Are there any changes at all? | ||
|
r6941 | modheads = repo.pull(other, heads=revs) | ||
|
r2800 | if modheads == 0: | ||
return 0 | ||||
|
r6941 | |||
|
r7007 | # Is this a simple fast-forward along the current branch? | ||
newheads = repo.branchheads(branch) | ||||
newchildren = repo.changelog.nodesbetween([parent], newheads)[2] | ||||
if len(newheads) == 1: | ||||
if newchildren[0] != parent: | ||||
return hg.clean(repo, newchildren[0]) | ||||
else: | ||||
return | ||||
# Are there more than one additional branch heads? | ||||
newchildren = [n for n in newchildren if n != parent] | ||||
|
r2800 | newparent = parent | ||
if newchildren: | ||||
newparent = newchildren[0] | ||||
|
r4917 | hg.clean(repo, newparent) | ||
|
r7007 | newheads = [n for n in newheads if n != newparent] | ||
|
r6206 | if len(newheads) > 1: | ||
|
r7007 | ui.status(_('not merging with %d other new branch heads ' | ||
'(use "hg heads ." and "hg merge" to merge them)\n') % | ||||
|
r2800 | (len(newheads) - 1)) | ||
|
r6206 | return | ||
|
r7007 | |||
# Otherwise, let's merge. | ||||
|
r6206 | err = False | ||
if newheads: | ||||
# By default, we consider the repository we're pulling | ||||
# *from* as authoritative, so we merge our changes into | ||||
# theirs. | ||||
if opts['switch_parent']: | ||||
firstparent, secondparent = newparent, newheads[0] | ||||
else: | ||||
firstparent, secondparent = newheads[0], newparent | ||||
ui.status(_('updating to %d:%s\n') % | ||||
(repo.changelog.rev(firstparent), | ||||
short(firstparent))) | ||||
hg.clean(repo, firstparent) | ||||
ui.status(_('merging with %d:%s\n') % | ||||
(repo.changelog.rev(secondparent), short(secondparent))) | ||||
err = hg.merge(repo, secondparent, remind=False) | ||||
|
r6941 | |||
|
r2800 | if not err: | ||
|
r4917 | mod, add, rem = repo.status()[:3] | ||
|
r4549 | message = (cmdutil.logmessage(opts) or | ||
|
r5798 | (_('Automated merge with %s') % | ||
|
r7270 | url.removeauth(other.url()))) | ||
|
r6225 | force_editor = opts.get('force_editor') or opts.get('edit') | ||
|
r2800 | n = repo.commit(mod + add + rem, message, | ||
|
r6385 | opts['user'], opts['date'], force=True, | ||
|
r6225 | force_editor=force_editor) | ||
|
r2800 | ui.status(_('new changeset %d:%s merges remote changes ' | ||
'with local\n') % (repo.changelog.rev(n), | ||||
short(n))) | ||||
|
r6206 | |||
|
r2825 | finally: | ||
|
r4915 | del lock, wlock | ||
|
r2800 | |||
cmdtable = { | ||||
'fetch': | ||||
|
r4730 | (fetch, | ||
|
r5147 | [('r', 'rev', [], _('a specific revision you would like to pull')), | ||
|
r6225 | ('e', 'edit', None, _('edit commit message')), | ||
('', 'force-editor', None, _('edit commit message (DEPRECATED)')), | ||||
|
r6206 | ('', 'switch-parent', None, _('switch parents when merging')), | ||
|
r5147 | ] + commands.commitopts + commands.commitopts2 + commands.remoteopts, | ||
|
r4730 | _('hg fetch [SOURCE]')), | ||
} | ||||