FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/coders/udiff_coder.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
13
(13)
Pull requests
8
(8)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
coders
/
udiff_coder.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
428 lines (319 loc) · 10.7 KB
Breadcrumbs
cecli
/
cecli
/
coders
/
udiff_coder.py
Copy path
File metadata and controls
428 lines (319 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import
difflib
from
itertools
import
groupby
from
pathlib
import
Path
from
..
dump
import
dump
# noqa: F401
from
.
base_coder
import
Coder
from
.
search_replace
import
(
SearchTextNotUnique
,
all_preprocs
,
diff_lines
,
flexible_search_and_replace
,
search_and_replace
,
)
no_match_error
=
"""UnifiedDiffNoMatch: hunk failed to apply!
{path} does not contain lines that match the diff you provided!
Try again.
DO NOT skip blank lines, comments, docstrings, etc!
The diff needs to apply cleanly to the lines in {path}!
{path} does not contain these {num_lines} exact lines in a row:
```
{original}```
"""
not_unique_error
=
"""UnifiedDiffNotUnique: hunk failed to apply!
{path} contains multiple sets of lines that match the diff you provided!
Try again.
Use additional ` ` lines to provide context that uniquely indicates which code needs to be changed.
The diff needs to apply to a unique set of lines in {path}!
{path} contains multiple copies of these {num_lines} lines:
```
{original}```
"""
other_hunks_applied
=
(
"Note: some hunks did apply successfully. See the updated source code shown above.
\n
\n
"
)
class
UnifiedDiffCoder
(
Coder
):
"""A coder that uses unified diff format for code modifications."""
edit_format
=
"udiff"
prompt_format
=
"udiff"
def
get_edits
(
self
):
content
=
self
.
partial_response_content
# might raise ValueError for malformed ORIG/UPD blocks
raw_edits
=
list
(
find_diffs
(
content
))
last_path
=
None
edits
=
[]
for
path
,
hunk
in
raw_edits
:
if
path
:
last_path
=
path
else
:
path
=
last_path
edits
.
append
((
path
,
hunk
))
return
edits
def
apply_edits
(
self
,
edits
):
seen
=
set
()
uniq
=
[]
for
path
,
hunk
in
edits
:
hunk
=
normalize_hunk
(
hunk
)
if
not
hunk
:
continue
this
=
[
path
+
"
\n
"
]
+
hunk
this
=
""
.
join
(
this
)
if
this
in
seen
:
continue
seen
.
add
(
this
)
uniq
.
append
((
path
,
hunk
))
errors
=
[]
for
path
,
hunk
in
uniq
:
full_path
=
self
.
abs_root_path
(
path
)
content
=
self
.
io
.
read_text
(
full_path
)
original
,
_
=
hunk_to_before_after
(
hunk
)
try
:
content
=
do_replace
(
full_path
,
content
,
hunk
)
except
SearchTextNotUnique
:
errors
.
append
(
not_unique_error
.
format
(
path
=
path
,
original
=
original
,
num_lines
=
len
(
original
.
splitlines
())
)
)
continue
if
not
content
:
errors
.
append
(
no_match_error
.
format
(
path
=
path
,
original
=
original
,
num_lines
=
len
(
original
.
splitlines
())
)
)
continue
# SUCCESS!
self
.
io
.
write_text
(
full_path
,
content
)
if
errors
:
errors
=
"
\n
\n
"
.
join
(
errors
)
if
len
(
errors
)
<
len
(
uniq
):
errors
+=
other_hunks_applied
raise
ValueError
(
errors
)
def
do_replace
(
fname
,
content
,
hunk
):
fname
=
Path
(
fname
)
before_text
,
after_text
=
hunk_to_before_after
(
hunk
)
# does it want to make a new file?
if
not
fname
.
exists
()
and
not
before_text
.
strip
():
fname
.
touch
()
content
=
""
if
content
is
None
:
return
# TODO: handle inserting into new file
if
not
before_text
.
strip
():
# append to existing file, or start a new file
new_content
=
content
+
after_text
return
new_content
new_content
=
None
new_content
=
apply_hunk
(
content
,
hunk
)
if
new_content
:
return
new_content
def
collapse_repeats
(
s
):
return
""
.
join
(
k
for
k
,
g
in
groupby
(
s
))
def
apply_hunk
(
content
,
hunk
):
before_text
,
after_text
=
hunk_to_before_after
(
hunk
)
res
=
directly_apply_hunk
(
content
,
hunk
)
if
res
:
return
res
hunk
=
make_new_lines_explicit
(
content
,
hunk
)
# just consider space vs not-space
ops
=
""
.
join
([
line
[
0
]
for
line
in
hunk
])
ops
=
ops
.
replace
(
"-"
,
"x"
)
ops
=
ops
.
replace
(
"+"
,
"x"
)
ops
=
ops
.
replace
(
"
\n
"
,
" "
)
cur_op
=
" "
section
=
[]
sections
=
[]
for
i
in
range
(
len
(
ops
)):
op
=
ops
[
i
]
if
op
!=
cur_op
:
sections
.
append
(
section
)
section
=
[]
cur_op
=
op
section
.
append
(
hunk
[
i
])
sections
.
append
(
section
)
if
cur_op
!=
" "
:
sections
.
append
([])
all_done
=
True
for
i
in
range
(
2
,
len
(
sections
),
2
):
preceding_context
=
sections
[
i
-
2
]
changes
=
sections
[
i
-
1
]
following_context
=
sections
[
i
]
res
=
apply_partial_hunk
(
content
,
preceding_context
,
changes
,
following_context
)
if
res
:
content
=
res
else
:
all_done
=
False
# FAILED!
# this_hunk = preceding_context + changes + following_context
break
if
all_done
:
return
content
def
flexi_just_search_and_replace
(
texts
):
strategies
=
[
(
search_and_replace
,
all_preprocs
),
]
return
flexible_search_and_replace
(
texts
,
strategies
)
def
make_new_lines_explicit
(
content
,
hunk
):
before
,
after
=
hunk_to_before_after
(
hunk
)
diff
=
diff_lines
(
before
,
content
)
back_diff
=
[]
for
line
in
diff
:
if
line
[
0
]
==
"+"
:
continue
# if line[0] == "-":
# line = "+" + line[1:]
back_diff
.
append
(
line
)
new_before
=
directly_apply_hunk
(
before
,
back_diff
)
if
not
new_before
:
return
hunk
if
len
(
new_before
.
strip
())
<
10
:
return
hunk
before
=
before
.
splitlines
(
keepends
=
True
)
new_before
=
new_before
.
splitlines
(
keepends
=
True
)
after
=
after
.
splitlines
(
keepends
=
True
)
if
len
(
new_before
)
<
len
(
before
)
*
0.66
:
return
hunk
new_hunk
=
difflib
.
unified_diff
(
new_before
,
after
,
n
=
max
(
len
(
new_before
),
len
(
after
)))
new_hunk
=
list
(
new_hunk
)[
3
:]
return
new_hunk
def
cleanup_pure_whitespace_lines
(
lines
):
res
=
[
line
if
line
.
strip
()
else
line
[
-
(
len
(
line
)
-
len
(
line
.
rstrip
(
"
\r
\n
"
)))]
for
line
in
lines
]
return
res
def
normalize_hunk
(
hunk
):
before
,
after
=
hunk_to_before_after
(
hunk
,
lines
=
True
)
before
=
cleanup_pure_whitespace_lines
(
before
)
after
=
cleanup_pure_whitespace_lines
(
after
)
diff
=
difflib
.
unified_diff
(
before
,
after
,
n
=
max
(
len
(
before
),
len
(
after
)))
diff
=
list
(
diff
)[
3
:]
return
diff
def
directly_apply_hunk
(
content
,
hunk
):
before
,
after
=
hunk_to_before_after
(
hunk
)
if
not
before
:
return
before_lines
,
_
=
hunk_to_before_after
(
hunk
,
lines
=
True
)
before_lines
=
""
.
join
([
line
.
strip
()
for
line
in
before_lines
])
# Refuse to do a repeated search and replace on a tiny bit of non-whitespace context
if
len
(
before_lines
)
<
10
and
content
.
count
(
before
)
>
1
:
return
try
:
new_content
=
flexi_just_search_and_replace
([
before
,
after
,
content
])
except
SearchTextNotUnique
:
new_content
=
None
return
new_content
def
apply_partial_hunk
(
content
,
preceding_context
,
changes
,
following_context
):
len_prec
=
len
(
preceding_context
)
len_foll
=
len
(
following_context
)
use_all
=
len_prec
+
len_foll
# if there is a - in the hunk, we can go all the way to `use=0`
for
drop
in
range
(
use_all
+
1
):
use
=
use_all
-
drop
for
use_prec
in
range
(
len_prec
,
-
1
,
-
1
):
if
use_prec
>
use
:
continue
use_foll
=
use
-
use_prec
if
use_foll
>
len_foll
:
continue
if
use_prec
:
this_prec
=
preceding_context
[
-
use_prec
:]
else
:
this_prec
=
[]
this_foll
=
following_context
[:
use_foll
]
res
=
directly_apply_hunk
(
content
,
this_prec
+
changes
+
this_foll
)
if
res
:
return
res
def
find_diffs
(
content
):
# We can always fence with triple-quotes, because all the udiff content
# is prefixed with +/-/space.
if
not
content
.
endswith
(
"
\n
"
):
content
=
content
+
"
\n
"
lines
=
content
.
splitlines
(
keepends
=
True
)
line_num
=
0
edits
=
[]
while
line_num
<
len
(
lines
):
while
line_num
<
len
(
lines
):
line
=
lines
[
line_num
]
if
line
.
startswith
(
"```diff"
):
line_num
,
these_edits
=
process_fenced_block
(
lines
,
line_num
+
1
)
edits
+=
these_edits
break
line_num
+=
1
# For now, just take 1!
# edits = edits[:1]
return
edits
def
process_fenced_block
(
lines
,
start_line_num
):
for
line_num
in
range
(
start_line_num
,
len
(
lines
)):
line
=
lines
[
line_num
]
if
line
.
startswith
(
"```"
):
break
block
=
lines
[
start_line_num
:
line_num
]
block
.
append
(
"@@ @@"
)
if
block
[
0
].
startswith
(
"--- "
)
and
block
[
1
].
startswith
(
"+++ "
):
# Extract the file path, considering that it might contain spaces
a_fname
=
block
[
0
][
4
:].
strip
()
b_fname
=
block
[
1
][
4
:].
strip
()
# Check if standard git diff prefixes are present (or /dev/null) and strip them
if
(
a_fname
.
startswith
(
"a/"
)
or
a_fname
==
"/dev/null"
)
and
b_fname
.
startswith
(
"b/"
):
fname
=
b_fname
[
2
:]
else
:
# Otherwise, assume the path is as intended
fname
=
b_fname
block
=
block
[
2
:]
else
:
fname
=
None
edits
=
[]
keeper
=
False
hunk
=
[]
op
=
" "
for
line
in
block
:
hunk
.
append
(
line
)
if
len
(
line
)
<
2
:
continue
if
line
.
startswith
(
"+++ "
)
and
hunk
[
-
2
].
startswith
(
"--- "
):
if
hunk
[
-
3
]
==
"
\n
"
:
hunk
=
hunk
[:
-
3
]
else
:
hunk
=
hunk
[:
-
2
]
edits
.
append
((
fname
,
hunk
))
hunk
=
[]
keeper
=
False
fname
=
line
[
4
:].
strip
()
continue
op
=
line
[
0
]
if
op
in
"-+"
:
keeper
=
True
continue
if
op
!=
"@"
:
continue
if
not
keeper
:
hunk
=
[]
continue
hunk
=
hunk
[:
-
1
]
edits
.
append
((
fname
,
hunk
))
hunk
=
[]
keeper
=
False
return
line_num
+
1
,
edits
def
hunk_to_before_after
(
hunk
,
lines
=
False
):
before
=
[]
after
=
[]
op
=
" "
for
line
in
hunk
:
if
len
(
line
)
<
2
:
op
=
" "
line
=
line
else
:
op
=
line
[
0
]
line
=
line
[
1
:]
if
op
==
" "
:
before
.
append
(
line
)
after
.
append
(
line
)
elif
op
==
"-"
:
before
.
append
(
line
)
elif
op
==
"+"
:
after
.
append
(
line
)
if
lines
:
return
before
,
after
before
=
""
.
join
(
before
)
after
=
""
.
join
(
after
)
return
before
,
after
Back
|
FazBrowse Home
|
New Git URL