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
30 # Command exception class
31 class CmdException(Exception):
42 rev_list
= rev
.split('/')
43 if len(rev_list
) == 2:
44 patch_id
= rev_list
[1]
47 elif len(rev_list
) == 1:
52 patch_branch
= rev_list
[0].split('@')
53 if len(patch_branch
) == 1:
55 elif len(patch_branch
) == 2:
56 series
= stack
.Series(patch_branch
[1])
58 raise CmdException
, 'Unknown id: %s' % rev
60 patch_name
= patch_branch
[0]
62 patch_name
= series
.get_current()
64 raise CmdException
, 'No patches applied'
67 if patch_name
in series
.get_applied() \
68 or patch_name
in series
.get_unapplied():
70 return series
.get_patch(patch_name
).get_top()
71 elif patch_id
== 'bottom':
72 return series
.get_patch(patch_name
).get_bottom()
73 # Note we can return None here.
74 elif patch_id
== 'top.old':
75 return series
.get_patch(patch_name
).get_old_top()
76 elif patch_id
== 'bottom.old':
77 return series
.get_patch(patch_name
).get_old_bottom()
80 if patch_name
== 'base' and len(rev_list
) == 1:
81 return read_string(series
.get_base_file())
83 # anything else failed
84 return git
.rev_parse(rev
)
86 def check_local_changes():
87 if git
.local_changes():
89 'local changes in the tree. Use "refresh" to commit them'
91 def check_head_top_equal():
92 if not crt_series
.head_top_equal():
94 'HEAD and top are not the same. You probably committed\n' \
95 ' changes to the tree ouside of StGIT. If you know what you\n' \
96 ' are doing, use the "refresh -f" command'
98 def check_conflicts():
99 if os
.path
.exists(os
.path
.join(git
.base_dir
, 'conflicts')):
100 raise CmdException
, 'Unsolved conflicts. Please resolve them first'
102 def print_crt_patch(branch
= None):
104 patch
= crt_series
.get_current()
106 patch
= stack
.Series(branch
).get_current()
109 print 'Now at patch "%s"' % patch
111 print 'No patches applied'
113 def resolved(filename
, reset
= None):
115 reset_file
= filename
+ '.' + reset
116 if os
.path
.isfile(reset_file
):
117 if os
.path
.isfile(filename
):
119 os
.rename(reset_file
, filename
)
121 git
.update_cache([filename
], force
= True)
123 for ext
in ['.local', '.older', '.remote']:
125 if os
.path
.isfile(fn
):
128 def resolved_all(reset
= None):
129 conflicts
= git
.get_conflicts()
131 for filename
in conflicts
:
132 resolved(filename
, reset
)
133 os
.remove(os
.path
.join(git
.base_dir
, 'conflicts'))
135 def name_email(address
):
136 """Return a tuple consisting of the name and email parsed from a
137 standard 'name <email>' string
139 address
= re
.sub('([^\w\s<>@.])', '\\\\\\1', address
)
140 str_list
= re
.findall('^(.*)\s*<(.*)>\s*$', address
)
142 raise CmdException
, 'Incorrect "name <email>" string: %s' % address
146 def name_email_date(address
):
147 """Return a tuple consisting of the name, email and date parsed
148 from a 'name <email> date' string
150 address
= re
.sub('([^\w\s<>@.])', '\\\\\\1', address
)
151 str_list
= re
.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address
)
153 raise CmdException
, 'Incorrect "name <email> date" string: %s' % address