strop-git 0.27.0

strop git: libgit2 hunks vs live buffers, log/blame/permalinks
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Git memory (M3, 0001 pillar 3.2/3.3): log graph, blame, changed-file
//! stats. Reads run through [`GitExec`] — local `git` or bounded remote
//! `git` over the shared execution boundary (0036 RW8) — with one argv
//! builder and one structured parser per query, so both backends admit
//! exactly the same machine formats. Permalinks and remote
//! normalization live in `permalink`/`ssh` (0033 finding 1).

use std::ffi::OsString;
use std::path::{Path, PathBuf};

use strop_core::worker::CancelToken;

use crate::exec::GitExec;

/// One log line from `git log --graph`, with the commit hash extracted.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LogRow {
    /// The rendered graph+summary line (what the buffer shows).
    pub text: String,
    /// Full SHA when the line names a commit (graph-only lines: None).
    pub sha: Option<String>,
}

/// `git log --graph` for the browser. One bounded run — the log is not
/// a per-keystroke path (0001 §3). Caller decides threading.
pub fn log_graph(
    exec: &GitExec,
    cancel: &CancelToken,
    max: usize,
    file: Option<&Path>,
) -> Result<Vec<LogRow>, String> {
    log_graph_range(exec, cancel, max, file, None)
}

/// `git log -L start,end:path` — the history of a line range (0014 wave
/// 4: selection archaeology). The graph flag is meaningless with -L;
/// rows come straight from the patch headers.
pub fn log_graph_range(
    exec: &GitExec,
    cancel: &CancelToken,
    max: usize,
    file: Option<&Path>,
    range: Option<(usize, usize)>,
) -> Result<Vec<LogRow>, String> {
    let (marker_fmt, ranged) = match range {
        Some(_) => ("%x01%h %an · %ar · %s%x00%H", true),
        None => ("%h %an · %ar · %s%x00%H", false),
    };
    // file operands pass as OsStr: a non-UTF8 tracked filename must
    // reach git byte-for-byte, not via a lossy display() rendering
    let mut argv: Vec<OsString> = vec![
        "log".into(),
        format!("--format={marker_fmt}").into(),
        "-n".into(),
        max.to_string().into(),
    ];
    match (file, range) {
        (Some(f), Some((a, b))) => {
            // -L embeds the path in one argument; compose the OsString
            // instead of formatting through display()
            let mut spec = OsString::from(format!("-L{a},{b}:"));
            spec.push(f);
            argv.push(spec);
        }
        (Some(f), None) => {
            argv.push("--graph".into());
            argv.push("--".into());
            argv.push(f.as_os_str().into());
        }
        (None, None) => argv.push("--graph".into()),
        (None, Some(_)) => return Err("-L needs a file".into()),
    }
    let stdout = exec.run_records("git log", &argv, cancel)?;
    let text = String::from_utf8_lossy(&stdout);
    Ok(text
        .lines()
        // -L output carries patch text; only marked lines are commits
        .filter(|line| !ranged || line.starts_with('\x01'))
        .map(|line| {
            let line = line.strip_prefix('\x01').unwrap_or(line);
            // the format hides the full SHA after a NUL
            let (vis, sha) = match line.split_once('\0') {
                Some((v, s)) => (v.to_string(), Some(s.trim().to_string())),
                None => (line.to_string(), None),
            };
            LogRow { text: vis, sha }
        })
        .collect())
}

/// A blame card for one line (0001 pillar 3.3).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BlameCard {
    pub sha: String,
    pub short_sha: String,
    pub author: String,
    pub age: String,
    pub summary: String,
    pub line: usize,
}

/// Blame one line of a file (1-based). Porcelain format.
pub fn blame_line(
    exec: &GitExec,
    cancel: &CancelToken,
    rel: &Path,
    line: usize,
) -> Result<BlameCard, String> {
    let argv: Vec<OsString> = vec![
        "blame".into(),
        "--line-porcelain".into(),
        "-L".into(),
        format!("{line},{line}").into(),
        "--".into(),
        rel.as_os_str().into(),
    ];
    let stdout = exec.run_records("git blame", &argv, cancel)?;
    let text = String::from_utf8_lossy(&stdout);
    let mut sha = String::new();
    let mut author = String::new();
    let mut summary = String::new();
    let mut ts = 0i64;
    for l in text.lines() {
        if sha.is_empty()
            && !l.starts_with('\t')
            && l.chars().take(8).all(|c| c.is_ascii_hexdigit())
        {
            sha = l.split_whitespace().next().unwrap_or("").to_string();
        } else if let Some(a) = l.strip_prefix("author ") {
            author = a.to_string();
        } else if let Some(t) = l.strip_prefix("author-time ") {
            ts = t.parse().unwrap_or(0);
        } else if let Some(s) = l.strip_prefix("summary ") {
            summary = s.to_string();
        }
    }
    if sha.is_empty() {
        return Err("no blame for line".into());
    }
    Ok(BlameCard {
        short_sha: sha.chars().take(8).collect(),
        sha,
        author,
        age: rel_age(ts),
        summary,
        line,
    })
}

/// One line of a whole-file blame (0001 pillar 3.3, the toggleable
/// column). `age` is rendered at parse time; `ts` keeps "recent"
/// honest for the caller's coloring.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BlameLine {
    pub sha: String,
    pub author: String,
    /// Human short form ("3h", "2d", "5mo"); "now" when uncommitted.
    pub age: String,
    /// Author time, unix seconds (0 = uncommitted).
    pub ts: i64,
}

impl BlameLine {
    /// Worktree lines git blame attributes to nobody (all-zero sha).
    pub fn is_uncommitted(&self) -> bool {
        !self.sha.is_empty() && self.sha.chars().all(|c| c == '0')
    }
}

/// Blame every line of a file (`--line-porcelain`; the gutter's data,
/// 0011 §3). One bounded run on a job thread — never the input path.
pub fn blame_file(
    exec: &GitExec,
    cancel: &CancelToken,
    rel: &Path,
) -> Result<Vec<BlameLine>, String> {
    let argv: Vec<OsString> = vec![
        "blame".into(),
        "--line-porcelain".into(),
        "--".into(),
        rel.as_os_str().into(),
    ];
    let stdout = exec.run_records("git blame", &argv, cancel)?;
    let mut lines = Vec::new();
    let mut sha = String::new();
    let mut author = String::new();
    let mut ts = 0i64;
    for l in String::from_utf8_lossy(&stdout).lines() {
        if let Some(content) = l.strip_prefix('\t') {
            // the record's content row closes it — porcelain repeats
            // the full header per line, so every tab row emits one
            let _ = content;
            if !sha.is_empty() {
                let uncommitted = sha.chars().all(|c| c == '0');
                lines.push(BlameLine {
                    sha: sha.clone(),
                    age: if uncommitted {
                        "now".into()
                    } else {
                        rel_age(ts)
                    },
                    author: if uncommitted {
                        "you".into()
                    } else {
                        author.clone()
                    },
                    ts: if uncommitted { 0 } else { ts },
                });
            }
            sha.clear();
            author.clear();
            ts = 0;
        } else if sha.is_empty()
            && !l.is_empty()
            && l.chars().take(40).all(|c| c.is_ascii_hexdigit())
        {
            sha = l.split_whitespace().next().unwrap_or("").to_string();
        } else if let Some(a) = l.strip_prefix("author ") {
            author = a.to_string();
        } else if let Some(t) = l.strip_prefix("author-time ") {
            ts = t.parse().unwrap_or(0);
        }
    }
    if lines.is_empty() {
        return Err("no blame for file".into());
    }
    Ok(lines)
}

/// Files changed by a commit: `path | +N -M` rows for the dive view.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChangedFile {
    #[serde(with = "strop_core::path_serde")]
    pub path: PathBuf,
    pub added: usize,
    pub deleted: usize,
}

/// Files changed by a commit: `path | +N -M` rows for the dive view.
/// Paths come from numstat's NUL-delimited machine form, so native —
/// never C-quoted, possibly non-UTF8 — names arrive as the worktree
/// identities `commit_file_diff` expects.
pub fn show_stat(
    exec: &GitExec,
    cancel: &CancelToken,
    sha: &str,
) -> Result<Vec<ChangedFile>, String> {
    let argv: Vec<OsString> = vec![
        "show".into(),
        "--numstat".into(),
        "-z".into(),
        "--format=".into(),
        sha.into(),
    ];
    let stdout = exec.run_records("git show", &argv, cancel)?;
    crate::numstat::parse_numstat(&stdout)
}

/// Relative age, human short form ("3h", "2d", "5mo").
fn rel_age(ts: i64) -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);
    let age = (now - ts).max(0);
    match age {
        a if a < 3600 => format!("{}m", a / 60),
        a if a < 86400 => format!("{}h", a / 3600),
        a if a < 86400 * 30 => format!("{}d", a / 86400),
        a if a < 86400 * 365 => format!("{}mo", a / (86400 * 30)),
        a => format!("{}y", a / (86400 * 365)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::exec::with_token;
    use crate::Repo;

    /// The local backend for a repo root — what every local memory
    /// call site constructs.
    fn local(root: &Path) -> GitExec<'_> {
        GitExec::Local { workdir: root }
    }

    /// Repo with two commits (f.rs grows a line), then a dirty edit —
    /// blame_file must attribute committed lines and flag dirty ones.
    #[test]
    fn blame_file_attributes_lines() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(root)
                .output()
                .unwrap();
        };
        git(&["init", "-q"]);
        git(&["config", "user.email", "t@t.t"]);
        git(&["config", "user.name", "t"]);
        std::fs::write(root.join("f.rs"), "one\n").unwrap();
        git(&["add", "."]);
        git(&["commit", "-qm", "first"]);
        std::fs::write(root.join("f.rs"), "one\ntwo\n").unwrap();
        git(&["commit", "-qam", "second"]);

        let clean =
            with_token(|token| blame_file(&local(root), &token, Path::new("f.rs"))).unwrap();
        assert_eq!(clean.len(), 2, "one BlameLine per file line");
        assert_eq!(clean[0].author, "t");
        assert_eq!(clean[1].author, "t");
        assert_ne!(clean[0].sha, clean[1].sha, "two commits, two shas");
        assert!(!clean[0].is_uncommitted());

        // dirty worktree: the new line belongs to nobody
        std::fs::write(root.join("f.rs"), "one\ntwo\nthree\n").unwrap();
        let dirty =
            with_token(|token| blame_file(&local(root), &token, Path::new("f.rs"))).unwrap();
        assert_eq!(dirty.len(), 3);
        assert!(dirty[2].is_uncommitted(), "last line is uncommitted");
        assert_eq!(dirty[2].age, "now");
        assert_eq!(dirty[2].author, "you");
        assert_eq!(dirty[2].ts, 0);
    }

    #[test]
    fn blame_file_rejects_missing_file() {
        let dir = tempfile::tempdir().unwrap();
        assert!(
            with_token(|token| blame_file(&local(dir.path()), &token, Path::new("nope.rs")))
                .is_err()
        );
    }

    /// Hermetic git: no reads of the real HOME or system/global config,
    /// no network, no sleeps. Returns trimmed stdout for rev-parse.
    fn git_here(root: &Path, args: &[&str]) -> String {
        let out = std::process::Command::new("git")
            .args(args)
            .current_dir(root)
            .env("HOME", root)
            .env("XDG_CONFIG_HOME", root.join(".xdg"))
            .env("GIT_CONFIG_NOSYSTEM", "1")
            .env("GIT_CONFIG_GLOBAL", "/dev/null")
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr).trim()
        );
        String::from_utf8_lossy(&out.stdout).trim().to_string()
    }

    /// The review-repo bug: `src/日本語.rs` reached ChangedFiles as a
    /// C-quoted octal escape and renames as `old => new`, neither a
    /// worktree identity. Under `-z` the native names must come back —
    /// ordinary, Unicode, rename (destination only) and binary rows all
    /// present.
    #[test]
    fn show_stat_keeps_native_paths() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        git_here(root, &["init", "-q"]);
        git_here(root, &["config", "user.email", "t@t.t"]);
        git_here(root, &["config", "user.name", "t"]);
        std::fs::create_dir(root.join("src")).unwrap();
        std::fs::write(root.join("a.rs"), "one\n").unwrap();
        std::fs::write(root.join("src/日本語.rs"), "fn x() {}\n").unwrap();
        std::fs::write(root.join("ren.txt"), "old\n").unwrap();
        std::fs::write(root.join("bin.dat"), b"\0\x01binary\0").unwrap();
        git_here(root, &["add", "."]);
        git_here(root, &["commit", "-qm", "first"]);
        git_here(root, &["mv", "ren.txt", "new.txt"]);
        std::fs::write(root.join("a.rs"), "one\ntwo\nthree\n").unwrap();
        std::fs::write(root.join("src/日本語.rs"), "fn x() {}\nfn y() {}\n").unwrap();
        std::fs::write(root.join("bin.dat"), b"\0\x01changed\0").unwrap();
        git_here(root, &["add", "."]);
        git_here(root, &["commit", "-qm", "second"]);
        let sha = git_here(root, &["rev-parse", "HEAD"]);

        let files = with_token(|token| show_stat(&local(root), &token, &sha)).unwrap();
        assert_eq!(files.len(), 4, "{files:?}");
        let row = |p: &str| {
            files
                .iter()
                .find(|f| f.path == Path::new(p))
                .unwrap_or_else(|| panic!("missing {p} in {files:?}"))
        };
        assert_eq!(row("a.rs").added, 2);
        assert_eq!(row("a.rs").deleted, 0);
        // the Unicode identity arrives native, never `"src/\346..."`
        assert_eq!(row("src/日本語.rs").added, 1);
        // rename: only the destination is a row
        assert_eq!(row("new.txt").added, 0);
        assert!(!files.iter().any(|f| f.path == Path::new("ren.txt")));
        // binary: the row survives with deliberate 0/0 counts
        assert_eq!((row("bin.dat").added, row("bin.dat").deleted), (0, 0));
        assert!(files
            .iter()
            .all(|f| !f.path.to_string_lossy().starts_with('"')));
    }

    /// show_stat's paths are real identities: hand one straight to the
    /// git2-based diff the dive opens next.
    #[test]
    fn show_stat_paths_feed_commit_file_diff() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        git_here(root, &["init", "-q"]);
        git_here(root, &["config", "user.email", "t@t.t"]);
        git_here(root, &["config", "user.name", "t"]);
        std::fs::create_dir(root.join("src")).unwrap();
        std::fs::write(root.join("src/日本語.rs"), "fn x() {}\n").unwrap();
        git_here(root, &["add", "."]);
        git_here(root, &["commit", "-qm", "first"]);
        std::fs::write(
            root.join("src/日本語.rs"),
            "fn x() {}\nfn y() {}\nfn z() {}\n",
        )
        .unwrap();
        git_here(root, &["commit", "-qam", "second"]);
        let sha = git_here(root, &["rev-parse", "HEAD"]);

        let files = with_token(|token| show_stat(&local(root), &token, &sha)).unwrap();
        let uni = files
            .iter()
            .find(|f| f.path == Path::new("src/日本語.rs"))
            .expect("native unicode path is a row");
        let repo = Repo::discover(root).unwrap();
        let diff = repo.commit_file_diff(&sha, &uni.path).unwrap();
        assert_eq!(diff.added, 2);
        assert_eq!(diff.deleted, 0);
    }

    /// Unix filenames may be non-UTF8; they must arrive byte-for-byte,
    /// not through a quoted or lossy spelling.
    #[cfg(unix)]
    #[test]
    fn show_stat_preserves_non_utf8_paths() {
        use std::os::unix::ffi::OsStrExt;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        git_here(root, &["init", "-q"]);
        git_here(root, &["config", "user.email", "t@t.t"]);
        git_here(root, &["config", "user.name", "t"]);
        std::fs::create_dir(root.join("src")).unwrap();
        let name = std::ffi::OsStr::from_bytes(b"src/\xff\xfe.rs");
        std::fs::write(root.join(name), "fn x() {}\n").unwrap();
        git_here(root, &["add", "."]);
        git_here(root, &["commit", "-qm", "first"]);
        let sha = git_here(root, &["rev-parse", "HEAD"]);

        let files = with_token(|token| show_stat(&local(root), &token, &sha)).unwrap();
        assert_eq!(files.len(), 1, "{files:?}");
        assert_eq!(files[0].path.as_os_str().as_bytes(), b"src/\xff\xfe.rs");
        assert_eq!(files[0].added, 1);
    }

    /// The log query runs through the same two-backend seam: local
    /// log_graph_range still returns marked rows with full shas.
    #[test]
    fn log_graph_returns_marked_rows() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        git_here(root, &["init", "-q"]);
        git_here(root, &["config", "user.email", "t@t.t"]);
        git_here(root, &["config", "user.name", "t"]);
        std::fs::write(root.join("f.rs"), "one\n").unwrap();
        git_here(root, &["add", "."]);
        git_here(root, &["commit", "-qm", "only"]);
        let sha = git_here(root, &["rev-parse", "HEAD"]);

        let rows = with_token(|token| log_graph(&local(root), &token, 10, None)).unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].sha.as_deref(), Some(sha.as_str()));
        assert!(rows[0].text.contains("only"));

        // a bad revision is an honest error through the same seam
        assert!(with_token(|token| {
            log_graph_range(&local(root), &token, 10, None, Some((1, 1)))
        })
        .is_err());
    }
}