Commit | Line | Data |
---|---|---|
fcee87cf CM |
1 | """Function/variables commmon to all the commands |
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 * | |
25 | from stgit import stack, git | |
26 | ||
27 | ||
28 | # Command exception class | |
29 | class CmdException(Exception): | |
30 | pass | |
31 | ||
32 | ||
fcee87cf | 33 | # Utility functions |
a2dcde71 | 34 | def git_id(string): |
fcee87cf CM |
35 | """Return the GIT id |
36 | """ | |
37 | if not string: | |
38 | return None | |
39 | ||
40 | string_list = string.split('/') | |
a2dcde71 CM |
41 | if len(string_list) == 2: |
42 | patch_id = string_list[1] | |
43 | if not patch_id: | |
44 | patch_id = 'top' | |
45 | elif len(string_list) == 1: | |
46 | patch_id = 'top' | |
47 | else: | |
48 | patch_id = None | |
fcee87cf | 49 | |
a2dcde71 CM |
50 | patch_branch = string_list[0].split('@') |
51 | if len(patch_branch) == 1: | |
52 | series = crt_series | |
53 | elif len(patch_branch) == 2: | |
54 | series = stack.Series(patch_branch[1]) | |
55 | else: | |
56 | raise CmdException, 'Unknown id: %s' % string | |
fcee87cf | 57 | |
a2dcde71 CM |
58 | patch_name = patch_branch[0] |
59 | if not patch_name: | |
60 | patch_name = series.get_current() | |
fcee87cf CM |
61 | if not patch_name: |
62 | raise CmdException, 'No patches applied' | |
fcee87cf | 63 | |
a2dcde71 CM |
64 | # patch |
65 | if patch_name in series.get_applied() \ | |
66 | or patch_name in series.get_unapplied(): | |
67 | if patch_id == 'top': | |
68 | return series.get_patch(patch_name).get_top() | |
69 | elif patch_id == 'bottom': | |
70 | return series.get_patch(patch_name).get_bottom() | |
71 | ||
72 | # base | |
73 | if patch_name == 'base' and len(string_list) == 1: | |
74 | return read_string(series.get_base_file()) | |
fcee87cf | 75 | |
a2dcde71 CM |
76 | # anything else failed |
77 | return git.rev_parse(string) | |
fcee87cf CM |
78 | |
79 | def check_local_changes(): | |
80 | if git.local_changes(): | |
81 | raise CmdException, \ | |
82 | 'local changes in the tree. Use "refresh" to commit them' | |
83 | ||
84 | def check_head_top_equal(): | |
85 | if not crt_series.head_top_equal(): | |
86 | raise CmdException, \ | |
87 | 'HEAD and top are not the same. You probably committed\n' \ | |
88 | ' changes to the tree ouside of StGIT. If you know what you\n' \ | |
89 | ' are doing, use the "refresh -f" command' | |
90 | ||
91 | def check_conflicts(): | |
92 | if os.path.exists(os.path.join(git.base_dir, 'conflicts')): | |
93 | raise CmdException, 'Unsolved conflicts. Please resolve them first' | |
94 | ||
94b7894d CL |
95 | def print_crt_patch(branch = None): |
96 | if not branch: | |
97 | patch = crt_series.get_current() | |
98 | else: | |
99 | patch = stack.Series(branch).get_current() | |
100 | ||
fcee87cf CM |
101 | if patch: |
102 | print 'Now at patch "%s"' % patch | |
103 | else: | |
104 | print 'No patches applied' | |
105 | ||
7c47d096 CM |
106 | def resolved(filename, reset = None): |
107 | if reset: | |
108 | reset_file = filename + '.' + reset | |
109 | if os.path.isfile(reset_file): | |
110 | if os.path.isfile(filename): | |
111 | os.remove(filename) | |
112 | os.rename(reset_file, filename) | |
113 | ||
402ad990 | 114 | git.update_cache([filename], force = True) |
7c47d096 | 115 | |
fcee87cf CM |
116 | for ext in ['.local', '.older', '.remote']: |
117 | fn = filename + ext | |
118 | if os.path.isfile(fn): | |
119 | os.remove(fn) | |
120 | ||
7c47d096 | 121 | def resolved_all(reset = None): |
fcee87cf CM |
122 | conflicts = git.get_conflicts() |
123 | if conflicts: | |
124 | for filename in conflicts: | |
7c47d096 | 125 | resolved(filename, reset) |
fcee87cf | 126 | os.remove(os.path.join(git.base_dir, 'conflicts')) |
19cd0a8f CM |
127 | |
128 | def name_email(string): | |
129 | """Return a tuple consisting of the name and email parsed from a | |
130 | standard 'name <email>' string | |
131 | """ | |
9cd0fc96 CM |
132 | string = re.sub('([^\w\s<>@.])', '\\\\\\1', string) |
133 | str_list = re.findall('^(.*)\s*<(.*)>\s*$', string) | |
37a4d1bf | 134 | if not str_list: |
19cd0a8f CM |
135 | raise CmdException, 'Incorrect "name <email>" string: %s' % string |
136 | ||
37a4d1bf CM |
137 | return str_list[0] |
138 | ||
139 | def name_email_date(string): | |
140 | """Return a tuple consisting of the name, email and date parsed | |
141 | from a 'name <email> date' string | |
142 | """ | |
9cd0fc96 CM |
143 | string = re.sub('([^\w\s<>@.])', '\\\\\\1', string) |
144 | str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', string) | |
37a4d1bf CM |
145 | if not str_list: |
146 | raise CmdException, 'Incorrect "name <email> date" string: %s' % string | |
147 | ||
148 | return str_list[0] |