gptdiff 0.1.9__py3-none-any.whl → 0.1.11__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- gptdiff/gptdiff.py +256 -53
- {gptdiff-0.1.9.dist-info → gptdiff-0.1.11.dist-info}/METADATA +62 -37
- gptdiff-0.1.11.dist-info/RECORD +8 -0
- gptdiff-0.1.9.dist-info/RECORD +0 -8
- {gptdiff-0.1.9.dist-info → gptdiff-0.1.11.dist-info}/LICENSE.txt +0 -0
- {gptdiff-0.1.9.dist-info → gptdiff-0.1.11.dist-info}/WHEEL +0 -0
- {gptdiff-0.1.9.dist-info → gptdiff-0.1.11.dist-info}/entry_points.txt +0 -0
- {gptdiff-0.1.9.dist-info → gptdiff-0.1.11.dist-info}/top_level.txt +0 -0
gptdiff/gptdiff.py
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
#!/usr/bin/env python3
|
2
|
+
from pathlib import Path
|
3
|
+
import subprocess
|
4
|
+
import hashlib
|
5
|
+
import re
|
6
|
+
|
2
7
|
|
3
8
|
import openai
|
4
9
|
from openai import OpenAI
|
@@ -44,6 +49,9 @@ a/file.py b/file.py
|
|
44
49
|
@@ -1,2 +1,2 @@
|
45
50
|
-def old():
|
46
51
|
+def new():
|
52
|
+
|
53
|
+
-
|
54
|
+
You must include the '--- file' and/or '+++ file' part of the diff. File modifications should include both.
|
47
55
|
"""
|
48
56
|
)
|
49
57
|
return toolbox
|
@@ -252,12 +260,20 @@ def build_environment(files_dict):
|
|
252
260
|
return '\n'.join(env)
|
253
261
|
|
254
262
|
def generate_diff(environment, goal, model=None, temperature=0.7, max_tokens=32000, api_key=None, base_url=None, prepend=None):
|
255
|
-
"""API: Generate diff from environment and goal
|
263
|
+
"""API: Generate a git diff from the environment and goal.
|
264
|
+
|
265
|
+
If 'prepend' is provided, it should be a path to a file whose content will be
|
266
|
+
prepended to the system prompt.
|
267
|
+
"""
|
256
268
|
if model is None:
|
257
269
|
model = os.getenv('GPTDIFF_MODEL', 'deepseek-reasoner')
|
258
270
|
if prepend:
|
259
|
-
prepend
|
260
|
-
|
271
|
+
if prepend.startswith("http://") or prepend.startswith("https://"):
|
272
|
+
import urllib.request
|
273
|
+
with urllib.request.urlopen(prepend) as response:
|
274
|
+
prepend = response.read().decode('utf-8')
|
275
|
+
else:
|
276
|
+
prepend = load_prepend_file(prepend)
|
261
277
|
else:
|
262
278
|
prepend = ""
|
263
279
|
|
@@ -340,18 +356,172 @@ def smartapply(diff_text, files, model=None, api_key=None, base_url=None):
|
|
340
356
|
|
341
357
|
return files
|
342
358
|
|
343
|
-
# Function to apply diff to project files
|
344
359
|
def apply_diff(project_dir, diff_text):
|
345
|
-
|
346
|
-
|
347
|
-
|
360
|
+
"""
|
361
|
+
Applies a unified diff (as generated by git diff) to the files in project_dir
|
362
|
+
using pure Python (without calling the external 'patch' command).
|
348
363
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
364
|
+
Handles file modifications, new file creation, and file deletions.
|
365
|
+
|
366
|
+
Returns:
|
367
|
+
True if at least one file was modified (or deleted/created) as a result of the patch,
|
368
|
+
False otherwise.
|
369
|
+
"""
|
370
|
+
from pathlib import Path
|
371
|
+
import re, hashlib
|
372
|
+
|
373
|
+
def file_hash(filepath):
|
374
|
+
h = hashlib.sha256()
|
375
|
+
with open(filepath, "rb") as f:
|
376
|
+
h.update(f.read())
|
377
|
+
return h.hexdigest()
|
378
|
+
|
379
|
+
def apply_patch_to_file(file_path, patch):
|
380
|
+
"""
|
381
|
+
Applies a unified diff patch (for a single file) to file_path.
|
382
|
+
|
383
|
+
Returns True if the patch was applied successfully, False otherwise.
|
384
|
+
"""
|
385
|
+
# Read the original file lines; if the file doesn't exist, treat it as empty.
|
386
|
+
if file_path.exists():
|
387
|
+
original_lines = file_path.read_text(encoding="utf8").splitlines(keepends=True)
|
388
|
+
else:
|
389
|
+
original_lines = []
|
390
|
+
new_lines = []
|
391
|
+
current_index = 0
|
392
|
+
|
393
|
+
patch_lines = patch.splitlines()
|
394
|
+
# Regex for a hunk header, e.g., @@ -3,7 +3,6 @@
|
395
|
+
hunk_header_re = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@")
|
396
|
+
i = 0
|
397
|
+
while i < len(patch_lines):
|
398
|
+
line = patch_lines[i]
|
399
|
+
if line.startswith("@@"):
|
400
|
+
m = hunk_header_re.match(line)
|
401
|
+
if not m:
|
402
|
+
print("Invalid hunk header:", line)
|
403
|
+
return False
|
404
|
+
orig_start = int(m.group(1))
|
405
|
+
# orig_len = int(m.group(2)) if m.group(2) else 1 # not used explicitly
|
406
|
+
# new_start = int(m.group(3))
|
407
|
+
# new_len = int(m.group(4)) if m.group(4) else 1
|
408
|
+
|
409
|
+
# Copy unchanged lines before the hunk.
|
410
|
+
hunk_start_index = orig_start - 1 # diff headers are 1-indexed
|
411
|
+
if hunk_start_index > len(original_lines):
|
412
|
+
print("Hunk start index beyond file length")
|
413
|
+
return False
|
414
|
+
new_lines.extend(original_lines[current_index:hunk_start_index])
|
415
|
+
current_index = hunk_start_index
|
416
|
+
|
417
|
+
i += 1
|
418
|
+
# Process the hunk lines until the next hunk header.
|
419
|
+
while i < len(patch_lines) and not patch_lines[i].startswith("@@"):
|
420
|
+
pline = patch_lines[i]
|
421
|
+
if pline.startswith(" "):
|
422
|
+
# Context line must match exactly.
|
423
|
+
expected = pline[1:]
|
424
|
+
if current_index >= len(original_lines):
|
425
|
+
print("Context line expected but file ended")
|
426
|
+
return False
|
427
|
+
orig_line = original_lines[current_index].rstrip("\n")
|
428
|
+
if orig_line != expected:
|
429
|
+
print("Context line mismatch. Expected:", expected, "Got:", orig_line)
|
430
|
+
return False
|
431
|
+
new_lines.append(original_lines[current_index])
|
432
|
+
current_index += 1
|
433
|
+
elif pline.startswith("-"):
|
434
|
+
# Removal line: verify and skip from original.
|
435
|
+
expected = pline[1:]
|
436
|
+
if current_index >= len(original_lines):
|
437
|
+
print("Removal line expected but file ended")
|
438
|
+
return False
|
439
|
+
orig_line = original_lines[current_index].rstrip("\n")
|
440
|
+
if orig_line != expected:
|
441
|
+
print("Removal line mismatch. Expected:", expected, "Got:", orig_line)
|
442
|
+
return False
|
443
|
+
current_index += 1
|
444
|
+
elif pline.startswith("+"):
|
445
|
+
# Addition line: add to new_lines.
|
446
|
+
new_lines.append(pline[1:] + "\n")
|
447
|
+
else:
|
448
|
+
print("Unexpected line in hunk:", pline)
|
449
|
+
return False
|
450
|
+
i += 1
|
451
|
+
else:
|
452
|
+
# Skip non-hunk header lines.
|
453
|
+
i += 1
|
454
|
+
|
455
|
+
# Append any remaining lines from the original file.
|
456
|
+
new_lines.extend(original_lines[current_index:])
|
457
|
+
# Ensure parent directories exist before writing the file.
|
458
|
+
file_path.parent.mkdir(parents=True, exist_ok=True)
|
459
|
+
# Write the new content back to the file.
|
460
|
+
file_path.write_text("".join(new_lines), encoding="utf8")
|
353
461
|
return True
|
354
462
|
|
463
|
+
# Parse the diff into per-file patches.
|
464
|
+
file_patches = parse_diff_per_file(diff_text)
|
465
|
+
if not file_patches:
|
466
|
+
print("No file patches found in diff.")
|
467
|
+
return False
|
468
|
+
|
469
|
+
# Record original file hashes.
|
470
|
+
original_hashes = {}
|
471
|
+
for file_path, _ in file_patches:
|
472
|
+
target_file = Path(project_dir) / file_path
|
473
|
+
if target_file.exists():
|
474
|
+
original_hashes[file_path] = file_hash(target_file)
|
475
|
+
else:
|
476
|
+
original_hashes[file_path] = None
|
477
|
+
|
478
|
+
any_change = False
|
479
|
+
# Process each file patch.
|
480
|
+
for file_path, patch in file_patches:
|
481
|
+
target_file = Path(project_dir) / file_path
|
482
|
+
if "+++ /dev/null" in patch:
|
483
|
+
# Deletion patch: delete the file if it exists.
|
484
|
+
if target_file.exists():
|
485
|
+
target_file.unlink()
|
486
|
+
if not target_file.exists():
|
487
|
+
any_change = True
|
488
|
+
else:
|
489
|
+
print(f"Failed to delete file: {target_file}")
|
490
|
+
return False
|
491
|
+
else:
|
492
|
+
# Modification or new file creation.
|
493
|
+
success = apply_patch_to_file(target_file, patch)
|
494
|
+
if not success:
|
495
|
+
print(f"Failed to apply patch to file: {target_file}")
|
496
|
+
return False
|
497
|
+
|
498
|
+
# Verify that at least one file was changed by comparing hashes.
|
499
|
+
for file_path, patch in file_patches:
|
500
|
+
target_file = Path(project_dir) / file_path
|
501
|
+
if "+++ /dev/null" in patch:
|
502
|
+
if not target_file.exists():
|
503
|
+
any_change = True
|
504
|
+
else:
|
505
|
+
print(f"Expected deletion but file still exists: {target_file}")
|
506
|
+
return False
|
507
|
+
else:
|
508
|
+
old_hash = original_hashes.get(file_path)
|
509
|
+
if target_file.exists():
|
510
|
+
new_hash = file_hash(target_file)
|
511
|
+
if old_hash != new_hash:
|
512
|
+
any_change = True
|
513
|
+
else:
|
514
|
+
print(f"No change detected in file: {target_file}")
|
515
|
+
else:
|
516
|
+
print(f"Expected modification or creation but file is missing: {target_file}")
|
517
|
+
return False
|
518
|
+
|
519
|
+
if not any_change:
|
520
|
+
print("Patch applied but no file modifications detected.")
|
521
|
+
return False
|
522
|
+
return True
|
523
|
+
|
524
|
+
|
355
525
|
def parse_arguments():
|
356
526
|
parser = argparse.ArgumentParser(description='Generate and optionally apply git diffs using GPT-4.')
|
357
527
|
parser.add_argument('prompt', type=str, help='Prompt that runs on the codebase.')
|
@@ -396,41 +566,76 @@ def parse_diff_per_file(diff_text):
|
|
396
566
|
Note:
|
397
567
|
Uses 'b/' prefix detection from git diffs to determine target paths
|
398
568
|
"""
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
if
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
569
|
+
header_re = re.compile(r'^(?:diff --git\s+)?(a/[^ ]+)\s+(b/[^ ]+)\s*$', re.MULTILINE)
|
570
|
+
lines = diff_text.splitlines()
|
571
|
+
|
572
|
+
# Check if any header line exists.
|
573
|
+
if not any(header_re.match(line) for line in lines):
|
574
|
+
# Fallback strategy: detect file headers starting with '--- a/' or '-- a/'
|
575
|
+
diffs = []
|
576
|
+
current_lines = []
|
577
|
+
current_file = None
|
578
|
+
deletion_mode = False
|
579
|
+
header_line_re = re.compile(r'^-{2,3}\s+a/(.+)$')
|
580
|
+
|
581
|
+
for line in lines:
|
582
|
+
if header_line_re.match(line):
|
583
|
+
if current_file is not None and current_lines:
|
584
|
+
if deletion_mode and not any(l.startswith("+++ ") for l in current_lines):
|
585
|
+
current_lines.append("+++ /dev/null")
|
586
|
+
diffs.append((current_file, "\n".join(current_lines)))
|
587
|
+
current_lines = [line]
|
588
|
+
deletion_mode = False
|
589
|
+
file_from = header_line_re.match(line).group(1).strip()
|
590
|
+
current_file = file_from
|
591
|
+
else:
|
592
|
+
current_lines.append(line)
|
593
|
+
if "deleted file mode" in line:
|
594
|
+
deletion_mode = True
|
595
|
+
if line.startswith("+++ "):
|
596
|
+
parts = line.split()
|
597
|
+
if len(parts) >= 2:
|
598
|
+
file_to = parts[1].strip()
|
599
|
+
if file_to != "/dev/null":
|
600
|
+
current_file = file_to[2:] if (file_to.startswith("a/") or file_to.startswith("b/")) else file_to
|
601
|
+
if current_file is not None and current_lines:
|
602
|
+
if deletion_mode and not any(l.startswith("+++ ") for l in current_lines):
|
603
|
+
current_lines.append("+++ /dev/null")
|
604
|
+
diffs.append((current_file, "\n".join(current_lines)))
|
605
|
+
return diffs
|
606
|
+
else:
|
607
|
+
# Use header-based strategy.
|
608
|
+
diffs = []
|
609
|
+
current_lines = []
|
610
|
+
current_file = None
|
611
|
+
deletion_mode = False
|
612
|
+
for line in lines:
|
613
|
+
m = header_re.match(line)
|
614
|
+
if m:
|
615
|
+
if current_file is not None and current_lines:
|
616
|
+
if deletion_mode and not any(l.startswith("+++ ") for l in current_lines):
|
617
|
+
current_lines.append("+++ /dev/null")
|
618
|
+
diffs.append((current_file, "\n".join(current_lines)))
|
619
|
+
current_lines = [line]
|
620
|
+
deletion_mode = False
|
621
|
+
file_from = m.group(1) # e.g. "a/index.html"
|
622
|
+
file_to = m.group(2) # e.g. "b/index.html"
|
623
|
+
current_file = file_to[2:] if file_to.startswith("b/") else file_to
|
624
|
+
else:
|
625
|
+
current_lines.append(line)
|
626
|
+
if "deleted file mode" in line:
|
627
|
+
deletion_mode = True
|
628
|
+
if line.startswith("+++ "):
|
629
|
+
parts = line.split()
|
630
|
+
if len(parts) >= 2:
|
631
|
+
file_to = parts[1].strip()
|
632
|
+
if file_to != "/dev/null":
|
633
|
+
current_file = file_to[2:] if (file_to.startswith("a/") or file_to.startswith("b/")) else file_to
|
634
|
+
if current_file is not None and current_lines:
|
635
|
+
if deletion_mode and not any(l.startswith("+++ ") for l in current_lines):
|
636
|
+
current_lines.append("+++ /dev/null")
|
637
|
+
diffs.append((current_file, "\n".join(current_lines)))
|
638
|
+
return diffs
|
434
639
|
|
435
640
|
def call_llm_for_apply_with_think_tool_available(file_path, original_content, file_diff, model, api_key=None, base_url=None, extra_prompt=None, max_tokens=30000):
|
436
641
|
parser = FlatXMLParser("think")
|
@@ -483,14 +688,12 @@ def call_llm_for_apply(file_path, original_content, file_diff, model, api_key=No
|
|
483
688
|
... )
|
484
689
|
>>> print(updated)
|
485
690
|
def new(): pass"""
|
486
|
-
|
487
691
|
system_prompt = """Please apply the diff to this file. Return the result in a block. Write the entire file.
|
488
692
|
|
489
693
|
1. Carefully apply all changes from the diff
|
490
694
|
2. Preserve surrounding context that isn't changed
|
491
695
|
3. Only return the final file content, do not add any additional markup and do not add a code block
|
492
696
|
4. You must return the entire file. It overwrites the existing file."""
|
493
|
-
|
494
697
|
user_prompt = f"""File: {file_path}
|
495
698
|
File contents:
|
496
699
|
<filecontents>
|
@@ -501,7 +704,6 @@ Diff to apply:
|
|
501
704
|
<diff>
|
502
705
|
{file_diff}
|
503
706
|
</diff>"""
|
504
|
-
|
505
707
|
if extra_prompt:
|
506
708
|
user_prompt += f"\n\n{extra_prompt}"
|
507
709
|
if model == "gemini-2.0-flash-thinking-exp-01-21":
|
@@ -510,7 +712,6 @@ Diff to apply:
|
|
510
712
|
{"role": "system", "content": system_prompt},
|
511
713
|
{"role": "user", "content": user_prompt},
|
512
714
|
]
|
513
|
-
|
514
715
|
if api_key is None:
|
515
716
|
api_key = os.getenv('GPTDIFF_LLM_API_KEY')
|
516
717
|
if base_url is None:
|
@@ -522,7 +723,6 @@ Diff to apply:
|
|
522
723
|
temperature=0.0,
|
523
724
|
max_tokens=max_tokens)
|
524
725
|
full_response = response.choices[0].message.content
|
525
|
-
|
526
726
|
elapsed = time.time() - start_time
|
527
727
|
minutes, seconds = divmod(int(elapsed), 60)
|
528
728
|
time_str = f"{minutes}m {seconds}s" if minutes else f"{seconds}s"
|
@@ -599,8 +799,9 @@ def main():
|
|
599
799
|
args.model = os.getenv('GPTDIFF_MODEL', 'deepseek-reasoner')
|
600
800
|
|
601
801
|
if not args.call and not args.apply:
|
802
|
+
append = "\nInstead of using <diff> tags, use ```diff backticks."
|
602
803
|
with open('prompt.txt', 'w') as f:
|
603
|
-
f.write(full_prompt)
|
804
|
+
f.write(full_prompt+append)
|
604
805
|
print(f"Total tokens: {token_count:5d}")
|
605
806
|
print(f"\033[1;32mNot calling GPT-4.\033[0m") # Green color for success message
|
606
807
|
print('Instead, wrote full prompt to prompt.txt. Use `xclip -selection clipboard < prompt.txt` then paste into chatgpt')
|
@@ -643,7 +844,6 @@ def main():
|
|
643
844
|
print("\a") # Terminal bell for completion notification
|
644
845
|
return
|
645
846
|
|
646
|
-
# Output result
|
647
847
|
elif args.apply:
|
648
848
|
print("\nAttempting apply with the following diff:")
|
649
849
|
print("\n<diff>")
|
@@ -725,3 +925,6 @@ def main():
|
|
725
925
|
print(f"Completion tokens: {completion_tokens}")
|
726
926
|
print(f"Total tokens: {total_tokens}")
|
727
927
|
#print(f"Total cost: ${cost:.4f}")
|
928
|
+
|
929
|
+
if __name__ == "__main__":
|
930
|
+
main()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: gptdiff
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.11
|
4
4
|
Summary: A tool to generate and apply git diffs using LLMs
|
5
5
|
Author: 255labs
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
@@ -26,22 +26,61 @@ Dynamic: requires-dist
|
|
26
26
|
Dynamic: summary
|
27
27
|
|
28
28
|
# GPTDiff
|
29
|
+
<!--
|
30
|
+
GPTDiff: Create and apply diffs using AI.
|
31
|
+
This tool leverages natural language instructions to modify project codebases.
|
32
|
+
-->
|
29
33
|
|
30
|
-
🚀 **
|
34
|
+
🚀 **Create and apply diffs with AI**
|
35
|
+
Modify your project using plain English.
|
36
|
+
|
37
|
+
More documentation at [gptdiff.255labs.xyz](gptdiff.255labs.xyz)
|
38
|
+
|
39
|
+
### Example Usage of `gptdiff`
|
40
|
+
|
41
|
+
#### Apply a Patch Directly
|
42
|
+
```
|
43
|
+
bash
|
44
|
+
gptdiff "Add button animations on press" --apply
|
45
|
+
```
|
46
|
+
✅ Successfully applied patch
|
47
|
+
|
48
|
+
#### Generate a Patch File
|
49
|
+
```
|
50
|
+
bash
|
51
|
+
gptdiff "Add API documentation" --call
|
52
|
+
```
|
53
|
+
🔧 Patch written to `diff.patch`
|
54
|
+
|
55
|
+
#### Generate a Prompt File Without Calling LLM
|
56
|
+
```
|
57
|
+
bash
|
58
|
+
gptdiff "Improve error messages"
|
59
|
+
```
|
60
|
+
📄 LLM not called, written to `prompt.txt`
|
61
|
+
|
62
|
+
---
|
63
|
+
|
64
|
+
### Basic Usage
|
31
65
|
|
32
66
|
```bash
|
33
67
|
cd myproject
|
34
68
|
gptdiff 'add hover effects to the buttons'
|
35
69
|
```
|
36
70
|
|
37
|
-
Generates a prompt.txt file
|
71
|
+
Generates a prompt.txt file containing the full request.
|
72
|
+
Copy and paste its content into your preferred LLM (e.g., ChatGPT) for further experimentation.
|
38
73
|
|
39
|
-
|
74
|
+
### Simple command line agent loops
|
40
75
|
|
41
76
|
```bash
|
42
|
-
|
77
|
+
while
|
78
|
+
do
|
79
|
+
gptdiff "Add missing test cases" --apply
|
80
|
+
done
|
43
81
|
```
|
44
|
-
|
82
|
+
|
83
|
+
*Requires reasoning model*
|
45
84
|
|
46
85
|
### Why GPTDiff?
|
47
86
|
|
@@ -114,7 +153,7 @@ First sign up for an API key at https://nano-gpt.com/api and generate your key.
|
|
114
153
|
export GPTDIFF_LLM_API_KEY='your-api-key'
|
115
154
|
# Optional: For switching API providers
|
116
155
|
export GPTDIFF_MODEL='deepseek-reasoner' # Set default model for all commands
|
117
|
-
export GPTDIFF_LLM_BASE_URL='https://nano-gpt.com/api/v1/
|
156
|
+
export GPTDIFF_LLM_BASE_URL='https://nano-gpt.com/api/v1/'
|
118
157
|
```
|
119
158
|
|
120
159
|
#### Windows
|
@@ -138,7 +177,7 @@ Prevent files being appended to the prompt by adding them to `.gitignore` or `.g
|
|
138
177
|
|
139
178
|
### Command Line Usage
|
140
179
|
|
141
|
-
After installing the package,
|
180
|
+
After installing the package, use the `gptdiff` command in your terminal. Change directory into your codebase and run:
|
142
181
|
|
143
182
|
```bash
|
144
183
|
gptdiff '<user_prompt>'
|
@@ -148,13 +187,7 @@ any files that are included in .gitignore are ignored when generating prompt.txt
|
|
148
187
|
|
149
188
|
#### Specifying Additional Files
|
150
189
|
|
151
|
-
You
|
152
|
-
|
153
|
-
Example usage:
|
154
|
-
|
155
|
-
```bash
|
156
|
-
gptdiff 'make this change' src test
|
157
|
-
```
|
190
|
+
You may supply extra files or directories as arguments to the `gptdiff` command. If omitted, the tool defaults to the current working directory, excluding those matching ignore rules.
|
158
191
|
|
159
192
|
#### Autopatch Changes
|
160
193
|
|
@@ -169,24 +202,20 @@ Preview changes without applying them by omitting the `--apply` flag when using
|
|
169
202
|
```bash
|
170
203
|
gptdiff "Modernize database queries" --call
|
171
204
|
```
|
172
|
-
<span style="color: #0066cc;"
|
205
|
+
<span style="color: #0066cc;">i️ Diff preview generated - review changes before applying</span>
|
173
206
|
|
174
207
|
This often generates incorrect diffs that need to be manually merged.
|
175
208
|
|
176
209
|
#### Smart Apply
|
177
210
|
|
178
|
-
For
|
211
|
+
For robust handling of complex changes, use `smartapply`. It processes each file’s diff individually via the LLM, ensuring nuanced conflict resolution.
|
179
212
|
|
180
|
-
|
181
|
-
gptdiff 'refactor authentication system' --apply
|
182
|
-
```
|
183
|
-
|
184
|
-
### Completion Notification
|
213
|
+
## Completion Notification
|
185
214
|
|
186
215
|
Use the `--nobeep` option to disable the default completion beep:
|
187
216
|
|
188
217
|
```bash
|
189
|
-
gptdiff '<user_prompt>' --
|
218
|
+
gptdiff '<user_prompt>' --nobeep
|
190
219
|
```
|
191
220
|
|
192
221
|
## Local API Documentation
|
@@ -209,29 +238,25 @@ import os
|
|
209
238
|
|
210
239
|
os.environ['GPTDIFF_LLM_API_KEY'] = 'your-api-key'
|
211
240
|
|
212
|
-
# Create
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
241
|
+
# Create files dictionary
|
242
|
+
files = {"main.py": "def old_name():\n print('Need renaming')"}
|
243
|
+
|
244
|
+
# Generate transformation diff using an environment string built from the files dictionary
|
245
|
+
environment = ""
|
246
|
+
for path, content in files.items():
|
247
|
+
environment += f"File: {path}\nContent:\n{content}\n"
|
219
248
|
|
220
|
-
# Generate transformation diff
|
221
249
|
diff = generate_diff(
|
222
250
|
environment=environment,
|
223
251
|
goal='Rename function to new_name()',
|
224
252
|
model='deepseek-reasoner'
|
225
253
|
)
|
226
254
|
|
227
|
-
# Apply changes safely
|
228
|
-
|
229
|
-
diff_text=diff,
|
230
|
-
environment_str=environment
|
231
|
-
)
|
255
|
+
# Apply changes safely using the files dict
|
256
|
+
updated_files = smartapply(diff, files)
|
232
257
|
|
233
258
|
print("Transformed codebase:")
|
234
|
-
print(
|
259
|
+
print(updated_files["main.py"])
|
235
260
|
```
|
236
261
|
|
237
262
|
**Batch Processing Example:**
|
@@ -0,0 +1,8 @@
|
|
1
|
+
gptdiff/__init__.py,sha256=yGjgwv7tNvH1ZLPsQyoo1CxpTOl1iCAwwDBp-_17ksQ,89
|
2
|
+
gptdiff/gptdiff.py,sha256=HbnFkP1o5jQ-WIC99y-et_X6BeBsArtKqiFb2FG6X28,36694
|
3
|
+
gptdiff-0.1.11.dist-info/LICENSE.txt,sha256=zCJk7yUYpMjFvlipi1dKtaljF8WdZ2NASndBYYbU8BY,1228
|
4
|
+
gptdiff-0.1.11.dist-info/METADATA,sha256=pCTb2Mu4w0Y6HSAY5A8F8jwrJrLfq_SQw80PyQvRIXA,7799
|
5
|
+
gptdiff-0.1.11.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
6
|
+
gptdiff-0.1.11.dist-info/entry_points.txt,sha256=0yvXYEVAZFI-p32kQ4-h3qKVWS0a86jsM9FAwF89t9w,49
|
7
|
+
gptdiff-0.1.11.dist-info/top_level.txt,sha256=XNkQkQGINaDndEwRxg8qToOrJ9coyfAb-EHrSUXzdCE,8
|
8
|
+
gptdiff-0.1.11.dist-info/RECORD,,
|
gptdiff-0.1.9.dist-info/RECORD
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
gptdiff/__init__.py,sha256=yGjgwv7tNvH1ZLPsQyoo1CxpTOl1iCAwwDBp-_17ksQ,89
|
2
|
-
gptdiff/gptdiff.py,sha256=k4-iqho7h6NV0hxfpiSVpT3wyG3liBr8Ut_5V5-hjyk,27595
|
3
|
-
gptdiff-0.1.9.dist-info/LICENSE.txt,sha256=zCJk7yUYpMjFvlipi1dKtaljF8WdZ2NASndBYYbU8BY,1228
|
4
|
-
gptdiff-0.1.9.dist-info/METADATA,sha256=jG17S0CxskLxN2HyTXKYOH1Az7i5iafyqe4PcbCj9Ok,7317
|
5
|
-
gptdiff-0.1.9.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
6
|
-
gptdiff-0.1.9.dist-info/entry_points.txt,sha256=0yvXYEVAZFI-p32kQ4-h3qKVWS0a86jsM9FAwF89t9w,49
|
7
|
-
gptdiff-0.1.9.dist-info/top_level.txt,sha256=XNkQkQGINaDndEwRxg8qToOrJ9coyfAb-EHrSUXzdCE,8
|
8
|
-
gptdiff-0.1.9.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|