chiark / gitweb /
Remove the reordering side-effect of the latter sync changes
[stgit] / stgit / commands / sync.py
CommitLineData
06848fab
CM
1__copyright__ = """
2Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
18import sys, os
19from optparse import OptionParser, make_option
20
21import stgit.commands.common
22from stgit.commands.common import *
23from stgit.utils import *
5e888f30 24from stgit.out import *
06848fab
CM
25from stgit import stack, git
26
27
28help = 'synchronise patches with a branch or a series'
29usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
30
31For each of the specified patches perform a three-way merge with the
32same patch in the specified branch or series. The command can be used
33for keeping patches on several branches in sync. Note that the
34operation may fail for some patches because of conflicts. The patches
35in the series must apply cleanly.
36
37The sync operation can be reverted for individual patches with --undo."""
38
7b601c9e 39directory = DirectoryGotoToplevel()
06848fab 40options = [make_option('-a', '--all',
b85a27bc 41 help = 'synchronise all the applied patches',
06848fab 42 action = 'store_true'),
6b79a09c 43 make_option('-B', '--ref-branch',
06848fab
CM
44 help = 'syncronise patches with BRANCH'),
45 make_option('-s', '--series',
46 help = 'syncronise patches with SERIES'),
47 make_option('--undo',
48 help = 'undo the synchronisation of the current patch',
49 action = 'store_true')]
50
51def __check_all():
52 check_local_changes()
53 check_conflicts()
6972fd6b 54 check_head_top_equal(crt_series)
06848fab
CM
55
56def __branch_merge_patch(remote_series, pname):
57 """Merge a patch from a remote branch into the current tree.
58 """
59 patch = remote_series.get_patch(pname)
60 git.merge(patch.get_bottom(), git.get_head(), patch.get_top())
61
62def __series_merge_patch(base, patchdir, pname):
63 """Merge a patch file with the given StGIT patch.
64 """
65 patchfile = os.path.join(patchdir, pname)
66 git.apply_patch(filename = patchfile, base = base)
67
68def func(parser, options, args):
69 """Synchronise a range of patches
70 """
06848fab 71 if options.undo:
6b79a09c 72 if options.ref_branch or options.series:
06848fab 73 raise CmdException, \
6b79a09c 74 '--undo cannot be specified with --ref-branch or --series'
06848fab
CM
75 __check_all()
76
27ac2b7e 77 out.start('Undoing the sync of "%s"' % crt_series.get_current())
06848fab
CM
78 crt_series.undo_refresh()
79 git.reset()
27ac2b7e 80 out.done()
06848fab
CM
81 return
82
6b79a09c 83 if options.ref_branch:
6b79a09c
YD
84 remote_series = stack.Series(options.ref_branch)
85 if options.ref_branch == crt_series.get_name():
06848fab
CM
86 raise CmdException, 'Cannot synchronise with the current branch'
87 remote_patches = remote_series.get_applied()
88
89 # the merge function merge_patch(patch, pname)
90 merge_patch = lambda patch, pname: \
91 __branch_merge_patch(remote_series, pname)
92 elif options.series:
93 patchdir = os.path.dirname(options.series)
94
95 remote_patches = []
96 f = file(options.series)
97 for line in f:
98 p = re.sub('#.*$', '', line).strip()
99 if not p:
100 continue
101 remote_patches.append(p)
102 f.close()
103
104 # the merge function merge_patch(patch, pname)
105 merge_patch = lambda patch, pname: \
106 __series_merge_patch(patch.get_bottom(), patchdir, pname)
107 else:
108 raise CmdException, 'No remote branch or series specified'
109
110 applied = crt_series.get_applied()
03af1025 111 unapplied = crt_series.get_unapplied()
06848fab
CM
112
113 if options.all:
114 patches = applied
115 elif len(args) != 0:
b85a27bc
CM
116 patches = parse_patches(args, applied + unapplied, len(applied),
117 ordered = True)
00d468f5
CM
118 elif applied:
119 patches = [crt_series.get_current()]
06848fab 120 else:
00d468f5 121 parser.error('no patches applied')
06848fab
CM
122
123 if not patches:
124 raise CmdException, 'No patches to synchronise'
125
126 __check_all()
127
128 # only keep the patches to be synchronised
129 sync_patches = [p for p in patches if p in remote_patches]
130 if not sync_patches:
131 raise CmdException, 'No common patches to be synchronised'
132
133 # pop to the one before the first patch to be synchronised
b85a27bc
CM
134 first_patch = sync_patches[0]
135 if first_patch in applied:
136 to_pop = applied[applied.index(first_patch) + 1:]
137 if to_pop:
138 pop_patches(crt_series, to_pop[::-1])
b85a27bc 139 else:
38645302
CM
140 to_pop = []
141 popped = to_pop + [p for p in patches if p in unapplied]
06848fab 142
38645302 143 for p in [first_patch] + popped:
06848fab 144 if p in popped:
b85a27bc
CM
145 # push this patch
146 push_patches(crt_series, [p])
147 if p not in sync_patches:
148 # nothing to synchronise
149 continue
06848fab
CM
150
151 # the actual sync
27ac2b7e 152 out.start('Synchronising "%s"' % p)
06848fab
CM
153
154 patch = crt_series.get_patch(p)
155 bottom = patch.get_bottom()
156 top = patch.get_top()
157
158 # reset the patch backup information. That's needed in case we
159 # undo the sync but there were no changes made
160 patch.set_bottom(bottom, backup = True)
161 patch.set_top(top, backup = True)
162
163 # the actual merging (either from a branch or an external file)
164 merge_patch(patch, p)
165
166 if git.local_changes(verbose = False):
167 # index (cache) already updated by the git merge. The
168 # backup information was already reset above
169 crt_series.refresh_patch(cache_update = False, backup = False,
170 log = 'sync')
27ac2b7e 171 out.done('updated')
06848fab 172 else:
27ac2b7e 173 out.done()