1 """Function/variables common to all the commands
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from optparse
import OptionParser
, make_option
24 from stgit
.utils
import *
25 from stgit
import stack
, git
, basedir
26 from stgit
.config
import config
, file_extensions
31 # Command exception class
32 class CmdException(Exception):
43 rev_list
= rev
.split('/')
44 if len(rev_list
) == 2:
45 patch_id
= rev_list
[1]
48 elif len(rev_list
) == 1:
53 patch_branch
= rev_list
[0].split('@')
54 if len(patch_branch
) == 1:
56 elif len(patch_branch
) == 2:
57 series
= stack
.Series(patch_branch
[1])
59 raise CmdException
, 'Unknown id: %s' % rev
61 patch_name
= patch_branch
[0]
63 patch_name
= series
.get_current()
65 raise CmdException
, 'No patches applied'
68 if patch_name
in series
.get_applied() \
69 or patch_name
in series
.get_unapplied():
71 return series
.get_patch(patch_name
).get_top()
72 elif patch_id
== 'bottom':
73 return series
.get_patch(patch_name
).get_bottom()
74 # Note we can return None here.
75 elif patch_id
== 'top.old':
76 return series
.get_patch(patch_name
).get_old_top()
77 elif patch_id
== 'bottom.old':
78 return series
.get_patch(patch_name
).get_old_bottom()
81 if patch_name
== 'base' and len(rev_list
) == 1:
82 return read_string(series
.get_base_file())
84 # anything else failed
85 return git
.rev_parse(rev
+ '^{commit}')
87 def check_local_changes():
88 if git
.local_changes():
90 'local changes in the tree. Use "refresh" to commit them'
92 def check_head_top_equal():
93 if not crt_series
.head_top_equal():
95 'HEAD and top are not the same. You probably committed\n' \
96 ' changes to the tree outside of StGIT. If you know what you\n' \
97 ' are doing, use the "refresh -f" command'
99 def check_conflicts():
100 if os
.path
.exists(os
.path
.join(basedir
.get(), 'conflicts')):
101 raise CmdException
, 'Unsolved conflicts. Please resolve them first'
103 def print_crt_patch(branch
= None):
105 patch
= crt_series
.get_current()
107 patch
= stack
.Series(branch
).get_current()
110 print 'Now at patch "%s"' % patch
112 print 'No patches applied'
114 def resolved(filename
, reset
= None):
116 reset_file
= filename
+ file_extensions()[reset
]
117 if os
.path
.isfile(reset_file
):
118 if os
.path
.isfile(filename
):
120 os
.rename(reset_file
, filename
)
122 git
.update_cache([filename
], force
= True)
124 for ext
in file_extensions().values():
126 if os
.path
.isfile(fn
):
129 def resolved_all(reset
= None):
130 conflicts
= git
.get_conflicts()
132 for filename
in conflicts
:
133 resolved(filename
, reset
)
134 os
.remove(os
.path
.join(basedir
.get(), 'conflicts'))
136 def push_patches(patches
, check_merged
= False):
137 """Push multiple patches onto the stack. This function is shared
138 between the push and pull commands
140 forwarded
= crt_series
.forward_patches(patches
)
142 print 'Fast-forwarded patches "%s" - "%s"' %
(patches
[0],
143 patches
[forwarded
- 1])
145 print 'Fast-forwarded patch "%s"' % patches
[0]
147 names
= patches
[forwarded
:]
149 # check for patches merged upstream
151 print 'Checking for patches merged upstream...',
154 merged
= crt_series
.merged_patches(names
)
156 print 'done (%d found)' %
len(merged
)
161 print 'Pushing patch "%s"...' % p
,
165 crt_series
.push_patch(p
, empty
= True)
166 print 'done (merged upstream)'
168 modified
= crt_series
.push_patch(p
)
170 if crt_series
.empty_patch(p
):
171 print 'done (empty patch)'
173 print 'done (modified)'
177 def name_email(address
):
178 """Return a tuple consisting of the name and email parsed from a
179 standard 'name <email>' or 'email (name)' string
181 address
= re
.sub('[\\\\"]', '\\\\\g<0>', address
)
182 str_list
= re
.findall('^(.*)\s*<(.*)>\s*$', address
)
184 str_list
= re
.findall('^(.*)\s*\((.*)\)\s*$', address
)
186 raise CmdException
, 'Incorrect "name <email>"/"email (name)" string: %s' % address
187 return ( str_list
[0][1], str_list
[0][0] )
191 def name_email_date(address
):
192 """Return a tuple consisting of the name, email and date parsed
193 from a 'name <email> date' string
195 address
= re
.sub('[\\\\"]', '\\\\\g<0>', address
)
196 str_list
= re
.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address
)
198 raise CmdException
, 'Incorrect "name <email> date" string: %s' % address