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