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