1 # fetch.py - pull and merge remote changes
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
8 '''pull, update and merge in one command'''
10 from mercurial.i18n import _
11 from mercurial.node import nullid, short
12 from mercurial import commands, cmdutil, hg, util, url, error
13 from mercurial.lock import release
15 def fetch(ui, repo, source='default', **opts):
16 '''pull changes from a remote repository, merge new changes if needed.
18 This finds all changes from the repository at the specified path
19 or URL and adds them to the local repository.
21 If the pulled changes add a new branch head, the head is
22 automatically merged, and the result of the merge is committed.
23 Otherwise, the working directory is updated to include the new
26 When a merge occurs, the newly pulled changes are assumed to be
27 "authoritative". The head of the new changes is used as the first
28 parent, with local changes as the second. To switch the merge
29 order, use --switch-parent.
31 See 'hg help dates' for a list of formats valid for -d/--date.
34 date = opts.get('date')
36 opts['date'] = util.parsedate(date)
38 parent, p2 = repo.dirstate.parents()
39 branch = repo.dirstate.branch()
40 branchnode = repo.branchtags().get(branch)
41 if parent != branchnode:
42 raise util.Abort(_('working dir not at branch tip '
43 '(use "hg update" to check out branch tip)'))
46 raise util.Abort(_('outstanding uncommitted merge'))
52 mod, add, rem, del_ = repo.status()[:4]
55 raise util.Abort(_('outstanding uncommitted changes'))
57 raise util.Abort(_('working directory is missing some files'))
58 bheads = repo.branchheads(branch)
59 bheads = [head for head in bheads if len(repo[head].children()) == 0]
61 raise util.Abort(_('multiple heads in this branch '
62 '(use "hg heads ." and "hg merge" to merge)'))
64 other = hg.repository(cmdutil.remoteui(repo, opts),
65 ui.expandpath(source))
66 ui.status(_('pulling from %s\n') %
67 url.hidepassword(ui.expandpath(source)))
71 revs = [other.lookup(rev) for rev in opts['rev']]
72 except error.CapabilityError:
73 err = _("Other repository doesn't support revision lookup, "
74 "so a rev cannot be specified.")
77 # Are there any changes at all?
78 modheads = repo.pull(other, heads=revs)
82 # Is this a simple fast-forward along the current branch?
83 newheads = repo.branchheads(branch)
84 newheads = [head for head in newheads if len(repo[head].children()) == 0]
85 newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
86 if len(newheads) == 1:
87 if newchildren[0] != parent:
88 return hg.clean(repo, newchildren[0])
92 # Are there more than one additional branch heads?
93 newchildren = [n for n in newchildren if n != parent]
96 newparent = newchildren[0]
97 hg.clean(repo, newparent)
98 newheads = [n for n in newheads if n != newparent]
100 ui.status(_('not merging with %d other new branch heads '
101 '(use "hg heads ." and "hg merge" to merge them)\n') %
105 # Otherwise, let's merge.
108 # By default, we consider the repository we're pulling
109 # *from* as authoritative, so we merge our changes into
111 if opts['switch_parent']:
112 firstparent, secondparent = newparent, newheads[0]
114 firstparent, secondparent = newheads[0], newparent
115 ui.status(_('updating to %d:%s\n') %
116 (repo.changelog.rev(firstparent),
118 hg.clean(repo, firstparent)
119 ui.status(_('merging with %d:%s\n') %
120 (repo.changelog.rev(secondparent), short(secondparent)))
121 err = hg.merge(repo, secondparent, remind=False)
124 # we don't translate commit messages
125 message = (cmdutil.logmessage(opts) or
126 ('Automated merge with %s' %
127 url.removeauth(other.url())))
128 editor = cmdutil.commiteditor
129 if opts.get('force_editor') or opts.get('edit'):
130 editor = cmdutil.commitforceeditor
131 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
132 ui.status(_('new changeset %d:%s merges remote changes '
133 'with local\n') % (repo.changelog.rev(n),
142 [('r', 'rev', [], _('a specific revision you would like to pull')),
143 ('e', 'edit', None, _('edit commit message')),
144 ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
145 ('', 'switch-parent', None, _('switch parents when merging')),
146 ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
147 _('hg fetch [SOURCE]')),