1 """Function/variables commmon 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
28 # Command exception class
29 class CmdException(Exception):
40 string_list = string.split('/')
41 if len(string_list) == 2:
42 patch_id = string_list[1]
45 elif len(string_list) == 1:
50 patch_branch = string_list[0].split('@')
51 if len(patch_branch) == 1:
53 elif len(patch_branch) == 2:
54 series = stack.Series(patch_branch[1])
56 raise CmdException, 'Unknown id: %s' % string
58 patch_name = patch_branch[0]
60 patch_name = series.get_current()
62 raise CmdException, 'No patches applied'
65 if patch_name in series.get_applied() \
66 or patch_name in series.get_unapplied():
68 return series.get_patch(patch_name).get_top()
69 elif patch_id == 'bottom':
70 return series.get_patch(patch_name).get_bottom()
71 # Note we can return None here.
72 elif patch_id == 'top.old':
73 return series.get_patch(patch_name).get_old_top()
74 elif patch_id == 'bottom.old':
75 return series.get_patch(patch_name).get_old_bottom()
78 if patch_name == 'base' and len(string_list) == 1:
79 return read_string(series.get_base_file())
81 # anything else failed
82 return git.rev_parse(string)
84 def check_local_changes():
85 if git.local_changes():
87 'local changes in the tree. Use "refresh" to commit them'
89 def check_head_top_equal():
90 if not crt_series.head_top_equal():
92 'HEAD and top are not the same. You probably committed\n' \
93 ' changes to the tree ouside of StGIT. If you know what you\n' \
94 ' are doing, use the "refresh -f" command'
96 def check_conflicts():
97 if os.path.exists(os.path.join(git.base_dir, 'conflicts')):
98 raise CmdException, 'Unsolved conflicts. Please resolve them first'
100 def print_crt_patch(branch = None):
102 patch = crt_series.get_current()
104 patch = stack.Series(branch).get_current()
107 print 'Now at patch "%s"' % patch
109 print 'No patches applied'
111 def resolved(filename, reset = None):
113 reset_file = filename + '.' + reset
114 if os.path.isfile(reset_file):
115 if os.path.isfile(filename):
117 os.rename(reset_file, filename)
119 git.update_cache([filename], force = True)
121 for ext in ['.local', '.older', '.remote']:
123 if os.path.isfile(fn):
126 def resolved_all(reset = None):
127 conflicts = git.get_conflicts()
129 for filename in conflicts:
130 resolved(filename, reset)
131 os.remove(os.path.join(git.base_dir, 'conflicts'))
133 def name_email(string):
134 """Return a tuple consisting of the name and email parsed from a
135 standard 'name <email>' string
137 string = re.sub('([^\w\s<>@.])', '\\\\\\1', string)
138 str_list = re.findall('^(.*)\s*<(.*)>\s*$', string)
140 raise CmdException, 'Incorrect "name <email>" string: %s' % string
144 def name_email_date(string):
145 """Return a tuple consisting of the name, email and date parsed
146 from a 'name <email> date' string
148 string = re.sub('([^\w\s<>@.])', '\\\\\\1', string)
149 str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', string)
151 raise CmdException, 'Incorrect "name <email> date" string: %s' % string