Use get/set_name for a stack's name.
[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 *
24from stgit import stack, git
25
26
27help = 'synchronise patches with a branch or a series'
28usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
29
30For each of the specified patches perform a three-way merge with the
31same patch in the specified branch or series. The command can be used
32for keeping patches on several branches in sync. Note that the
33operation may fail for some patches because of conflicts. The patches
34in the series must apply cleanly.
35
36The sync operation can be reverted for individual patches with --undo."""
37
38options = [make_option('-a', '--all',
39 help = 'synchronise all the patches',
40 action = 'store_true'),
41 make_option('-b', '--branch',
42 help = 'syncronise patches with BRANCH'),
43 make_option('-s', '--series',
44 help = 'syncronise patches with SERIES'),
45 make_option('--undo',
46 help = 'undo the synchronisation of the current patch',
47 action = 'store_true')]
48
49def __check_all():
50 check_local_changes()
51 check_conflicts()
52 check_head_top_equal()
53
54def __branch_merge_patch(remote_series, pname):
55 """Merge a patch from a remote branch into the current tree.
56 """
57 patch = remote_series.get_patch(pname)
58 git.merge(patch.get_bottom(), git.get_head(), patch.get_top())
59
60def __series_merge_patch(base, patchdir, pname):
61 """Merge a patch file with the given StGIT patch.
62 """
63 patchfile = os.path.join(patchdir, pname)
64 git.apply_patch(filename = patchfile, base = base)
65
66def func(parser, options, args):
67 """Synchronise a range of patches
68 """
69 global crt_series
70
71 if options.undo:
72 if options.branch or options.series:
73 raise CmdException, \
74 '--undo cannot be specified with --branch or --series'
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
83 if options.branch:
84 # the main function already made crt_series to be the remote
85 # branch
86 remote_series = crt_series
87 stgit.commands.common.crt_series = crt_series = stack.Series()
d37ff079 88 if options.branch == crt_series.get_name():
06848fab
CM
89 raise CmdException, 'Cannot synchronise with the current branch'
90 remote_patches = remote_series.get_applied()
91
92 # the merge function merge_patch(patch, pname)
93 merge_patch = lambda patch, pname: \
94 __branch_merge_patch(remote_series, pname)
95 elif options.series:
96 patchdir = os.path.dirname(options.series)
97
98 remote_patches = []
99 f = file(options.series)
100 for line in f:
101 p = re.sub('#.*$', '', line).strip()
102 if not p:
103 continue
104 remote_patches.append(p)
105 f.close()
106
107 # the merge function merge_patch(patch, pname)
108 merge_patch = lambda patch, pname: \
109 __series_merge_patch(patch.get_bottom(), patchdir, pname)
110 else:
111 raise CmdException, 'No remote branch or series specified'
112
113 applied = crt_series.get_applied()
114
115 if options.all:
116 patches = applied
117 elif len(args) != 0:
118 patches = parse_patches(args, applied, ordered = True)
00d468f5
CM
119 elif applied:
120 patches = [crt_series.get_current()]
06848fab 121 else:
00d468f5 122 parser.error('no patches applied')
06848fab
CM
123
124 if not patches:
125 raise CmdException, 'No patches to synchronise'
126
127 __check_all()
128
129 # only keep the patches to be synchronised
130 sync_patches = [p for p in patches if p in remote_patches]
131 if not sync_patches:
132 raise CmdException, 'No common patches to be synchronised'
133
134 # pop to the one before the first patch to be synchronised
135 popped = applied[applied.index(sync_patches[0]) + 1:]
136 if popped:
137 pop_patches(popped[::-1])
138
139 for p in sync_patches:
140 if p in popped:
141 # push to this patch
142 idx = popped.index(p) + 1
143 push_patches(popped[:idx])
144 del popped[:idx]
145
146 # the actual sync
27ac2b7e 147 out.start('Synchronising "%s"' % p)
06848fab
CM
148
149 patch = crt_series.get_patch(p)
150 bottom = patch.get_bottom()
151 top = patch.get_top()
152
153 # reset the patch backup information. That's needed in case we
154 # undo the sync but there were no changes made
155 patch.set_bottom(bottom, backup = True)
156 patch.set_top(top, backup = True)
157
158 # the actual merging (either from a branch or an external file)
159 merge_patch(patch, p)
160
161 if git.local_changes(verbose = False):
162 # index (cache) already updated by the git merge. The
163 # backup information was already reset above
164 crt_series.refresh_patch(cache_update = False, backup = False,
165 log = 'sync')
27ac2b7e 166 out.done('updated')
06848fab 167 else:
27ac2b7e 168 out.done()
06848fab
CM
169
170 # push the remaining patches
171 if popped:
172 push_patches(popped)