b312157d18a240cdef9f3d9207ec7aeb29fedf92
[stgit] / stgit / commands / publish.py
1 __copyright__ = """
2 Copyright (C) 2009, 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 from stgit import argparse
19 from stgit.argparse import opt
20 from stgit.commands import common
21 from stgit.config import config
22 from stgit.lib import git, stack, transaction
23 from stgit.out import out
24 from stgit import utils
25
26 help = 'Push the stack changes to a merge-friendly branch'
27 kind = 'stack'
28 usage = ['[options] [--] [branch]']
29 description = """
30 This command commits a set of changes on a separate (called public) branch
31 based on the modifications of the given or current stack. The history of the
32 public branch is not re-written, making it merge-friendly and feasible for
33 publishing. The heads of the stack and public branch may be different but the
34 corresponding tree objects are always the same.
35
36 If the trees of the stack and public branch are different (otherwise the
37 command has no effect), StGit first checks for a rebase of the stack since the
38 last publishing. If a rebase is detected, StGit creates a commit on the public
39 branch corresponding to a merge between the new stack base and the latest
40 public head.
41
42 If no rebasing was detected, StGit checks for new patches that may have been
43 created on top of the stack since the last publishing. If new patches are
44 found and are not empty, they are checked into the public branch keeping the
45 same commit information (e.g. log message, author, committer, date).
46
47 If the above tests fail (e.g. patches modified or removed), StGit creates a
48 new commit on the public branch having the same tree as the stack but the
49 public head as its parent. The editor will be invoked if no "--message" option
50 is given.
51
52 It is recommended that stack modifications falling in different categories as
53 described above are separated by a publish command in order to keep the public
54 branch history cleaner (otherwise StGit would generate a big commit including
55 several stack modifications).
56
57 The '--unpublished' option can be used to check if there are applied patches
58 that have not been published to the public branch. This is done by trying to
59 revert the patches in the public tree (similar to the 'push --merged'
60 detection).
61
62 The public branch name can be set via the branch.<branch>.public configuration
63 variable (defaulting to "<branch>.public").
64 """
65
66 args = [argparse.all_branches]
67 options = [
68 opt('-b', '--branch', args = [argparse.stg_branches],
69 short = 'Use BRANCH instead of the default branch'),
70 opt('-u', '--unpublished', action = 'store_true',
71 short = 'Show applied patches that have not been published')
72 ] + (argparse.author_options()
73 + argparse.message_options(save_template = False)
74 + argparse.sign_options())
75
76 directory = common.DirectoryHasRepositoryLib()
77
78 def __create_commit(repository, tree, parents, options, message = ''):
79 """Return a new Commit object."""
80 cd = git.CommitData(
81 tree = tree, parents = parents, message = message,
82 author = git.Person.author(), committer = git.Person.committer())
83 cd = common.update_commit_data(cd, options)
84
85 return repository.commit(cd)
86
87 def __get_published(stack, tree):
88 """Check the patches that were already published."""
89 trans = transaction.StackTransaction(stack, 'publish')
90 published = trans.check_merged(trans.applied, tree = tree, quiet = True)
91 trans.abort()
92
93 return published
94
95 def func(parser, options, args):
96 """Publish the stack changes."""
97 repository = directory.repository
98 stack = repository.get_stack(options.branch)
99
100 if not args:
101 public_ref = common.get_public_ref(stack.name)
102 elif len(args) == 1:
103 public_ref = args[0]
104 else:
105 parser.error('incorrect number of arguments')
106
107 if not public_ref.startswith('refs/heads/'):
108 public_ref = 'refs/heads/' + public_ref
109
110 # just clone the stack if the public ref does not exist
111 if not repository.refs.exists(public_ref):
112 if options.unpublished:
113 raise common.CmdException('"%s" does not exist' % public_ref)
114 repository.refs.set(public_ref, stack.head, 'publish')
115 out.info('Created "%s"' % public_ref)
116 return
117
118 public_head = repository.refs.get(public_ref)
119 public_tree = public_head.data.tree
120
121 # check for same tree (already up to date)
122 if public_tree.sha1 == stack.head.data.tree.sha1:
123 out.info('"%s" already up to date' % public_ref)
124 return
125
126 # check for unpublished patches
127 if options.unpublished:
128 published = set(__get_published(stack, public_tree))
129 for p in stack.patchorder.applied:
130 if p not in published:
131 print p
132 return
133
134 # check for rebased stack. In this case we emulate a merge with the stack
135 # base by setting two parents.
136 merge_bases = repository.get_merge_bases(public_head, stack.base)
137 if public_head in merge_bases:
138 # fast-forward the public ref
139 repository.refs.set(public_ref, stack.head, 'publish')
140 out.info('Fast-forwarded "%s"' % public_ref)
141 return
142 if not stack.base in merge_bases:
143 message = 'Merge %s into %s' % (repository.describe(stack.base).strip(),
144 utils.strip_prefix('refs/heads/',
145 public_ref))
146 public_head = __create_commit(repository, stack.head.data.tree,
147 [public_head, stack.base], options,
148 message)
149 repository.refs.set(public_ref, public_head, 'publish')
150 out.info('Merged the stack base into "%s"' % public_ref)
151 return
152
153 # check for new patches from the last publishing. This is done by checking
154 # whether the public tree is the same as the bottom of the checked patch.
155 # If older patches were modified, new patches cannot be detected. The new
156 # patches and their metadata are pushed directly to the published head.
157 for p in stack.patchorder.applied:
158 pc = stack.patches.get(p).commit
159 if public_tree.sha1 == pc.data.parent.data.tree.sha1:
160 if pc.data.is_nochange():
161 out.info('Ignored new empty patch "%s"' % p)
162 continue
163 cd = pc.data.set_parent(public_head)
164 public_head = repository.commit(cd)
165 public_tree = public_head.data.tree
166 out.info('Published new patch "%s"' % p)
167
168 # create a new commit (only happens if no new patches are detected)
169 if public_tree.sha1 != stack.head.data.tree.sha1:
170 public_head = __create_commit(repository, stack.head.data.tree,
171 [public_head], options)
172
173 # update the public head
174 repository.refs.set(public_ref, public_head, 'publish')
175 out.info('Updated "%s"' % public_ref)