chiark / gitweb /
ef944390cf4aa452fe948bbe557e2e0673867f81
[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 usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
28 description = """
29 For each of the specified patches perform a three-way merge with the
30 same patch in the specified branch or series. The command can be used
31 for keeping patches on several branches in sync. Note that the
32 operation may fail for some patches because of conflicts. The patches
33 in the series must apply cleanly.
34
35 The sync operation can be reverted for individual patches with --undo."""
36
37 options = [
38     opt('-a', '--all', action = 'store_true',
39         short = 'Synchronise all the applied patches'),
40     opt('-B', '--ref-branch',
41         short = 'Syncronise patches with BRANCH'),
42     opt('-s', '--series',
43         short = 'Syncronise patches with SERIES'),
44     opt('--undo', action = 'store_true',
45         short = 'Undo the synchronisation of the current patch')]
46
47 directory = DirectoryGotoToplevel()
48
49 def __check_all():
50     check_local_changes()
51     check_conflicts()
52     check_head_top_equal(crt_series)
53
54 def __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_recursive(patch.get_bottom(), git.get_head(), patch.get_top())
59
60 def __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
66 def func(parser, options, args):
67     """Synchronise a range of patches
68     """
69     if options.undo:
70         if options.ref_branch or options.series:
71             raise CmdException, \
72                   '--undo cannot be specified with --ref-branch or --series'
73         __check_all()
74
75         out.start('Undoing the sync of "%s"' % crt_series.get_current())
76         crt_series.undo_refresh()
77         git.reset()
78         out.done()
79         return
80
81     if options.ref_branch:
82         remote_series = stack.Series(options.ref_branch)
83         if options.ref_branch == crt_series.get_name():
84             raise CmdException, 'Cannot synchronise with the current branch'
85         remote_patches = remote_series.get_applied()
86
87         # the merge function merge_patch(patch, pname)
88         merge_patch = lambda patch, pname: \
89                       __branch_merge_patch(remote_series, pname)
90     elif options.series:
91         patchdir = os.path.dirname(options.series)
92
93         remote_patches = []
94         f = file(options.series)
95         for line in f:
96             p = re.sub('#.*$', '', line).strip()
97             if not p:
98                 continue
99             remote_patches.append(p)
100         f.close()
101
102         # the merge function merge_patch(patch, pname)
103         merge_patch = lambda patch, pname: \
104                       __series_merge_patch(patch.get_bottom(), patchdir, pname)
105     else:
106         raise CmdException, 'No remote branch or series specified'
107
108     applied = crt_series.get_applied()
109     unapplied = crt_series.get_unapplied()
110     
111     if options.all:
112         patches = applied
113     elif len(args) != 0:
114         patches = parse_patches(args, applied + unapplied, len(applied),
115                                 ordered = True)
116     elif applied:
117         patches = [crt_series.get_current()]
118     else:
119         parser.error('no patches applied')
120
121     if not patches:
122         raise CmdException, 'No patches to synchronise'
123
124     __check_all()
125
126     # only keep the patches to be synchronised
127     sync_patches = [p for p in patches if p in remote_patches]
128     if not sync_patches:
129         raise CmdException, 'No common patches to be synchronised'
130
131     # pop to the one before the first patch to be synchronised
132     first_patch = sync_patches[0]
133     if first_patch in applied:
134         to_pop = applied[applied.index(first_patch) + 1:]
135         if to_pop:
136             pop_patches(crt_series, to_pop[::-1])
137         pushed = [first_patch]
138     else:
139         to_pop = []
140         pushed = []
141     popped = to_pop + [p for p in patches if p in unapplied]
142
143     for p in pushed + popped:
144         if p in popped:
145             # push this patch
146             push_patches(crt_series, [p])
147         if p not in sync_patches:
148             # nothing to synchronise
149             continue
150
151         # the actual sync
152         out.start('Synchronising "%s"' % p)
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_top(top, backup = True)
161
162         # the actual merging (either from a branch or an external file)
163         merge_patch(patch, p)
164
165         if git.local_changes(verbose = False):
166             # index (cache) already updated by the git merge. The
167             # backup information was already reset above
168             crt_series.refresh_patch(cache_update = False, backup = False,
169                                      log = 'sync')
170             out.done('updated')
171         else:
172             out.done()