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