fetch.py
95 lines
| 3.8 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. | ||||
|
r3891 | from mercurial.i18n import _ | ||
|
r2800 | from mercurial.node import * | ||
|
r4549 | from mercurial import commands, cmdutil, hg, node, util | ||
|
r2800 | |||
def fetch(ui, repo, source='default', **opts): | ||||
'''Pull changes from a remote repository, merge new changes if needed. | ||||
This finds all changes from the repository at the specified path | ||||
or URL and adds them to the local repository. | ||||
If the pulled changes add a new head, the head is automatically | ||||
merged, and the result of the merge is committed. Otherwise, the | ||||
working directory is updated.''' | ||||
|
r4917 | def postincoming(other, modheads): | ||
|
r2800 | if modheads == 0: | ||
return 0 | ||||
if modheads == 1: | ||||
|
r4917 | return hg.clean(repo, repo.changelog.tip()) | ||
|
r2800 | newheads = repo.heads(parent) | ||
newchildren = [n for n in repo.heads(parent) if n != parent] | ||||
newparent = parent | ||||
if newchildren: | ||||
newparent = newchildren[0] | ||||
|
r4917 | hg.clean(repo, newparent) | ||
|
r2800 | newheads = [n for n in repo.heads() if n != newparent] | ||
err = False | ||||
if newheads: | ||||
ui.status(_('merging with new head %d:%s\n') % | ||||
(repo.changelog.rev(newheads[0]), short(newheads[0]))) | ||||
|
r4917 | err = hg.merge(repo, newheads[0], remind=False) | ||
|
r2800 | if not err and len(newheads) > 1: | ||
ui.status(_('not merging with %d other new heads ' | ||||
'(use "hg heads" and "hg merge" to merge them)') % | ||||
(len(newheads) - 1)) | ||||
if not err: | ||||
|
r4917 | mod, add, rem = repo.status()[:3] | ||
|
r4549 | message = (cmdutil.logmessage(opts) or | ||
|
r5798 | (_('Automated merge with %s') % | ||
util.removeauth(other.url()))) | ||||
|
r2800 | n = repo.commit(mod + add + rem, message, | ||
|
r4917 | opts['user'], opts['date'], | ||
|
r2800 | force_editor=opts.get('force_editor')) | ||
ui.status(_('new changeset %d:%s merges remote changes ' | ||||
'with local\n') % (repo.changelog.rev(n), | ||||
short(n))) | ||||
|
r4917 | def pull(): | ||
|
r4549 | cmdutil.setremoteconfig(ui, opts) | ||
|
r2800 | |||
other = hg.repository(ui, ui.expandpath(source)) | ||||
|
r5798 | ui.status(_('pulling from %s\n') % | ||
util.hidepassword(ui.expandpath(source))) | ||||
|
r2800 | revs = None | ||
if opts['rev'] and not other.local(): | ||||
raise util.Abort(_("fetch -r doesn't work for remote repositories yet")) | ||||
elif opts['rev']: | ||||
revs = [other.lookup(rev) for rev in opts['rev']] | ||||
|
r4917 | modheads = repo.pull(other, heads=revs) | ||
return postincoming(other, modheads) | ||||
|
r3223 | |||
|
r2800 | parent, p2 = repo.dirstate.parents() | ||
if parent != repo.changelog.tip(): | ||||
raise util.Abort(_('working dir not at tip ' | ||||
'(use "hg update" to check out tip)')) | ||||
if p2 != nullid: | ||||
raise util.Abort(_('outstanding uncommitted merge')) | ||||
|
r4915 | wlock = lock = None | ||
|
r2825 | try: | ||
|
r4915 | wlock = repo.wlock() | ||
lock = repo.lock() | ||||
|
r4917 | mod, add, rem = repo.status()[:3] | ||
|
r2827 | if mod or add or rem: | ||
raise util.Abort(_('outstanding uncommitted changes')) | ||||
if len(repo.heads()) > 1: | ||||
raise util.Abort(_('multiple heads in this repository ' | ||||
'(use "hg heads" and "hg merge" to merge)')) | ||||
|
r4917 | return pull() | ||
|
r2825 | finally: | ||
|
r4915 | del lock, wlock | ||
|
r2800 | |||
cmdtable = { | ||||
'fetch': | ||||
|
r4730 | (fetch, | ||
|
r5147 | [('r', 'rev', [], _('a specific revision you would like to pull')), | ||
|
r4730 | ('f', 'force-editor', None, _('edit commit message')), | ||
|
r5147 | ] + commands.commitopts + commands.commitopts2 + commands.remoteopts, | ||
|
r4730 | _('hg fetch [SOURCE]')), | ||
} | ||||