18
18
from stgit .lib .transaction import StackTransaction , TransactionHalted
19
19
from stgit .out import out
20
20
from stgit .run import Run
21
- from stgit .utils import make_patch_name
22
21
23
22
__copyright__ = """
24
23
Copyright (C) 2005, Catalin Marinas <[email protected] >
149
148
directory = DirectoryHasRepository ()
150
149
151
150
152
- def __strip_patch_name (name ):
153
- stripped = re .sub ('^[0-9]+-(.*)$' , r'\g<1>' , name )
154
- stripped = re .sub (r'^(.*)\.(diff|patch)$' , r'\g<1>' , stripped )
155
- return stripped
156
-
157
-
158
- def __replace_slashes_with_dashes (name ):
159
- stripped = name .replace ('/' , '-' )
160
- return stripped
161
-
162
-
163
151
def __create_patch (
164
152
filename , message , author_name , author_email , author_date , diff , options
165
153
):
@@ -168,29 +156,31 @@ def __create_patch(
168
156
169
157
if options .name :
170
158
name = options .name
171
- if not stack .patches .is_name_valid (name ):
172
- raise CmdException ('Invalid patch name: %s' % name )
173
159
elif filename :
174
160
name = os .path .basename (filename )
175
161
else :
176
162
name = ''
177
- if options .stripname :
178
- name = __strip_patch_name (name )
179
163
180
- if not name :
181
- if options .ignore or options .replace :
164
+ if options .stripname :
165
+ # Removing leading numbers and trailing extension
166
+ name = re .sub (
167
+ r'''^
168
+ (?:[0-9]+-)? # Optional leading patch number
169
+ (.*?) # Patch name group (non-greedy)
170
+ (?:\.(?:diff|patch))? # Optional .diff or .patch extension
171
+ $
172
+ ''' ,
173
+ r'\g<1>' ,
174
+ name ,
175
+ flags = re .VERBOSE ,
176
+ )
182
177
183
- def unacceptable_name (name ):
184
- return False
178
+ need_unique = not (options .ignore or options .replace )
185
179
186
- else :
187
- unacceptable_name = stack .patches .exists
188
- name = make_patch_name (message , unacceptable_name )
180
+ if name :
181
+ name = stack .patches .make_name (name , unique = need_unique , lower = False )
189
182
else :
190
- # fix possible invalid characters in the patch name
191
- name = re .sub (r'[^\w.]+' , '-' , name ).strip ('-' )
192
-
193
- assert stack .patches .is_name_valid (name )
183
+ name = stack .patches .make_name (message , unique = need_unique , lower = True )
194
184
195
185
if options .ignore and name in stack .patchorder .applied :
196
186
out .info ('Ignoring already applied patch "%s"' % name )
@@ -266,28 +256,30 @@ def __get_handle_and_name(filename):
266
256
return (open (filename , 'rb' ), filename )
267
257
268
258
269
- def __import_file (filename , options , patch = None ):
259
+ def __import_file (filename , options ):
270
260
"""Import a patch from a file or standard input"""
271
- pname = None
272
261
if filename :
273
- ( f , pname ) = __get_handle_and_name (filename )
262
+ f , filename = __get_handle_and_name (filename )
274
263
else :
275
264
f = os .fdopen (sys .__stdin__ .fileno (), 'rb' )
276
265
277
- if patch :
278
- pname = patch
279
- elif not pname :
280
- pname = filename
281
-
282
- (message , author_name , author_email , author_date , diff ) = parse_patch (
283
- f .read (), contains_diff = True
284
- )
266
+ patch_data = f .read ()
285
267
286
268
if filename :
287
269
f .close ()
288
270
271
+ message , author_name , author_email , author_date , diff = parse_patch (
272
+ patch_data , contains_diff = True
273
+ )
274
+
289
275
__create_patch (
290
- pname , message , author_name , author_email , author_date , diff , options
276
+ filename ,
277
+ message ,
278
+ author_name ,
279
+ author_email ,
280
+ author_date ,
281
+ diff ,
282
+ options ,
291
283
)
292
284
293
285
@@ -327,9 +319,8 @@ def __import_series(filename, options):
327
319
else :
328
320
options .strip = 1
329
321
patchfile = os .path .join (patchdir , patch )
330
- patch = __replace_slashes_with_dashes (patch )
331
322
332
- __import_file (patchfile , options , patch )
323
+ __import_file (patchfile , options )
333
324
334
325
if filename :
335
326
f .close ()
0 commit comments