55import hashlib
66import stat
77from collections .abc import Sequence
8- from dataclasses import dataclass
8+ from dataclasses import dataclass , field
99from pathlib import Path
1010
1111import patch_ng
@@ -211,7 +211,9 @@ class PatchInfo:
211211 total_patches : int = 1
212212 current_patch_idx : int = 1
213213 revision : str = ""
214- date : datetime .datetime = datetime .datetime .now (tz = datetime .timezone .utc )
214+ date : datetime .datetime = field (
215+ default_factory = lambda : datetime .datetime .now (tz = datetime .timezone .utc )
216+ )
215217 description : str = ""
216218
217219 def to_git_header (self ) -> str :
@@ -235,50 +237,42 @@ def to_svn_header(self) -> str:
235237 return ""
236238
237239
238- def add_prefix_to_patch (file_path : str , path_prefix : str , is_git : bool = True ) -> str :
239- """Rewrite a patch to prefix file paths."""
240+ def add_prefix_to_patch (file_path : str , path_prefix : str ) -> str :
241+ """Add a prefix to all file paths in the given patch file ."""
240242 patch = patch_ng .fromfile (file_path )
241-
242- if not patch :
243+ if not patch or not patch .items :
243244 return ""
244245
245- out : list [bytes ] = []
246+ # make sure prefix is bytes
247+ prefix = path_prefix .strip ("/" ).encode () # b"myprefix"
248+ if prefix :
249+ prefix += b"/"
246250
247251 for file in patch .items :
248- # normalize prefix (no leading/trailing slash surprises)
249- prefix = path_prefix .strip ("/" ).encode ()
250- prefix = prefix + b"/" if prefix else b""
251-
252- src = file .source
253- tgt = file .target
254-
255- # strip a/ b/ if present
256- if src .startswith (b"a/" ):
257- src = src [2 :]
258- if tgt .startswith (b"b/" ):
259- tgt = tgt [2 :]
260-
261- new_src = b"a/" + prefix + src
262- new_tgt = b"b/" + prefix + tgt
263-
264- # diff header
265- out .append (b"" )
266- if is_git :
267- out .append (b"diff --git " + new_src + b" " + new_tgt )
268- else :
269- out .append (b"Index: " + new_src )
270- out .append (b"=" * 67 )
271- out .append (b"--- " + new_src )
272- out .append (b"+++ " + new_tgt )
273252
274- for hunk in file . hunks :
275- out . append (
276- f"@@ - { hunk . startsrc } , { hunk . linessrc } "
277- f"+ { hunk . starttgt } , { hunk . linestgt } @@" . encode ()
278- )
279- for line in hunk . text :
280- out . append ( line . rstrip ( b" \r \n " ) )
253+ def rewrite_path ( path : bytes ) -> bytes :
254+ if path == b"/dev/null" :
255+ return b"/dev/null "
256+ return prefix + path
257+
258+ file . source = rewrite_path ( file . source )
259+ file . target = rewrite_path ( file . target )
281260
282- out . append ( b"" ) # blank line between files
261+ return dump ( patch )
283262
284- return b"\n " .join (out ).decode ("utf-8" )
263+
264+ def dump (patch_set : patch_ng .PatchSet ) -> str :
265+ """Dump a patch set to a string."""
266+ output_lines : list [str ] = []
267+ for p in patch_set .items :
268+ for headline in p .header :
269+ output_lines .append (headline .rstrip (b"\r \n " ).decode ("utf-8" ))
270+ output_lines .append ("--- " + p .source .decode ("utf-8" ))
271+ output_lines .append ("+++ " + p .target .decode ("utf-8" ))
272+ for h in p .hunks :
273+ output_lines .append (
274+ f"@@ -{ h .startsrc } ,{ h .linessrc } +{ h .starttgt } ,{ h .linestgt } @@"
275+ )
276+ for line in h .text :
277+ output_lines .append (line .rstrip (b"\r \n " ).decode ("utf-8" ))
278+ return "\n " .join (output_lines )
0 commit comments