chiark / gitweb /
26a49f03f9e058836a600b652721e2fb323f7881
[stgit] / stgit / commands / sync.py
1 __copyright__ = """
2 Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
3
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.
7
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.
12
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
16 """
17
18 import sys, os
19 import stgit.commands.common
20 from stgit.argparse import opt
21 from stgit.commands.common import *
22 from stgit.utils import *
23 from stgit.out import *
24 from stgit import stack, git
25
26 help = 'Synchronise patches with a branch or a series'
27 kind = 'patch'
28 usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
29 description = """
30 For each of the specified patches perform a three-way merge with the
31 same patch in the specified branch or series. The command can be used
32 for keeping patches on several branches in sync. Note that the
33 operation may fail for some patches because of conflicts. The patches
34 in the series must apply cleanly.
35
36 The sync operation can be reverted for individual patches with --undo."""
37
38 options = [
39     opt('-a', '--all', action = 'store_true',
40         short = 'Synchronise all the applied patches'),
41     opt('-B', '--ref-branch',
42         short = 'Syncronise patches with BRANCH'),
43     opt('-s', '--series',
44         short = 'Syncronise patches with SERIES'),
45     opt('--undo', action = 'store_true',
46         short = 'Undo the synchronisation of the current patch')]
47
48 directory = DirectoryGotoToplevel(log = True)
49
50 def __check_all():
51     check_local_changes()
52     check_conflicts()
53     check_head_top_equal(crt_series)
54
55 def __branch_merge_patch(remote_series, pname):
56     """Merge a patch from a remote branch into the current tree.
57     """
58     patch = remote_series.get_patch(pname)
59     git.merge_recursive(patch.get_bottom(), git.get_head(), patch.get_top())
60
61 def __series_merge_patch(base, patchdir, pname):
62     """Merge a patch file with the given StGIT patch.
63     """
64     patchfile = os.path.join(patchdir, pname)
65     git.apply_patch(filename = patchfile, base = base)
66
67 def func(parser, options, args):
68     """Synchronise a range of patches
69     """
70     if options.undo:
71         if options.ref_branch or options.series:
72             raise CmdException, \
73                   '--undo cannot be specified with --ref-branch or --series'
74         __check_all()
75
76         out.start('Undoing the sync of "%s"' % crt_series.get_current())
77         crt_series.undo_refresh()
78         git.reset()
79         out.done()
80         return
81
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()
87
88         # the merge function merge_patch(patch, pname)
89         merge_patch = lambda patch, pname: \
90                       __branch_merge_patch(remote_series, pname)
91     elif options.series:
92         patchdir = os.path.dirname(options.series)
93
94         remote_patches = []
95         f = file(options.series)
96         for line in f:
97             p = re.sub('#.*$', '', line).strip()
98             if not p:
99                 continue
100             remote_patches.append(p)
101         f.close()
102
103         # the merge function merge_patch(patch, pname)
104         merge_patch = lambda patch, pname: \
105                       __series_merge_patch(patch.get_bottom(), patchdir, pname)
106     else:
107         raise CmdException, 'No remote branch or series specified'
108
109     applied = crt_series.get_applied()
110     unapplied = crt_series.get_unapplied()
111     
112     if options.all:
113         patches = applied
114     elif len(args) != 0:
115         patches = parse_patches(args, applied + unapplied, len(applied),
116                                 ordered = True)
117     elif applied:
118         patches = [crt_series.get_current()]
119     else:
120         parser.error('no patches applied')
121
122     if not patches:
123         raise CmdException, 'No patches to synchronise'
124
125     __check_all()
126
127     # only keep the patches to be synchronised
128     sync_patches = [p for p in patches if p in remote_patches]
129     if not sync_patches:
130         raise CmdException, 'No common patches to be synchronised'
131
132     # pop to the one before the first patch to be synchronised
133     first_patch = sync_patches[0]
134     if first_patch in applied:
135         to_pop = applied[applied.index(first_patch) + 1:]
136         if to_pop:
137             pop_patches(crt_series, to_pop[::-1])
138         pushed = [first_patch]
139     else:
140         to_pop = []
141         pushed = []
142     popped = to_pop + [p for p in patches if p in unapplied]
143
144     for p in pushed + popped:
145         if p in popped:
146             # push this patch
147             push_patches(crt_series, [p])
148         if p not in sync_patches:
149             # nothing to synchronise
150             continue
151
152         # the actual sync
153         out.start('Synchronising "%s"' % p)
154
155         patch = crt_series.get_patch(p)
156         bottom = patch.get_bottom()
157         top = patch.get_top()
158
159         # reset the patch backup information. That's needed in case we
160         # undo the sync but there were no changes made
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')
171             out.done('updated')
172         else:
173             out.done()