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