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