6
6
from .snippets import split_by_regex , findall_regex
7
7
from . import exceptions
8
8
9
- header = namedtuple (
10
- 'header' ,
11
- 'index_path old_path old_version new_path new_version' ,
12
- )
9
+ header = namedtuple ("header" , "index_path old_path old_version new_path new_version" ,)
13
10
14
- diffobj = namedtuple (' diff' , ' header changes text' )
15
- Change = namedtuple (' Change' , ' old new line hunk' )
11
+ diffobj = namedtuple (" diff" , " header changes text" )
12
+ Change = namedtuple (" Change" , " old new line hunk" )
16
13
17
- file_timestamp_str = ' (.+?)(?:\t |:| +)(.*)'
14
+ file_timestamp_str = " (.+?)(?:\t |:| +)(.*)"
18
15
# .+? was previously [^:\t\n\r\f\v]+
19
16
20
17
# general diff regex
21
- diffcmd_header = re .compile (' ^diff.* (.+) (.+)$' )
22
- unified_header_index = re .compile (' ^Index: (.+)$' )
23
- unified_header_old_line = re .compile (r' ^--- ' + file_timestamp_str + '$' )
24
- unified_header_new_line = re .compile (r' ^\+\+\+ ' + file_timestamp_str + '$' )
25
- unified_hunk_start = re .compile (r' ^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)$' )
26
- unified_change = re .compile (' ^([-+ ])(.*)$' )
27
-
28
- context_header_old_line = re .compile (r' ^\*\*\* ' + file_timestamp_str + '$' )
29
- context_header_new_line = re .compile (' ^--- ' + file_timestamp_str + '$' )
30
- context_hunk_start = re .compile (r' ^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$' )
31
- context_hunk_old = re .compile (r' ^\*\*\* (\d+),?(\d*) \*\*\*\*$' )
32
- context_hunk_new = re .compile (r' ^--- (\d+),?(\d*) ----$' )
33
- context_change = re .compile (' ^([-+ !]) (.*)$' )
34
-
35
- ed_hunk_start = re .compile (r' ^(\d+),?(\d*)([acd])$' )
36
- ed_hunk_end = re .compile (' ^.$' )
18
+ diffcmd_header = re .compile (" ^diff.* (.+) (.+)$" )
19
+ unified_header_index = re .compile (" ^Index: (.+)$" )
20
+ unified_header_old_line = re .compile (r" ^--- " + file_timestamp_str + "$" )
21
+ unified_header_new_line = re .compile (r" ^\+\+\+ " + file_timestamp_str + "$" )
22
+ unified_hunk_start = re .compile (r" ^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)$" )
23
+ unified_change = re .compile (" ^([-+ ])(.*)$" )
24
+
25
+ context_header_old_line = re .compile (r" ^\*\*\* " + file_timestamp_str + "$" )
26
+ context_header_new_line = re .compile (" ^--- " + file_timestamp_str + "$" )
27
+ context_hunk_start = re .compile (r" ^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$" )
28
+ context_hunk_old = re .compile (r" ^\*\*\* (\d+),?(\d*) \*\*\*\*$" )
29
+ context_hunk_new = re .compile (r" ^--- (\d+),?(\d*) ----$" )
30
+ context_change = re .compile (" ^([-+ !]) (.*)$" )
31
+
32
+ ed_hunk_start = re .compile (r" ^(\d+),?(\d*)([acd])$" )
33
+ ed_hunk_end = re .compile (" ^.$" )
37
34
# much like forward ed, but no 'c' type
38
- rcs_ed_hunk_start = re .compile (r' ^([ad])(\d+) ?(\d*)$' )
35
+ rcs_ed_hunk_start = re .compile (r" ^([ad])(\d+) ?(\d*)$" )
39
36
40
- default_hunk_start = re .compile (r' ^(\d+),?(\d*)([acd])(\d+),?(\d*)$' )
41
- default_hunk_mid = re .compile (' ^---$' )
42
- default_change = re .compile (' ^([><]) (.*)$' )
37
+ default_hunk_start = re .compile (r" ^(\d+),?(\d*)([acd])(\d+),?(\d*)$" )
38
+ default_hunk_mid = re .compile (" ^---$" )
39
+ default_change = re .compile (" ^([><]) (.*)$" )
43
40
44
41
# Headers
45
42
46
43
# git has a special index header and no end part
47
- git_diffcmd_header = re .compile (' ^diff --git a/(.+) b/(.+)$' )
48
- git_header_index = re .compile (r' ^index ([a-f0-9]+)..([a-f0-9]+) ?(\d*)$' )
49
- git_header_old_line = re .compile (' ^--- (.+)$' )
50
- git_header_new_line = re .compile (r' ^\+\+\+ (.+)$' )
51
- git_header_file_mode = re .compile (r' ^(new|deleted) file mode \d{6}$' )
52
- git_header_binary_file = re .compile (' ^Binary files (.+) and (.+) differ' )
44
+ git_diffcmd_header = re .compile (" ^diff --git a/(.+) b/(.+)$" )
45
+ git_header_index = re .compile (r" ^index ([a-f0-9]+)..([a-f0-9]+) ?(\d*)$" )
46
+ git_header_old_line = re .compile (" ^--- (.+)$" )
47
+ git_header_new_line = re .compile (r" ^\+\+\+ (.+)$" )
48
+ git_header_file_mode = re .compile (r" ^(new|deleted) file mode \d{6}$" )
49
+ git_header_binary_file = re .compile (" ^Binary files (.+) and (.+) differ" )
53
50
54
51
bzr_header_index = re .compile ("=== (.+)" )
55
52
bzr_header_old_line = unified_header_old_line
56
53
bzr_header_new_line = unified_header_new_line
57
54
58
55
svn_header_index = unified_header_index
59
- svn_header_timestamp_version = re .compile (
60
- r'\((?:working copy|revision (\d+))\)'
61
- )
62
- svn_header_timestamp = re .compile (r'.*(\(.*\))$' )
56
+ svn_header_timestamp_version = re .compile (r"\((?:working copy|revision (\d+))\)" )
57
+ svn_header_timestamp = re .compile (r".*(\(.*\))$" )
63
58
64
59
cvs_header_index = unified_header_index
65
- cvs_header_rcs = re .compile (r' ^RCS file: (.+)(?:,\w{1}$|$)' )
66
- cvs_header_timestamp = re .compile (r' (.+)\t([\d.]+)' )
67
- cvs_header_timestamp_colon = re .compile (r' :([\d.]+)\t(.+)' )
68
- old_cvs_diffcmd_header = re .compile (' ^diff.* (.+):(.*) (.+):(.*)$' )
60
+ cvs_header_rcs = re .compile (r" ^RCS file: (.+)(?:,\w{1}$|$)" )
61
+ cvs_header_timestamp = re .compile (r" (.+)\t([\d.]+)" )
62
+ cvs_header_timestamp_colon = re .compile (r" :([\d.]+)\t(.+)" )
63
+ old_cvs_diffcmd_header = re .compile (" ^diff.* (.+):(.*) (.+):(.*)$" )
69
64
70
65
71
66
def parse_patch (text ):
@@ -93,7 +88,7 @@ def parse_patch(text):
93
88
break
94
89
95
90
for diff in diffs :
96
- difftext = ' \n ' .join (diff ) + ' \n '
91
+ difftext = " \n " .join (diff ) + " \n "
97
92
h = parse_header (diff )
98
93
d = parse_diff (diff )
99
94
if h or d :
@@ -114,11 +109,11 @@ def parse_scm_header(text):
114
109
lines = text
115
110
116
111
check = [
117
- (git_header_index , parse_git_header ),
118
- (old_cvs_diffcmd_header , parse_cvs_header ),
119
- (cvs_header_rcs , parse_cvs_header ),
120
- (svn_header_index , parse_svn_header ),
121
- ]
112
+ (git_header_index , parse_git_header ),
113
+ (old_cvs_diffcmd_header , parse_cvs_header ),
114
+ (cvs_header_rcs , parse_cvs_header ),
115
+ (svn_header_index , parse_svn_header ),
116
+ ]
122
117
123
118
for regex , parser in check :
124
119
diffs = findall_regex (lines , regex )
@@ -129,18 +124,18 @@ def parse_scm_header(text):
129
124
if res :
130
125
old_path = res .old_path
131
126
new_path = res .new_path
132
- if old_path .startswith ('a/' ):
127
+ if old_path .startswith ("a/" ):
133
128
old_path = old_path [2 :]
134
129
135
- if new_path .startswith ('b/' ):
130
+ if new_path .startswith ("b/" ):
136
131
new_path = new_path [2 :]
137
132
138
133
return header (
139
134
index_path = res .index_path ,
140
135
old_path = old_path ,
141
136
old_version = res .old_version ,
142
137
new_path = new_path ,
143
- new_version = res .new_version
138
+ new_version = res .new_version ,
144
139
)
145
140
else :
146
141
res = parser (lines )
@@ -234,37 +229,37 @@ def parse_git_header(text):
234
229
new_path = binary .group (2 )
235
230
236
231
if old_path and new_path :
237
- if old_path .startswith ('a/' ):
232
+ if old_path .startswith ("a/" ):
238
233
old_path = old_path [2 :]
239
234
240
- if new_path .startswith ('b/' ):
235
+ if new_path .startswith ("b/" ):
241
236
new_path = new_path [2 :]
242
237
return header (
243
238
index_path = None ,
244
239
old_path = old_path ,
245
240
old_version = old_version ,
246
241
new_path = new_path ,
247
- new_version = new_version
242
+ new_version = new_version ,
248
243
)
249
244
250
245
# if we go through all of the text without finding our normal info,
251
246
# use the cmd if available
252
247
if cmd_old_path and cmd_new_path and old_version and new_version :
253
248
print ("returning from dumb path" )
254
- if cmd_old_path .startswith ('a/' ):
249
+ if cmd_old_path .startswith ("a/" ):
255
250
cmd_old_path = cmd_old_path [2 :]
256
251
257
- if cmd_new_path .startswith ('b/' ):
252
+ if cmd_new_path .startswith ("b/" ):
258
253
cmd_new_path = cmd_new_path [2 :]
259
254
260
255
return header (
261
256
index_path = None ,
262
257
# wow, I kind of hate this:
263
258
# assume /dev/null if the versions are zeroed out
264
- old_path = ' /dev/null' if old_version == ' 0000000' else cmd_old_path ,
259
+ old_path = " /dev/null" if old_version == " 0000000" else cmd_old_path ,
265
260
old_version = old_version ,
266
- new_path = ' /dev/null' if new_version == ' 0000000' else cmd_new_path ,
267
- new_version = new_version
261
+ new_path = " /dev/null" if new_version == " 0000000" else cmd_new_path ,
262
+ new_version = new_version ,
268
263
)
269
264
270
265
return None
@@ -305,7 +300,7 @@ def parse_svn_header(text):
305
300
elif opath :
306
301
ts = svn_header_timestamp .match (opath )
307
302
if ts :
308
- opath = opath [:- len (ts .group (1 ))]
303
+ opath = opath [: - len (ts .group (1 ))]
309
304
oend = svn_header_timestamp_version .match (ts .group (1 ))
310
305
if oend and oend .group (1 ):
311
306
over = int (oend .group (1 ))
@@ -319,7 +314,7 @@ def parse_svn_header(text):
319
314
elif npath :
320
315
ts = svn_header_timestamp .match (npath )
321
316
if ts :
322
- npath = npath [:- len (ts .group (1 ))]
317
+ npath = npath [: - len (ts .group (1 ))]
323
318
nend = svn_header_timestamp_version .match (ts .group (1 ))
324
319
if nend and nend .group (1 ):
325
320
nver = int (nend .group (1 ))
@@ -564,10 +559,10 @@ def parse_default_diff(text):
564
559
kind = c .group (1 )
565
560
line = c .group (2 )
566
561
567
- if kind == '<' and (r != old_len or r == 0 ):
562
+ if kind == "<" and (r != old_len or r == 0 ):
568
563
changes .append (Change (old + r , None , line , hunk_n ))
569
564
r += 1
570
- elif kind == '>' and (i != new_len or i == 0 ):
565
+ elif kind == ">" and (i != new_len or i == 0 ):
571
566
changes .append (Change (None , new + i , line , hunk_n ))
572
567
i += 1
573
568
@@ -621,13 +616,13 @@ def parse_unified_diff(text):
621
616
line = c .group (2 )
622
617
c = None
623
618
624
- if kind == '-' and (r != old_len or r == 0 ):
619
+ if kind == "-" and (r != old_len or r == 0 ):
625
620
changes .append (Change (old + r , None , line , hunk_n ))
626
621
r += 1
627
- elif kind == '+' and (i != new_len or i == 0 ):
622
+ elif kind == "+" and (i != new_len or i == 0 ):
628
623
changes .append (Change (None , new + i , line , hunk_n ))
629
624
i += 1
630
- elif kind == ' ' and r != old_len and i != new_len :
625
+ elif kind == " " and r != old_len and i != new_len :
631
626
changes .append (Change (old + r , new + i , line , hunk_n ))
632
627
r += 1
633
628
i += 1
@@ -702,15 +697,16 @@ def parse_context_diff(text):
702
697
kind = c .group (1 )
703
698
line = c .group (2 )
704
699
705
- if kind == '-' and (j != old_len or j == 0 ):
700
+ if kind == "-" and (j != old_len or j == 0 ):
706
701
changes .append (Change (old + j , None , line , hunk_n ))
707
702
j += 1
708
- elif kind == ' ' and ((j != old_len and k != new_len )
709
- or (j == 0 or k == 0 )):
703
+ elif kind == " " and (
704
+ (j != old_len and k != new_len ) or (j == 0 or k == 0 )
705
+ ):
710
706
changes .append (Change (old + j , new + k , line , hunk_n ))
711
707
j += 1
712
708
k += 1
713
- elif kind == '+' or kind == '!' :
709
+ elif kind == "+" or kind == "!" :
714
710
raise exceptions .ParseException (msg + kind , hunk_n )
715
711
716
712
continue
@@ -728,15 +724,16 @@ def parse_context_diff(text):
728
724
kind = c .group (1 )
729
725
line = c .group (2 )
730
726
731
- if kind == '+' and (k != new_len or k == 0 ):
727
+ if kind == "+" and (k != new_len or k == 0 ):
732
728
changes .append (Change (None , new + k , line , hunk_n ))
733
729
k += 1
734
- elif kind == ' ' and ((j != old_len and k != new_len )
735
- or (j == 0 or k == 0 )):
730
+ elif kind == " " and (
731
+ (j != old_len and k != new_len ) or (j == 0 or k == 0 )
732
+ ):
736
733
changes .append (Change (old + j , new + k , line , hunk_n ))
737
734
j += 1
738
735
k += 1
739
- elif kind == '-' or kind == '!' :
736
+ elif kind == "-" or kind == "!" :
740
737
raise exceptions .ParseException (msg + kind , hunk_n )
741
738
continue
742
739
@@ -758,17 +755,17 @@ def parse_context_diff(text):
758
755
if not (oc or nc ):
759
756
del old_hunk [0 ]
760
757
del new_hunk [0 ]
761
- elif okind == ' ' and nkind == ' ' and oline == nline :
758
+ elif okind == " " and nkind == " " and oline == nline :
762
759
changes .append (Change (old + j , new + k , oline , hunk_n ))
763
760
j += 1
764
761
k += 1
765
762
del old_hunk [0 ]
766
763
del new_hunk [0 ]
767
- elif okind == '-' or okind == '!' and (j != old_len or j == 0 ):
764
+ elif okind == "-" or okind == "!" and (j != old_len or j == 0 ):
768
765
changes .append (Change (old + j , None , oline , hunk_n ))
769
766
j += 1
770
767
del old_hunk [0 ]
771
- elif nkind == '+' or nkind == '!' and (k != new_len or k == 0 ):
768
+ elif nkind == "+" or nkind == "!" and (k != new_len or k == 0 ):
772
769
changes .append (Change (None , new + k , nline , hunk_n ))
773
770
k += 1
774
771
del new_hunk [0 ]
@@ -814,7 +811,7 @@ def parse_ed_diff(text):
814
811
old_end = int (o .group (2 )) if len (o .group (2 )) else old
815
812
816
813
hunk_kind = o .group (3 )
817
- if hunk_kind == 'd' :
814
+ if hunk_kind == "d" :
818
815
k = 0
819
816
while old_end >= old :
820
817
changes .append (Change (old + k , None , None , hunk_n ))
@@ -825,7 +822,7 @@ def parse_ed_diff(text):
825
822
826
823
while len (hunk ) > 0 :
827
824
e = ed_hunk_end .match (hunk [0 ])
828
- if not e and hunk_kind == 'c' :
825
+ if not e and hunk_kind == "c" :
829
826
k = 0
830
827
while old_end >= old :
831
828
changes .append (Change (old + k , None , None , hunk_n ))
@@ -835,21 +832,11 @@ def parse_ed_diff(text):
835
832
836
833
# I basically have no idea why this works
837
834
# for these tests.
838
- changes .append (Change (
839
- None ,
840
- old - r + i + k + j ,
841
- hunk [0 ],
842
- hunk_n ,
843
- ))
835
+ changes .append (Change (None , old - r + i + k + j , hunk [0 ], hunk_n ,))
844
836
i += 1
845
837
j += 1
846
- if not e and hunk_kind == 'a' :
847
- changes .append (Change (
848
- None ,
849
- old - r + i + 1 ,
850
- hunk [0 ],
851
- hunk_n ,
852
- ))
838
+ if not e and hunk_kind == "a" :
839
+ changes .append (Change (None , old - r + i + 1 , hunk [0 ], hunk_n ,))
853
840
i += 1
854
841
855
842
del hunk [0 ]
@@ -889,7 +876,7 @@ def parse_rcs_ed_diff(text):
889
876
old = int (o .group (2 ))
890
877
size = int (o .group (3 ))
891
878
892
- if hunk_kind == 'a' :
879
+ if hunk_kind == "a" :
893
880
old += total_change_size + 1
894
881
total_change_size += size
895
882
while size > 0 and len (hunk ) > 0 :
@@ -899,7 +886,7 @@ def parse_rcs_ed_diff(text):
899
886
900
887
del hunk [0 ]
901
888
902
- elif hunk_kind == 'd' :
889
+ elif hunk_kind == "d" :
903
890
total_change_size -= size
904
891
while size > 0 :
905
892
changes .append (Change (old + j , None , None , hunk_n ))
0 commit comments