Skip to content

Commit 5d4e00f

Browse files
sveneberthclaude
andauthored
fix: Show colorized unified diff in script pull before overwrite prompt (#215)
Replace hash-based comparison with `difflib` to fix a bug where differing line endings (`\n` vs `\r\n`) between local and server caused every script to appear changed. `splitlines()` normalizes line endings before comparing. Additionally, a colored server→local diff is now shown before asking the user whether to overwrite. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d6333e6 commit 5d4e00f

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

src/viur_cli/scriptor/cli.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import os
66
import hashlib
7+
import difflib
78
import asyncio
89
import sys
910
import glob
@@ -162,14 +163,30 @@ def create_file():
162163
create_file()
163164
else:
164165
with open(_path, "r") as f:
165-
if hashlib.sha256(entry["script"].encode()).digest() \
166-
!= hashlib.sha256(f.read().encode()).digest():
167-
try:
168-
if click.confirm(f"There is a difference with {entry['path']}. Overwrite?"):
169-
os.remove(_path)
170-
create_file()
171-
except click.exceptions.Abort:
172-
click.echo("\nSkipping...")
166+
local_content = f.read()
167+
remote_content = entry["script"]
168+
diff = list(difflib.unified_diff(
169+
remote_content.splitlines(),
170+
local_content.splitlines(),
171+
fromfile=f"server/{entry['path'].lstrip('/')}",
172+
tofile=f"local/{entry['path'].lstrip('/')}",
173+
lineterm="",
174+
))
175+
if diff:
176+
for line in diff:
177+
if line.startswith("+++") or line.startswith("---"):
178+
click.echo(click.style(line, bold=True), nl=True)
179+
elif line.startswith("@@"):
180+
click.echo(click.style(line, fg="cyan"), nl=True)
181+
elif line.startswith("+"):
182+
click.echo(click.style(line, fg="green"), nl=True)
183+
elif line.startswith("-"):
184+
click.echo(click.style(line, fg="red"), nl=True)
185+
else:
186+
click.echo(line, nl=True)
187+
if click.confirm(f"Overwrite local {entry['path']} with remote version?"):
188+
os.remove(_path)
189+
create_file()
173190

174191
else:
175192
create_file()

0 commit comments

Comments
 (0)