Commit | Line | Data |
---|---|---|
388f63b6 | 1 | """Function/variables common to all the commands |
fcee87cf CM |
2 | """ |
3 | ||
4 | __copyright__ = """ | |
5 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> | |
6 | ||
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. | |
10 | ||
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. | |
15 | ||
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 | |
19 | """ | |
20 | ||
19cd0a8f | 21 | import sys, os, re |
fcee87cf CM |
22 | from optparse import OptionParser, make_option |
23 | ||
24 | from stgit.utils import * | |
170f576b | 25 | from stgit import stack, git, basedir |
fcee87cf | 26 | |
e7dc3866 CL |
27 | crt_series = None |
28 | ||
fcee87cf CM |
29 | |
30 | # Command exception class | |
31 | class CmdException(Exception): | |
32 | pass | |
33 | ||
34 | ||
fcee87cf | 35 | # Utility functions |
7cc615f3 | 36 | def git_id(rev): |
fcee87cf CM |
37 | """Return the GIT id |
38 | """ | |
7cc615f3 | 39 | if not rev: |
fcee87cf CM |
40 | return None |
41 | ||
7cc615f3 CL |
42 | rev_list = rev.split('/') |
43 | if len(rev_list) == 2: | |
44 | patch_id = rev_list[1] | |
a2dcde71 CM |
45 | if not patch_id: |
46 | patch_id = 'top' | |
7cc615f3 | 47 | elif len(rev_list) == 1: |
a2dcde71 CM |
48 | patch_id = 'top' |
49 | else: | |
50 | patch_id = None | |
fcee87cf | 51 | |
7cc615f3 | 52 | patch_branch = rev_list[0].split('@') |
a2dcde71 CM |
53 | if len(patch_branch) == 1: |
54 | series = crt_series | |
55 | elif len(patch_branch) == 2: | |
56 | series = stack.Series(patch_branch[1]) | |
57 | else: | |
7cc615f3 | 58 | raise CmdException, 'Unknown id: %s' % rev |
fcee87cf | 59 | |
a2dcde71 CM |
60 | patch_name = patch_branch[0] |
61 | if not patch_name: | |
62 | patch_name = series.get_current() | |
fcee87cf CM |
63 | if not patch_name: |
64 | raise CmdException, 'No patches applied' | |
fcee87cf | 65 | |
a2dcde71 CM |
66 | # patch |
67 | if patch_name in series.get_applied() \ | |
68 | or patch_name in series.get_unapplied(): | |
69 | if patch_id == 'top': | |
70 | return series.get_patch(patch_name).get_top() | |
71 | elif patch_id == 'bottom': | |
72 | return series.get_patch(patch_name).get_bottom() | |
54b09584 PBG |
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() | |
a2dcde71 CM |
78 | |
79 | # base | |
7cc615f3 | 80 | if patch_name == 'base' and len(rev_list) == 1: |
a2dcde71 | 81 | return read_string(series.get_base_file()) |
fcee87cf | 82 | |
a2dcde71 | 83 | # anything else failed |
2fef9462 | 84 | return git.rev_parse(rev + '^{commit}') |
fcee87cf CM |
85 | |
86 | def check_local_changes(): | |
87 | if git.local_changes(): | |
88 | raise CmdException, \ | |
89 | 'local changes in the tree. Use "refresh" to commit them' | |
90 | ||
91 | def check_head_top_equal(): | |
92 | if not crt_series.head_top_equal(): | |
93 | raise CmdException, \ | |
94 | 'HEAD and top are not the same. You probably committed\n' \ | |
388f63b6 | 95 | ' changes to the tree outside of StGIT. If you know what you\n' \ |
fcee87cf CM |
96 | ' are doing, use the "refresh -f" command' |
97 | ||
98 | def check_conflicts(): | |
170f576b | 99 | if os.path.exists(os.path.join(basedir.get(), 'conflicts')): |
fcee87cf CM |
100 | raise CmdException, 'Unsolved conflicts. Please resolve them first' |
101 | ||
94b7894d CL |
102 | def print_crt_patch(branch = None): |
103 | if not branch: | |
104 | patch = crt_series.get_current() | |
105 | else: | |
106 | patch = stack.Series(branch).get_current() | |
107 | ||
fcee87cf CM |
108 | if patch: |
109 | print 'Now at patch "%s"' % patch | |
110 | else: | |
111 | print 'No patches applied' | |
112 | ||
7c47d096 CM |
113 | def resolved(filename, reset = None): |
114 | if reset: | |
115 | reset_file = filename + '.' + reset | |
116 | if os.path.isfile(reset_file): | |
117 | if os.path.isfile(filename): | |
118 | os.remove(filename) | |
119 | os.rename(reset_file, filename) | |
120 | ||
402ad990 | 121 | git.update_cache([filename], force = True) |
7c47d096 | 122 | |
fcee87cf CM |
123 | for ext in ['.local', '.older', '.remote']: |
124 | fn = filename + ext | |
125 | if os.path.isfile(fn): | |
126 | os.remove(fn) | |
127 | ||
7c47d096 | 128 | def resolved_all(reset = None): |
fcee87cf CM |
129 | conflicts = git.get_conflicts() |
130 | if conflicts: | |
131 | for filename in conflicts: | |
7c47d096 | 132 | resolved(filename, reset) |
170f576b | 133 | os.remove(os.path.join(basedir.get(), 'conflicts')) |
19cd0a8f | 134 | |
1777d8cd CM |
135 | def push_patches(patches, check_merged = False): |
136 | """Push multiple patches onto the stack. This function is shared | |
137 | between the push and pull commands | |
138 | """ | |
139 | forwarded = crt_series.forward_patches(patches) | |
140 | if forwarded > 1: | |
141 | print 'Fast-forwarded patches "%s" - "%s"' % (patches[0], | |
142 | patches[forwarded - 1]) | |
143 | elif forwarded == 1: | |
144 | print 'Fast-forwarded patch "%s"' % patches[0] | |
145 | ||
146 | names = patches[forwarded:] | |
147 | ||
148 | # check for patches merged upstream | |
149 | if check_merged: | |
150 | print 'Checking for patches merged upstream...', | |
151 | sys.stdout.flush() | |
152 | ||
153 | merged = crt_series.merged_patches(names) | |
154 | ||
155 | print 'done (%d found)' % len(merged) | |
156 | else: | |
157 | merged = [] | |
158 | ||
159 | for p in names: | |
160 | print 'Pushing patch "%s"...' % p, | |
161 | sys.stdout.flush() | |
162 | ||
163 | if p in merged: | |
164 | crt_series.push_patch(p, empty = True) | |
165 | print 'done (merged upstream)' | |
166 | else: | |
167 | modified = crt_series.push_patch(p) | |
168 | ||
169 | if crt_series.empty_patch(p): | |
170 | print 'done (empty patch)' | |
171 | elif modified: | |
172 | print 'done (modified)' | |
173 | else: | |
174 | print 'done' | |
175 | ||
7cc615f3 | 176 | def name_email(address): |
19cd0a8f | 177 | """Return a tuple consisting of the name and email parsed from a |
df992d5a | 178 | standard 'name <email>' or 'email (name)' string |
19cd0a8f | 179 | """ |
e2d769b2 | 180 | address = re.sub('[\\\\"]', '\\\\\g<0>', address) |
7cc615f3 | 181 | str_list = re.findall('^(.*)\s*<(.*)>\s*$', address) |
37a4d1bf | 182 | if not str_list: |
df992d5a SV |
183 | str_list = re.findall('^(.*)\s*\((.*)\)\s*$', address) |
184 | if not str_list: | |
185 | raise CmdException, 'Incorrect "name <email>"/"email (name)" string: %s' % address | |
186 | return ( str_list[0][1], str_list[0][0] ) | |
19cd0a8f | 187 | |
37a4d1bf CM |
188 | return str_list[0] |
189 | ||
7cc615f3 | 190 | def name_email_date(address): |
37a4d1bf CM |
191 | """Return a tuple consisting of the name, email and date parsed |
192 | from a 'name <email> date' string | |
193 | """ | |
e2d769b2 | 194 | address = re.sub('[\\\\"]', '\\\\\g<0>', address) |
7cc615f3 | 195 | str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address) |
37a4d1bf | 196 | if not str_list: |
7cc615f3 | 197 | raise CmdException, 'Incorrect "name <email> date" string: %s' % address |
37a4d1bf CM |
198 | |
199 | return str_list[0] |