Better diagnostic for wrong branch configuration.
[stgit] / stgit / commands / assimilate.py
CommitLineData
4d0ba818
KH
1# -*- coding: utf-8 -*-
2
3__copyright__ = """
4Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License version 2 as
8published by the Free Software Foundation.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18"""
19
20import sys, os
21from optparse import OptionParser, make_option
22
23from stgit.commands.common import *
24from stgit.utils import *
5e888f30 25from stgit.out import *
4d0ba818
KH
26from stgit import stack, git
27
28help = 'StGIT-ify any GIT commits made on top of your StGIT stack'
29usage = """%prog [options]
30
31If you have made GIT commits on top of your stack of StGIT patches,
32many StGIT commands will refuse to work. This command converts any
33such commits to StGIT patches, preserving their contents.
34
35Only GIT commits with exactly one parent can be assimilated; in other
36words, if you have committed a merge on top of your stack, this
37command cannot help you."""
38
39options = []
40
41def func(parser, options, args):
42 """Assimilate a number of patches.
43 """
44
45 def nothing_to_do():
27ac2b7e 46 out.info('No commits to assimilate')
4d0ba818
KH
47
48 top_patch = crt_series.get_current_patch()
49 if not top_patch:
50 return nothing_to_do()
51
52 victims = []
53 victim = git.get_commit(git.get_head())
54 while victim.get_id_hash() != top_patch.get_top():
55 victims.append(victim)
56 parents = victim.get_parents()
57 if not parents:
58 raise CmdException, 'Commit %s has no parents, aborting' % victim
59 elif len(parents) > 1:
60 raise CmdException, 'Commit %s is a merge, aborting' % victim
61 victim = git.get_commit(parents[0])
62
63 if not victims:
64 return nothing_to_do()
65
66 if crt_series.get_protected():
67 raise CmdException(
68 'This branch is protected. Modification is not permitted')
69
70 patch2name = {}
71 name2patch = {}
72
73 def name_taken(name):
74 return name in name2patch or crt_series.patch_exists(name)
75
76 for victim in victims:
b839b1cf 77 patchname = make_patch_name(victim.get_log(), name_taken)
4d0ba818
KH
78 patch2name[victim] = patchname
79 name2patch[patchname] = victim
80
81 victims.reverse()
82 for victim in victims:
27ac2b7e
KH
83 out.info('Creating patch "%s" from commit %s'
84 % (patch2name[victim], victim))
4d0ba818
KH
85 aname, amail, adate = name_email_date(victim.get_author())
86 cname, cmail, cdate = name_email_date(victim.get_committer())
87 crt_series.new_patch(
88 patch2name[victim],
0ec93bfd 89 can_edit = False, before_existing = False, commit = False,
4d0ba818
KH
90 top = victim.get_id_hash(), bottom = victim.get_parent(),
91 message = victim.get_log(),
92 author_name = aname, author_email = amail, author_date = adate,
93 committer_name = cname, committer_email = cmail)