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