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