##// END OF EJS Templates
fetch: patch cornercase in children calculation (issue2773)
Matt Mackall -
r15748:6eb5bbaa stable
parent child Browse files
Show More
@@ -1,151 +1,151
1 # fetch.py - pull and merge remote changes
1 # fetch.py - pull and merge remote changes
2 #
2 #
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7
7
8 '''pull, update and merge in one command'''
8 '''pull, update and merge in one command'''
9
9
10 from mercurial.i18n import _
10 from mercurial.i18n import _
11 from mercurial.node import nullid, short
11 from mercurial.node import nullid, short
12 from mercurial import commands, cmdutil, hg, util, error
12 from mercurial import commands, cmdutil, hg, util, error
13 from mercurial.lock import release
13 from mercurial.lock import release
14
14
15 def fetch(ui, repo, source='default', **opts):
15 def fetch(ui, repo, source='default', **opts):
16 '''pull changes from a remote repository, merge new changes if needed.
16 '''pull changes from a remote repository, merge new changes if needed.
17
17
18 This finds all changes from the repository at the specified path
18 This finds all changes from the repository at the specified path
19 or URL and adds them to the local repository.
19 or URL and adds them to the local repository.
20
20
21 If the pulled changes add a new branch head, the head is
21 If the pulled changes add a new branch head, the head is
22 automatically merged, and the result of the merge is committed.
22 automatically merged, and the result of the merge is committed.
23 Otherwise, the working directory is updated to include the new
23 Otherwise, the working directory is updated to include the new
24 changes.
24 changes.
25
25
26 When a merge occurs, the newly pulled changes are assumed to be
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
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
28 parent, with local changes as the second. To switch the merge
29 order, use --switch-parent.
29 order, use --switch-parent.
30
30
31 See :hg:`help dates` for a list of formats valid for -d/--date.
31 See :hg:`help dates` for a list of formats valid for -d/--date.
32
32
33 Returns 0 on success.
33 Returns 0 on success.
34 '''
34 '''
35
35
36 date = opts.get('date')
36 date = opts.get('date')
37 if date:
37 if date:
38 opts['date'] = util.parsedate(date)
38 opts['date'] = util.parsedate(date)
39
39
40 parent, p2 = repo.dirstate.parents()
40 parent, p2 = repo.dirstate.parents()
41 branch = repo.dirstate.branch()
41 branch = repo.dirstate.branch()
42 branchnode = repo.branchtags().get(branch)
42 branchnode = repo.branchtags().get(branch)
43 if parent != branchnode:
43 if parent != branchnode:
44 raise util.Abort(_('working dir not at branch tip '
44 raise util.Abort(_('working dir not at branch tip '
45 '(use "hg update" to check out branch tip)'))
45 '(use "hg update" to check out branch tip)'))
46
46
47 if p2 != nullid:
47 if p2 != nullid:
48 raise util.Abort(_('outstanding uncommitted merge'))
48 raise util.Abort(_('outstanding uncommitted merge'))
49
49
50 wlock = lock = None
50 wlock = lock = None
51 try:
51 try:
52 wlock = repo.wlock()
52 wlock = repo.wlock()
53 lock = repo.lock()
53 lock = repo.lock()
54 mod, add, rem, del_ = repo.status()[:4]
54 mod, add, rem, del_ = repo.status()[:4]
55
55
56 if mod or add or rem:
56 if mod or add or rem:
57 raise util.Abort(_('outstanding uncommitted changes'))
57 raise util.Abort(_('outstanding uncommitted changes'))
58 if del_:
58 if del_:
59 raise util.Abort(_('working directory is missing some files'))
59 raise util.Abort(_('working directory is missing some files'))
60 bheads = repo.branchheads(branch)
60 bheads = repo.branchheads(branch)
61 bheads = [head for head in bheads if len(repo[head].children()) == 0]
61 bheads = [head for head in bheads if len(repo[head].children()) == 0]
62 if len(bheads) > 1:
62 if len(bheads) > 1:
63 raise util.Abort(_('multiple heads in this branch '
63 raise util.Abort(_('multiple heads in this branch '
64 '(use "hg heads ." and "hg merge" to merge)'))
64 '(use "hg heads ." and "hg merge" to merge)'))
65
65
66 other = hg.peer(repo, opts, ui.expandpath(source))
66 other = hg.peer(repo, opts, ui.expandpath(source))
67 ui.status(_('pulling from %s\n') %
67 ui.status(_('pulling from %s\n') %
68 util.hidepassword(ui.expandpath(source)))
68 util.hidepassword(ui.expandpath(source)))
69 revs = None
69 revs = None
70 if opts['rev']:
70 if opts['rev']:
71 try:
71 try:
72 revs = [other.lookup(rev) for rev in opts['rev']]
72 revs = [other.lookup(rev) for rev in opts['rev']]
73 except error.CapabilityError:
73 except error.CapabilityError:
74 err = _("Other repository doesn't support revision lookup, "
74 err = _("Other repository doesn't support revision lookup, "
75 "so a rev cannot be specified.")
75 "so a rev cannot be specified.")
76 raise util.Abort(err)
76 raise util.Abort(err)
77
77
78 # Are there any changes at all?
78 # Are there any changes at all?
79 modheads = repo.pull(other, heads=revs)
79 modheads = repo.pull(other, heads=revs)
80 if modheads == 0:
80 if modheads == 0:
81 return 0
81 return 0
82
82
83 # Is this a simple fast-forward along the current branch?
83 # Is this a simple fast-forward along the current branch?
84 newheads = repo.branchheads(branch)
84 newheads = repo.branchheads(branch)
85 newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
85 newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
86 if len(newheads) == 1:
86 if len(newheads) == 1 and len(newchildren):
87 if newchildren[0] != parent:
87 if newchildren[0] != parent:
88 return hg.clean(repo, newchildren[0])
88 return hg.clean(repo, newchildren[0])
89 else:
89 else:
90 return 0
90 return 0
91
91
92 # Are there more than one additional branch heads?
92 # Are there more than one additional branch heads?
93 newchildren = [n for n in newchildren if n != parent]
93 newchildren = [n for n in newchildren if n != parent]
94 newparent = parent
94 newparent = parent
95 if newchildren:
95 if newchildren:
96 newparent = newchildren[0]
96 newparent = newchildren[0]
97 hg.clean(repo, newparent)
97 hg.clean(repo, newparent)
98 newheads = [n for n in newheads if n != newparent]
98 newheads = [n for n in newheads if n != newparent]
99 if len(newheads) > 1:
99 if len(newheads) > 1:
100 ui.status(_('not merging with %d other new branch heads '
100 ui.status(_('not merging with %d other new branch heads '
101 '(use "hg heads ." and "hg merge" to merge them)\n') %
101 '(use "hg heads ." and "hg merge" to merge them)\n') %
102 (len(newheads) - 1))
102 (len(newheads) - 1))
103 return 1
103 return 1
104
104
105 # Otherwise, let's merge.
105 # Otherwise, let's merge.
106 err = False
106 err = False
107 if newheads:
107 if newheads:
108 # By default, we consider the repository we're pulling
108 # By default, we consider the repository we're pulling
109 # *from* as authoritative, so we merge our changes into
109 # *from* as authoritative, so we merge our changes into
110 # theirs.
110 # theirs.
111 if opts['switch_parent']:
111 if opts['switch_parent']:
112 firstparent, secondparent = newparent, newheads[0]
112 firstparent, secondparent = newparent, newheads[0]
113 else:
113 else:
114 firstparent, secondparent = newheads[0], newparent
114 firstparent, secondparent = newheads[0], newparent
115 ui.status(_('updating to %d:%s\n') %
115 ui.status(_('updating to %d:%s\n') %
116 (repo.changelog.rev(firstparent),
116 (repo.changelog.rev(firstparent),
117 short(firstparent)))
117 short(firstparent)))
118 hg.clean(repo, firstparent)
118 hg.clean(repo, firstparent)
119 ui.status(_('merging with %d:%s\n') %
119 ui.status(_('merging with %d:%s\n') %
120 (repo.changelog.rev(secondparent), short(secondparent)))
120 (repo.changelog.rev(secondparent), short(secondparent)))
121 err = hg.merge(repo, secondparent, remind=False)
121 err = hg.merge(repo, secondparent, remind=False)
122
122
123 if not err:
123 if not err:
124 # we don't translate commit messages
124 # we don't translate commit messages
125 message = (cmdutil.logmessage(ui, opts) or
125 message = (cmdutil.logmessage(ui, opts) or
126 ('Automated merge with %s' %
126 ('Automated merge with %s' %
127 util.removeauth(other.url())))
127 util.removeauth(other.url())))
128 editor = cmdutil.commiteditor
128 editor = cmdutil.commiteditor
129 if opts.get('force_editor') or opts.get('edit'):
129 if opts.get('force_editor') or opts.get('edit'):
130 editor = cmdutil.commitforceeditor
130 editor = cmdutil.commitforceeditor
131 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
131 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
132 ui.status(_('new changeset %d:%s merges remote changes '
132 ui.status(_('new changeset %d:%s merges remote changes '
133 'with local\n') % (repo.changelog.rev(n),
133 'with local\n') % (repo.changelog.rev(n),
134 short(n)))
134 short(n)))
135
135
136 return err
136 return err
137
137
138 finally:
138 finally:
139 release(lock, wlock)
139 release(lock, wlock)
140
140
141 cmdtable = {
141 cmdtable = {
142 'fetch':
142 'fetch':
143 (fetch,
143 (fetch,
144 [('r', 'rev', [],
144 [('r', 'rev', [],
145 _('a specific revision you would like to pull'), _('REV')),
145 _('a specific revision you would like to pull'), _('REV')),
146 ('e', 'edit', None, _('edit commit message')),
146 ('e', 'edit', None, _('edit commit message')),
147 ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
147 ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
148 ('', 'switch-parent', None, _('switch parents when merging')),
148 ('', 'switch-parent', None, _('switch parents when merging')),
149 ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
149 ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
150 _('hg fetch [SOURCE]')),
150 _('hg fetch [SOURCE]')),
151 }
151 }
General Comments 0
You need to be logged in to leave comments. Login now