##// END OF EJS Templates
fetch: use update rather than clean when updating (issue3246)...
Matt Mackall -
r16091:f6e9c731 stable
parent child Browse files
Show More
@@ -1,154 +1,154 b''
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 and len(newchildren):
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.update(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 if not newheads:
105 if not newheads:
106 return 0
106 return 0
107
107
108 # Otherwise, let's merge.
108 # Otherwise, let's merge.
109 err = False
109 err = False
110 if newheads:
110 if newheads:
111 # By default, we consider the repository we're pulling
111 # By default, we consider the repository we're pulling
112 # *from* as authoritative, so we merge our changes into
112 # *from* as authoritative, so we merge our changes into
113 # theirs.
113 # theirs.
114 if opts['switch_parent']:
114 if opts['switch_parent']:
115 firstparent, secondparent = newparent, newheads[0]
115 firstparent, secondparent = newparent, newheads[0]
116 else:
116 else:
117 firstparent, secondparent = newheads[0], newparent
117 firstparent, secondparent = newheads[0], newparent
118 ui.status(_('updating to %d:%s\n') %
118 ui.status(_('updating to %d:%s\n') %
119 (repo.changelog.rev(firstparent),
119 (repo.changelog.rev(firstparent),
120 short(firstparent)))
120 short(firstparent)))
121 hg.clean(repo, firstparent)
121 hg.clean(repo, firstparent)
122 ui.status(_('merging with %d:%s\n') %
122 ui.status(_('merging with %d:%s\n') %
123 (repo.changelog.rev(secondparent), short(secondparent)))
123 (repo.changelog.rev(secondparent), short(secondparent)))
124 err = hg.merge(repo, secondparent, remind=False)
124 err = hg.merge(repo, secondparent, remind=False)
125
125
126 if not err:
126 if not err:
127 # we don't translate commit messages
127 # we don't translate commit messages
128 message = (cmdutil.logmessage(ui, opts) or
128 message = (cmdutil.logmessage(ui, opts) or
129 ('Automated merge with %s' %
129 ('Automated merge with %s' %
130 util.removeauth(other.url())))
130 util.removeauth(other.url())))
131 editor = cmdutil.commiteditor
131 editor = cmdutil.commiteditor
132 if opts.get('force_editor') or opts.get('edit'):
132 if opts.get('force_editor') or opts.get('edit'):
133 editor = cmdutil.commitforceeditor
133 editor = cmdutil.commitforceeditor
134 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
134 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
135 ui.status(_('new changeset %d:%s merges remote changes '
135 ui.status(_('new changeset %d:%s merges remote changes '
136 'with local\n') % (repo.changelog.rev(n),
136 'with local\n') % (repo.changelog.rev(n),
137 short(n)))
137 short(n)))
138
138
139 return err
139 return err
140
140
141 finally:
141 finally:
142 release(lock, wlock)
142 release(lock, wlock)
143
143
144 cmdtable = {
144 cmdtable = {
145 'fetch':
145 'fetch':
146 (fetch,
146 (fetch,
147 [('r', 'rev', [],
147 [('r', 'rev', [],
148 _('a specific revision you would like to pull'), _('REV')),
148 _('a specific revision you would like to pull'), _('REV')),
149 ('e', 'edit', None, _('edit commit message')),
149 ('e', 'edit', None, _('edit commit message')),
150 ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
150 ('', 'force-editor', None, _('edit commit message (DEPRECATED)')),
151 ('', 'switch-parent', None, _('switch parents when merging')),
151 ('', 'switch-parent', None, _('switch parents when merging')),
152 ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
152 ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
153 _('hg fetch [SOURCE]')),
153 _('hg fetch [SOURCE]')),
154 }
154 }
General Comments 0
You need to be logged in to leave comments. Login now