##// END OF EJS Templates
fetch: migrate `opts` to native kwargs
Matt Harbison -
r51769:eeffc968 default
parent child Browse files
Show More
@@ -1,198 +1,195 b''
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 (DEPRECATED)'''
9 9
10 10
11 11 from mercurial.i18n import _
12 12 from mercurial.node import short
13 13 from mercurial import (
14 14 cmdutil,
15 15 error,
16 16 exchange,
17 17 hg,
18 18 lock,
19 19 pycompat,
20 20 registrar,
21 21 )
22 22 from mercurial.utils import (
23 23 dateutil,
24 24 urlutil,
25 25 )
26 26
27 27 release = lock.release
28 28 cmdtable = {}
29 29 command = registrar.command(cmdtable)
30 30 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
31 31 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
32 32 # be specifying the version(s) of Mercurial they are tested with, or
33 33 # leave the attribute unspecified.
34 34 testedwith = b'ships-with-hg-core'
35 35
36 36
37 37 @command(
38 38 b'fetch',
39 39 [
40 40 (
41 41 b'r',
42 42 b'rev',
43 43 [],
44 44 _(b'a specific revision you would like to pull'),
45 45 _(b'REV'),
46 46 ),
47 47 (b'', b'edit', None, _(b'invoke editor on commit messages')),
48 48 (b'', b'force-editor', None, _(b'edit commit message (DEPRECATED)')),
49 49 (b'', b'switch-parent', None, _(b'switch parents when merging')),
50 50 ]
51 51 + cmdutil.commitopts
52 52 + cmdutil.commitopts2
53 53 + cmdutil.remoteopts,
54 54 _(b'hg fetch [SOURCE]'),
55 55 helpcategory=command.CATEGORY_REMOTE_REPO_MANAGEMENT,
56 56 )
57 57 def fetch(ui, repo, source=b'default', **opts):
58 58 """pull changes from a remote repository, merge new changes if needed.
59 59
60 60 This finds all changes from the repository at the specified path
61 61 or URL and adds them to the local repository.
62 62
63 63 If the pulled changes add a new branch head, the head is
64 64 automatically merged, and the result of the merge is committed.
65 65 Otherwise, the working directory is updated to include the new
66 66 changes.
67 67
68 68 When a merge is needed, the working directory is first updated to
69 69 the newly pulled changes. Local changes are then merged into the
70 70 pulled changes. To switch the merge order, use --switch-parent.
71 71
72 72 See :hg:`help dates` for a list of formats valid for -d/--date.
73 73
74 74 Returns 0 on success.
75 75 """
76 76
77 opts = pycompat.byteskwargs(opts)
78 date = opts.get(b'date')
77 date = opts.get('date')
79 78 if date:
80 opts[b'date'] = dateutil.parsedate(date)
79 opts['date'] = dateutil.parsedate(date)
81 80
82 81 parent = repo.dirstate.p1()
83 82 branch = repo.dirstate.branch()
84 83 try:
85 84 branchnode = repo.branchtip(branch)
86 85 except error.RepoLookupError:
87 86 branchnode = None
88 87 if parent != branchnode:
89 88 raise error.Abort(
90 89 _(b'working directory not at branch tip'),
91 90 hint=_(b"use 'hg update' to check out branch tip"),
92 91 )
93 92
94 93 wlock = lock = None
95 94 try:
96 95 wlock = repo.wlock()
97 96 lock = repo.lock()
98 97
99 98 cmdutil.bailifchanged(repo)
100 99
101 100 bheads = repo.branchheads(branch)
102 101 bheads = [head for head in bheads if len(repo[head].children()) == 0]
103 102 if len(bheads) > 1:
104 103 raise error.Abort(
105 104 _(
106 105 b'multiple heads in this branch '
107 106 b'(use "hg heads ." and "hg merge" to merge)'
108 107 )
109 108 )
110 109
111 110 path = urlutil.get_unique_pull_path_obj(b'fetch', ui, source)
112 other = hg.peer(repo, opts, path)
111 other = hg.peer(repo, pycompat.byteskwargs(opts), path)
113 112 ui.status(_(b'pulling from %s\n') % urlutil.hidepassword(path.loc))
114 113 revs = None
115 if opts[b'rev']:
114 if opts['rev']:
116 115 try:
117 revs = [other.lookup(rev) for rev in opts[b'rev']]
116 revs = [other.lookup(rev) for rev in opts['rev']]
118 117 except error.CapabilityError:
119 118 err = _(
120 119 b"other repository doesn't support revision lookup, "
121 120 b"so a rev cannot be specified."
122 121 )
123 122 raise error.Abort(err)
124 123
125 124 # Are there any changes at all?
126 125 modheads = exchange.pull(repo, other, heads=revs).cgresult
127 126 if modheads == 0:
128 127 return 0
129 128
130 129 # Is this a simple fast-forward along the current branch?
131 130 newheads = repo.branchheads(branch)
132 131 newchildren = repo.changelog.nodesbetween([parent], newheads)[2]
133 132 if len(newheads) == 1 and len(newchildren):
134 133 if newchildren[0] != parent:
135 134 return hg.update(repo, newchildren[0])
136 135 else:
137 136 return 0
138 137
139 138 # Are there more than one additional branch heads?
140 139 newchildren = [n for n in newchildren if n != parent]
141 140 newparent = parent
142 141 if newchildren:
143 142 newparent = newchildren[0]
144 143 hg.clean(repo, newparent)
145 144 newheads = [n for n in newheads if n != newparent]
146 145 if len(newheads) > 1:
147 146 ui.status(
148 147 _(
149 148 b'not merging with %d other new branch heads '
150 149 b'(use "hg heads ." and "hg merge" to merge them)\n'
151 150 )
152 151 % (len(newheads) - 1)
153 152 )
154 153 return 1
155 154
156 155 if not newheads:
157 156 return 0
158 157
159 158 # Otherwise, let's merge.
160 159 err = False
161 160 if newheads:
162 161 # By default, we consider the repository we're pulling
163 162 # *from* as authoritative, so we merge our changes into
164 163 # theirs.
165 if opts[b'switch_parent']:
164 if opts['switch_parent']:
166 165 firstparent, secondparent = newparent, newheads[0]
167 166 else:
168 167 firstparent, secondparent = newheads[0], newparent
169 168 ui.status(
170 169 _(b'updating to %d:%s\n')
171 170 % (repo.changelog.rev(firstparent), short(firstparent))
172 171 )
173 172 hg.clean(repo, firstparent)
174 173 p2ctx = repo[secondparent]
175 174 ui.status(
176 175 _(b'merging with %d:%s\n') % (p2ctx.rev(), short(secondparent))
177 176 )
178 177 err = hg.merge(p2ctx, remind=False)
179 178
180 179 if not err:
181 180 # we don't translate commit messages
182 message = cmdutil.logmessage(ui, opts) or (
181 message = cmdutil.logmessage(ui, pycompat.byteskwargs(opts)) or (
183 182 b'Automated merge with %s' % urlutil.removeauth(other.url())
184 183 )
185 editopt = opts.get(b'edit') or opts.get(b'force_editor')
184 editopt = opts.get('edit') or opts.get('force_editor')
186 185 editor = cmdutil.getcommiteditor(edit=editopt, editform=b'fetch')
187 n = repo.commit(
188 message, opts[b'user'], opts[b'date'], editor=editor
189 )
186 n = repo.commit(message, opts['user'], opts['date'], editor=editor)
190 187 ui.status(
191 188 _(b'new changeset %d:%s merges remote changes with local\n')
192 189 % (repo.changelog.rev(n), short(n))
193 190 )
194 191
195 192 return err
196 193
197 194 finally:
198 195 release(lock, wlock)
General Comments 0
You need to be logged in to leave comments. Login now