dandavison / dandavison/delta

🐛 "\ No newline at end of file" is not correctly handled

Open
#428 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
32.2k
Forks
572
Avg merge
11m
Merged PRs (30d)
1

Description

When the file does not end in a new line, diff emits a \ No newline at end of file line. Currently delta would just treat it similar to a normal unchanged line, but that causes problems when the diff section extends all the way to file end:

$ diff -u minus.txt plus.txt
--- minus.txt   2020-12-04 20:47:52.000000000 -0500
+++ plus.txt    2020-12-04 20:48:00.000000000 -0500
@@ -1,5 +1,5 @@
-key_0: value_0
-key_1: value_1
-key_2: value_2
-key_3: value_3
-key_4: value_4
\ No newline at end of file
+key_0: value_1
+key_1: value_2
+key_2: value_3
+key_3: value_4
+key_4: value_5
\ No newline at end of file

In this case, the minus and plus hunks should be matched together, but since the "no newline" message was in between, the minus hunk is marked as deleted:

$ delta -s minus.txt plus.txt --max-line-distance=1

comparing: minus.txt ⟶   plus.txt

│ 1  │key_0: value_0                  │    │
│ 2  │key_1: value_1                  │    │
│ 3  │key_2: value_2                  │    │
│ 4  │key_3: value_3                  │    │
│ 5  │key_4: value_4                  │    │
\ No newline at end of file
│    │                                │ 1  │key_0: value_1
│    │                                │ 2  │key_1: value_2
│    │                                │ 3  │key_2: value_3
│    │                                │ 4  │key_3: value_4
│    │                                │ 5  │key_4: value_5
\ No newline at end of file

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case with diff -u minus.txt plus.txt and delta -s minus.txt plus.txt --max-line-distance=1 using files without trailing newlines. Trace the diff parsing and hunk-matching path; done means the minus and plus hunks remain matched together while preserving the \ No newline at end of file markers.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, rust
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.