lziff 0.1.4

A cross-platform TUI diff & review tool with JetBrains-style snap-aligned side-by-side diffs
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
//! Diff sources: where the left/right text comes from.
//!
//! `DiffSource` is the abstraction that the future plugin protocol will speak
//! to. We ship two in-process implementations:
//!
//! - [`GitSource`] — git-backed, with several modes (working tree vs index,
//!   index vs HEAD, a single commit vs its parent, two arbitrary refs).
//! - [`FilePair`] — ad-hoc comparison of two arbitrary files.

use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::UNIX_EPOCH;

#[derive(Debug, Clone)]
pub struct Entry {
    pub id: String,
    pub display: String,
    pub status: String,
}

pub struct DiffPayload {
    pub left_label: String,
    pub right_label: String,
    pub left: String,
    pub right: String,
}

pub trait DiffSource {
    fn list(&self) -> Result<Vec<Entry>>;
    fn load(&self, id: &str) -> Result<DiffPayload>;
    /// Cheap fingerprint used to detect external changes between ticks.
    /// Implementations should return a different value whenever the data
    /// returned by `list()` or `load(current_id)` could have changed.
    fn signature(&self, current_id: Option<&str>) -> u64 {
        let _ = current_id;
        0
    }
    /// Whether to render the Files panel. Sources that only ever produce a
    /// single fixed entry (file pair) can hide it for a roomier diff.
    fn show_files_panel(&self) -> bool {
        true
    }
}

fn mtime_ms(p: &Path) -> u64 {
    std::fs::metadata(p)
        .ok()
        .and_then(|m| m.modified().ok())
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

pub struct FilePair {
    pub left: PathBuf,
    pub right: PathBuf,
}

impl DiffSource for FilePair {
    fn list(&self) -> Result<Vec<Entry>> {
        Ok(vec![Entry {
            id: "pair".into(),
            display: format!(
                "{}  ↔  {}",
                self.left.display(),
                self.right.display()
            ),
            status: "  ".into(),
        }])
    }

    fn load(&self, _id: &str) -> Result<DiffPayload> {
        let left = read_file(&self.left)?;
        let right = read_file(&self.right)?;
        Ok(DiffPayload {
            left_label: self.left.display().to_string(),
            right_label: self.right.display().to_string(),
            left,
            right,
        })
    }

    fn signature(&self, _: Option<&str>) -> u64 {
        mtime_ms(&self.left).wrapping_mul(1469598103934665603).wrapping_add(mtime_ms(&self.right))
    }

    fn show_files_panel(&self) -> bool {
        false
    }
}

#[derive(Debug, Clone)]
pub enum GitMode {
    /// `git status` working tree vs (index ∪ HEAD).
    WorkingTree,
    /// `git diff --cached`: index vs HEAD.
    Staged,
    /// What this commit changed: `<rev>~` vs `<rev>`.
    Commit { rev: String },
    /// Diff between two arbitrary refs: `<from>` vs `<to>`.
    Range { from: String, to: String },
}

pub struct GitSource {
    pub root: PathBuf,
    pub mode: GitMode,
}

impl GitSource {
    pub fn working_tree(start: &Path) -> Result<Self> {
        Ok(Self {
            root: discover_root(start)?,
            mode: GitMode::WorkingTree,
        })
    }

    pub fn staged(start: &Path) -> Result<Self> {
        Ok(Self {
            root: discover_root(start)?,
            mode: GitMode::Staged,
        })
    }

    pub fn commit(start: &Path, rev: impl Into<String>) -> Result<Self> {
        Ok(Self {
            root: discover_root(start)?,
            mode: GitMode::Commit { rev: rev.into() },
        })
    }

    pub fn range(start: &Path, from: impl Into<String>, to: impl Into<String>) -> Result<Self> {
        Ok(Self {
            root: discover_root(start)?,
            mode: GitMode::Range {
                from: from.into(),
                to: to.into(),
            },
        })
    }
}

fn discover_root(start: &Path) -> Result<PathBuf> {
    let out = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(start)
        .output()
        .context("failed to invoke git")?;
    if !out.status.success() {
        anyhow::bail!("not inside a git repository");
    }
    let root = String::from_utf8_lossy(&out.stdout).trim().to_string();
    Ok(PathBuf::from(root))
}

impl DiffSource for GitSource {
    fn list(&self) -> Result<Vec<Entry>> {
        match &self.mode {
            GitMode::WorkingTree => list_working_tree(&self.root),
            GitMode::Staged => list_diff_name_status(
                &self.root,
                &["diff", "--cached", "--name-status", "-z", "--no-renames"],
            ),
            GitMode::Commit { rev } => list_diff_name_status(
                &self.root,
                &[
                    "diff",
                    "--name-status",
                    "-z",
                    "--no-renames",
                    &format!("{rev}~"),
                    rev,
                ],
            ),
            GitMode::Range { from, to } => list_diff_name_status(
                &self.root,
                &[
                    "diff",
                    "--name-status",
                    "-z",
                    "--no-renames",
                    from,
                    to,
                ],
            ),
        }
    }

    fn load(&self, id: &str) -> Result<DiffPayload> {
        match &self.mode {
            GitMode::WorkingTree => load_working_tree(&self.root, id),
            GitMode::Staged => Ok(DiffPayload {
                left_label: format!("HEAD:{id}"),
                right_label: format!("index:{id}"),
                left: git_show(&self.root, &format!("HEAD:{id}")).unwrap_or_default(),
                right: git_show(&self.root, &format!(":{id}")).unwrap_or_default(),
            }),
            GitMode::Commit { rev } => {
                let parent = format!("{rev}~");
                Ok(DiffPayload {
                    left_label: format!("{parent}:{id}"),
                    right_label: format!("{rev}:{id}"),
                    left: git_show(&self.root, &format!("{parent}:{id}")).unwrap_or_default(),
                    right: git_show(&self.root, &format!("{rev}:{id}")).unwrap_or_default(),
                })
            }
            GitMode::Range { from, to } => Ok(DiffPayload {
                left_label: format!("{from}:{id}"),
                right_label: format!("{to}:{id}"),
                left: git_show(&self.root, &format!("{from}:{id}")).unwrap_or_default(),
                right: git_show(&self.root, &format!("{to}:{id}")).unwrap_or_default(),
            }),
        }
    }

    fn signature(&self, current_id: Option<&str>) -> u64 {
        match &self.mode {
            GitMode::WorkingTree => {
                let mut h = mtime_ms(&self.root.join(".git").join("index"));
                h = h.wrapping_mul(1469598103934665603);
                h ^= mtime_ms(&self.root.join(".git").join("HEAD")).rotate_left(7);
                if let Some(id) = current_id {
                    h ^= mtime_ms(&self.root.join(id)).rotate_left(17);
                }
                h
            }
            GitMode::Staged => {
                let mut h = mtime_ms(&self.root.join(".git").join("index"));
                h = h.wrapping_mul(1469598103934665603);
                h ^= mtime_ms(&self.root.join(".git").join("HEAD")).rotate_left(7);
                h
            }
            // Commit/Range refer to immutable snapshots — nothing to poll.
            GitMode::Commit { .. } | GitMode::Range { .. } => 0,
        }
    }
}

fn list_working_tree(root: &Path) -> Result<Vec<Entry>> {
    let out = Command::new("git")
        .args(["status", "--porcelain=v1", "-z", "--untracked-files=all"])
        .current_dir(root)
        .output()
        .context("git status failed")?;
    if !out.status.success() {
        anyhow::bail!(
            "git status: {}",
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    // porcelain v1 -z format: XY␠path[\0orig\0]?...
    let mut entries = Vec::new();
    let mut iter = out.stdout.split(|&b| b == 0).peekable();
    while let Some(rec) = iter.next() {
        if rec.is_empty() {
            continue;
        }
        if rec.len() < 4 {
            continue;
        }
        let xy = std::str::from_utf8(&rec[..2]).unwrap_or("??");
        let path = std::str::from_utf8(&rec[3..]).unwrap_or("").to_string();
        if xy.starts_with('R') || xy.starts_with('C') {
            let _ = iter.next();
        }
        entries.push(Entry {
            id: path.clone(),
            display: path,
            status: xy.to_string(),
        });
    }
    Ok(entries)
}

/// Parse `git diff --name-status -z` output into entries.
///
/// With `-z`, git separates the status field and the path with a NUL byte (not
/// a tab as in the non-`-z` form), so each non-rename record occupies two
/// NUL-separated fields: `<status>\0<path>\0`. With `--no-renames` we don't
/// need to worry about the three-field rename form (`R<score>\0<src>\0<dst>\0`).
fn list_diff_name_status(root: &Path, args: &[&str]) -> Result<Vec<Entry>> {
    let out = Command::new("git")
        .args(args)
        .current_dir(root)
        .output()
        .context("git diff failed")?;
    if !out.status.success() {
        anyhow::bail!(
            "git {}: {}",
            args.join(" "),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    let mut entries = Vec::new();
    let mut fields = out.stdout.split(|&b| b == 0).filter(|f| !f.is_empty());
    while let Some(status_field) = fields.next() {
        let status = std::str::from_utf8(status_field).unwrap_or("?").to_string();
        // Rename / copy entries carry an extra src field before the dst path.
        // We're called with `--no-renames`, but be defensive in case a caller
        // omits it.
        let is_rename = status.starts_with('R') || status.starts_with('C');
        let path_field = if is_rename {
            fields.next();
            fields.next()
        } else {
            fields.next()
        };
        let Some(path_bytes) = path_field else {
            break;
        };
        let path = std::str::from_utf8(path_bytes).unwrap_or("").to_string();
        if path.is_empty() {
            continue;
        }
        // Pad status to 2 chars so the renderer's `.chars().next()` check
        // and existing color mapping behave the same as porcelain output.
        let status_padded = if status.chars().count() == 1 {
            format!("{status} ")
        } else {
            status
        };
        entries.push(Entry {
            id: path.clone(),
            display: path,
            status: status_padded,
        });
    }
    Ok(entries)
}

fn load_working_tree(root: &Path, id: &str) -> Result<DiffPayload> {
    let path = root.join(id);
    let untracked = git_is_untracked(root, id)?;

    // Merge-conflict detection: if either the "ours" (stage 2) or "theirs"
    // (stage 3) blob exists in the index, we're in the middle of an
    // unresolved merge. Show the two sides directly so the reviewer sees
    // what each branch wanted, instead of "HEAD vs file-with-markers".
    if !untracked {
        let ours = git_show(root, &format!(":2:{id}")).ok();
        let theirs = git_show(root, &format!(":3:{id}")).ok();
        if ours.is_some() || theirs.is_some() {
            return Ok(DiffPayload {
                left_label: format!("ours:{id}"),
                right_label: format!("theirs:{id}"),
                left: ours.unwrap_or_default(),
                right: theirs.unwrap_or_default(),
            });
        }
    }

    let left = if untracked {
        String::new()
    } else {
        git_show_index_or_head(root, id).unwrap_or_default()
    };
    let right = if path.exists() {
        read_file(&path).unwrap_or_default()
    } else {
        String::new()
    };

    Ok(DiffPayload {
        left_label: if untracked {
            format!("(untracked)  {id}")
        } else {
            format!("HEAD:{id}")
        },
        right_label: format!("worktree:{id}"),
        left,
        right,
    })
}

fn git_is_untracked(root: &Path, path: &str) -> Result<bool> {
    let out = Command::new("git")
        .args(["ls-files", "--error-unmatch", "--", path])
        .current_dir(root)
        .output()?;
    Ok(!out.status.success())
}

fn git_show_index_or_head(root: &Path, path: &str) -> Result<String> {
    let spec_index = format!(":{path}");
    if let Ok(s) = git_show(root, &spec_index) {
        return Ok(s);
    }
    let spec_head = format!("HEAD:{path}");
    git_show(root, &spec_head)
}

fn git_show(root: &Path, spec: &str) -> Result<String> {
    let out = Command::new("git")
        .args(["show", spec])
        .current_dir(root)
        .output()?;
    if !out.status.success() {
        anyhow::bail!("git show {} failed", spec);
    }
    Ok(String::from_utf8_lossy(&out.stdout).to_string())
}

fn read_file(p: &Path) -> Result<String> {
    let bytes = std::fs::read(p).with_context(|| format!("read {}", p.display()))?;
    Ok(String::from_utf8_lossy(&bytes).to_string())
}