2 Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from optparse import OptionParser, make_option
21 import stgit.commands.common
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit.out import *
25 from stgit import stack, git
28 help = 'synchronise patches with a branch or a series'
29 usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
31 For each of the specified patches perform a three-way merge with the
32 same patch in the specified branch or series. The command can be used
33 for keeping patches on several branches in sync. Note that the
34 operation may fail for some patches because of conflicts. The patches
35 in the series must apply cleanly.
37 The sync operation can be reverted for individual patches with --undo."""
39 options = [make_option('-a', '--all',
40 help = 'synchronise all the patches',
41 action = 'store_true'),
42 make_option('-B', '--ref-branch',
43 help = 'syncronise patches with BRANCH'),
44 make_option('-s', '--series',
45 help = 'syncronise patches with SERIES'),
47 help = 'undo the synchronisation of the current patch',
48 action = 'store_true')]
53 check_head_top_equal()
55 def __branch_merge_patch(remote_series, pname):
56 """Merge a patch from a remote branch into the current tree.
58 patch = remote_series.get_patch(pname)
59 git.merge(patch.get_bottom(), git.get_head(), patch.get_top())
61 def __series_merge_patch(base, patchdir, pname):
62 """Merge a patch file with the given StGIT patch.
64 patchfile = os.path.join(patchdir, pname)
65 git.apply_patch(filename = patchfile, base = base)
67 def func(parser, options, args):
68 """Synchronise a range of patches
71 if options.ref_branch or options.series:
73 '--undo cannot be specified with --ref-branch or --series'
76 out.start('Undoing the sync of "%s"' % crt_series.get_current())
77 crt_series.undo_refresh()
82 if options.ref_branch:
83 remote_series = stack.Series(options.ref_branch)
84 if options.ref_branch == crt_series.get_name():
85 raise CmdException, 'Cannot synchronise with the current branch'
86 remote_patches = remote_series.get_applied()
88 # the merge function merge_patch(patch, pname)
89 merge_patch = lambda patch, pname: \
90 __branch_merge_patch(remote_series, pname)
92 patchdir = os.path.dirname(options.series)
95 f = file(options.series)
97 p = re.sub('#.*$', '', line).strip()
100 remote_patches.append(p)
103 # the merge function merge_patch(patch, pname)
104 merge_patch = lambda patch, pname: \
105 __series_merge_patch(patch.get_bottom(), patchdir, pname)
107 raise CmdException, 'No remote branch or series specified'
109 applied = crt_series.get_applied()
114 patches = parse_patches(args, applied, ordered = True)
116 patches = [crt_series.get_current()]
118 parser.error('no patches applied')
121 raise CmdException, 'No patches to synchronise'
125 # only keep the patches to be synchronised
126 sync_patches = [p for p in patches if p in remote_patches]
128 raise CmdException, 'No common patches to be synchronised'
130 # pop to the one before the first patch to be synchronised
131 popped = applied[applied.index(sync_patches[0]) + 1:]
133 pop_patches(popped[::-1])
135 for p in sync_patches:
138 idx = popped.index(p) + 1
139 push_patches(popped[:idx])
143 out.start('Synchronising "%s"' % p)
145 patch = crt_series.get_patch(p)
146 bottom = patch.get_bottom()
147 top = patch.get_top()
149 # reset the patch backup information. That's needed in case we
150 # undo the sync but there were no changes made
151 patch.set_bottom(bottom, backup = True)
152 patch.set_top(top, backup = True)
154 # the actual merging (either from a branch or an external file)
155 merge_patch(patch, p)
157 if git.local_changes(verbose = False):
158 # index (cache) already updated by the git merge. The
159 # backup information was already reset above
160 crt_series.refresh_patch(cache_update = False, backup = False,
166 # push the remaining patches