Show More
@@ -1,198 +1,198 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 | 77 | opts = pycompat.byteskwargs(opts) |
|
78 | 78 | date = opts.get(b'date') |
|
79 | 79 | if date: |
|
80 | 80 | opts[b'date'] = dateutil.parsedate(date) |
|
81 | 81 | |
|
82 | 82 | parent = repo.dirstate.p1() |
|
83 | 83 | branch = repo.dirstate.branch() |
|
84 | 84 | try: |
|
85 | 85 | branchnode = repo.branchtip(branch) |
|
86 | 86 | except error.RepoLookupError: |
|
87 | 87 | branchnode = None |
|
88 | 88 | if parent != branchnode: |
|
89 | 89 | raise error.Abort( |
|
90 | 90 | _(b'working directory not at branch tip'), |
|
91 | 91 | hint=_(b"use 'hg update' to check out branch tip"), |
|
92 | 92 | ) |
|
93 | 93 | |
|
94 | 94 | wlock = lock = None |
|
95 | 95 | try: |
|
96 | 96 | wlock = repo.wlock() |
|
97 | 97 | lock = repo.lock() |
|
98 | 98 | |
|
99 | 99 | cmdutil.bailifchanged(repo) |
|
100 | 100 | |
|
101 | 101 | bheads = repo.branchheads(branch) |
|
102 | 102 | bheads = [head for head in bheads if len(repo[head].children()) == 0] |
|
103 | 103 | if len(bheads) > 1: |
|
104 | 104 | raise error.Abort( |
|
105 | 105 | _( |
|
106 | 106 | b'multiple heads in this branch ' |
|
107 | 107 | b'(use "hg heads ." and "hg merge" to merge)' |
|
108 | 108 | ) |
|
109 | 109 | ) |
|
110 | 110 | |
|
111 |
path = urlutil.get_unique_pull_path(b'fetch', |
|
|
111 | path = urlutil.get_unique_pull_path_obj(b'fetch', ui, source) | |
|
112 | 112 | other = hg.peer(repo, opts, path) |
|
113 | ui.status(_(b'pulling from %s\n') % urlutil.hidepassword(path)) | |
|
113 | ui.status(_(b'pulling from %s\n') % urlutil.hidepassword(path.loc)) | |
|
114 | 114 | revs = None |
|
115 | 115 | if opts[b'rev']: |
|
116 | 116 | try: |
|
117 | 117 | revs = [other.lookup(rev) for rev in opts[b'rev']] |
|
118 | 118 | except error.CapabilityError: |
|
119 | 119 | err = _( |
|
120 | 120 | b"other repository doesn't support revision lookup, " |
|
121 | 121 | b"so a rev cannot be specified." |
|
122 | 122 | ) |
|
123 | 123 | raise error.Abort(err) |
|
124 | 124 | |
|
125 | 125 | # Are there any changes at all? |
|
126 | 126 | modheads = exchange.pull(repo, other, heads=revs).cgresult |
|
127 | 127 | if modheads == 0: |
|
128 | 128 | return 0 |
|
129 | 129 | |
|
130 | 130 | # Is this a simple fast-forward along the current branch? |
|
131 | 131 | newheads = repo.branchheads(branch) |
|
132 | 132 | newchildren = repo.changelog.nodesbetween([parent], newheads)[2] |
|
133 | 133 | if len(newheads) == 1 and len(newchildren): |
|
134 | 134 | if newchildren[0] != parent: |
|
135 | 135 | return hg.update(repo, newchildren[0]) |
|
136 | 136 | else: |
|
137 | 137 | return 0 |
|
138 | 138 | |
|
139 | 139 | # Are there more than one additional branch heads? |
|
140 | 140 | newchildren = [n for n in newchildren if n != parent] |
|
141 | 141 | newparent = parent |
|
142 | 142 | if newchildren: |
|
143 | 143 | newparent = newchildren[0] |
|
144 | 144 | hg.clean(repo, newparent) |
|
145 | 145 | newheads = [n for n in newheads if n != newparent] |
|
146 | 146 | if len(newheads) > 1: |
|
147 | 147 | ui.status( |
|
148 | 148 | _( |
|
149 | 149 | b'not merging with %d other new branch heads ' |
|
150 | 150 | b'(use "hg heads ." and "hg merge" to merge them)\n' |
|
151 | 151 | ) |
|
152 | 152 | % (len(newheads) - 1) |
|
153 | 153 | ) |
|
154 | 154 | return 1 |
|
155 | 155 | |
|
156 | 156 | if not newheads: |
|
157 | 157 | return 0 |
|
158 | 158 | |
|
159 | 159 | # Otherwise, let's merge. |
|
160 | 160 | err = False |
|
161 | 161 | if newheads: |
|
162 | 162 | # By default, we consider the repository we're pulling |
|
163 | 163 | # *from* as authoritative, so we merge our changes into |
|
164 | 164 | # theirs. |
|
165 | 165 | if opts[b'switch_parent']: |
|
166 | 166 | firstparent, secondparent = newparent, newheads[0] |
|
167 | 167 | else: |
|
168 | 168 | firstparent, secondparent = newheads[0], newparent |
|
169 | 169 | ui.status( |
|
170 | 170 | _(b'updating to %d:%s\n') |
|
171 | 171 | % (repo.changelog.rev(firstparent), short(firstparent)) |
|
172 | 172 | ) |
|
173 | 173 | hg.clean(repo, firstparent) |
|
174 | 174 | p2ctx = repo[secondparent] |
|
175 | 175 | ui.status( |
|
176 | 176 | _(b'merging with %d:%s\n') % (p2ctx.rev(), short(secondparent)) |
|
177 | 177 | ) |
|
178 | 178 | err = hg.merge(p2ctx, remind=False) |
|
179 | 179 | |
|
180 | 180 | if not err: |
|
181 | 181 | # we don't translate commit messages |
|
182 | 182 | message = cmdutil.logmessage(ui, opts) or ( |
|
183 | 183 | b'Automated merge with %s' % urlutil.removeauth(other.url()) |
|
184 | 184 | ) |
|
185 | 185 | editopt = opts.get(b'edit') or opts.get(b'force_editor') |
|
186 | 186 | editor = cmdutil.getcommiteditor(edit=editopt, editform=b'fetch') |
|
187 | 187 | n = repo.commit( |
|
188 | 188 | message, opts[b'user'], opts[b'date'], editor=editor |
|
189 | 189 | ) |
|
190 | 190 | ui.status( |
|
191 | 191 | _(b'new changeset %d:%s merges remote changes with local\n') |
|
192 | 192 | % (repo.changelog.rev(n), short(n)) |
|
193 | 193 | ) |
|
194 | 194 | |
|
195 | 195 | return err |
|
196 | 196 | |
|
197 | 197 | finally: |
|
198 | 198 | release(lock, wlock) |
General Comments 0
You need to be logged in to leave comments.
Login now