strop-git 0.3.8

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
//! Git memory (M3, 0001 pillar 3.2/3.3): log graph, blame, permalinks.
//! Reads via shell `git` (matches user config; not hot-path), permalinks
//! via libgit2 config (no spawn).

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

use crate::Repo;

/// One log line from `git log --graph`, with the commit hash extracted.
#[derive(Debug, Clone)]
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. Shells out — the log is not a
/// per-keystroke path (0001 §3). Caller decides threading.
pub fn log_graph(workdir: &Path, max: usize, file: Option<&Path>) -> Result<Vec<LogRow>, String> {
    let mut cmd = std::process::Command::new("git");
    cmd.args([
        "-C",
        &workdir.display().to_string(),
        "log",
        "--graph",
        "--format=%h %an · %ar · %s%x00%H",
        "-n",
        &max.to_string(),
    ]);
    if let Some(f) = file {
        cmd.arg("--").arg(f);
    }
    let out = cmd.output().map_err(|e| format!("spawn git log: {e}"))?;
    if !out.status.success() {
        return Err(String::from_utf8_lossy(&out.stderr).trim().to_string());
    }
    let text = String::from_utf8_lossy(&out.stdout);
    Ok(text
        .lines()
        .map(|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)]
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). Shells out; porcelain format.
pub fn blame_line(workdir: &Path, rel: &Path, line: usize) -> Result<BlameCard, String> {
    let out = std::process::Command::new("git")
        .args([
            "-C",
            &workdir.display().to_string(),
            "blame",
            "--line-porcelain",
            "-L",
            &format!("{line},{line}"),
            "--",
            &rel.display().to_string(),
        ])
        .output()
        .map_err(|e| format!("spawn git blame: {e}"))?;
    if !out.status.success() {
        return Err(String::from_utf8_lossy(&out.stderr).trim().to_string());
    }
    let text = String::from_utf8_lossy(&out.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)]
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). Shells out on a job thread — never the input path.
pub fn blame_file(workdir: &Path, rel: &Path) -> Result<Vec<BlameLine>, String> {
    let out = std::process::Command::new("git")
        .args([
            "-C",
            &workdir.display().to_string(),
            "blame",
            "--line-porcelain",
            "--",
            &rel.display().to_string(),
        ])
        .output()
        .map_err(|e| format!("spawn git blame: {e}"))?;
    if !out.status.success() {
        return Err(String::from_utf8_lossy(&out.stderr).trim().to_string());
    }
    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(&out.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)]
pub struct ChangedFile {
    pub path: PathBuf,
    pub added: usize,
    pub deleted: usize,
}

pub fn show_stat(workdir: &Path, sha: &str) -> Result<Vec<ChangedFile>, String> {
    let out = std::process::Command::new("git")
        .args([
            "-C",
            &workdir.display().to_string(),
            "show",
            "--numstat",
            "--format=",
            sha,
        ])
        .output()
        .map_err(|e| format!("spawn git show: {e}"))?;
    if !out.status.success() {
        return Err(String::from_utf8_lossy(&out.stderr).trim().to_string());
    }
    Ok(String::from_utf8_lossy(&out.stdout)
        .lines()
        .filter_map(|l| {
            let mut parts = l.split('\t');
            let added = parts.next()?.parse().ok()?;
            let deleted = parts.next()?.parse().ok()?;
            Some(ChangedFile {
                path: PathBuf::from(parts.next()?),
                added,
                deleted,
            })
        })
        .collect())
}

// ---- permalinks ----------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Host {
    GitHub,
    GitLab,
    Bitbucket,
    Gitea,
    /// Unknown host: emit whatever HTTPS we can normalize to.
    Other,
}

pub struct Remote {
    pub host: Host,
    pub owner_repo: String, // "org/repo"
    pub base: String,       // "https://github.com"
}

/// Normalize a remote URL (SSH or HTTPS) to a web base. Priority
/// upstream > origin > rest is the caller's job (0001 pillar 3.3).
pub fn normalize_remote(url: &str) -> Option<Remote> {
    let url = url.trim().trim_end_matches(".git");
    let (base, path) = if let Some(rest) = url.strip_prefix("git@") {
        // git@host:org/repo
        let (host, path) = rest.split_once(':')?;
        (format!("https://{host}"), path.to_string())
    } else if let Some(rest) = url.strip_prefix("ssh://git@") {
        // ssh://git@host/org/repo
        let rest = rest.split('/').collect::<Vec<_>>();
        let host = rest.first()?;
        (format!("https://{host}"), rest[1..].join("/"))
    } else if url.starts_with("https://") || url.starts_with("http://") {
        let stripped = url
            .strip_prefix("https://")
            .or_else(|| url.strip_prefix("http://"))?;
        let (host, path) = stripped.split_once('/')?;
        (format!("https://{host}"), path.to_string())
    } else if let Some((host, path)) = url.split_once(':') {
        // scp syntax without user@: bare hostname or an ssh host alias
        // (`bbgithub:org/repo` — ~/.ssh/config supplies the real host)
        if host.contains('@') || host.contains('/') {
            return None;
        }
        let host = resolve_ssh_alias(host).unwrap_or_else(|| host.to_string());
        (format!("https://{host}"), path.to_string())
    } else {
        return None;
    };
    let host = match base.as_str() {
        "https://github.com" => Host::GitHub,
        "https://gitlab.com" => Host::GitLab,
        "https://bitbucket.org" => Host::Bitbucket,
        b if b.contains("gitea") => Host::Gitea,
        _ => Host::Other,
    };
    Some(Remote {
        host,
        owner_repo: path,
        base,
    })
}

/// Resolve an ssh host alias via `~/.ssh/config` Host blocks (exact
/// matches; wildcard blocks skipped). Enterprise GitHub setups live on
/// these — the alias exists so the hostname isn't repeated per clone.
fn resolve_ssh_alias(alias: &str) -> Option<String> {
    let home = std::env::var_os("HOME")?;
    let config = std::fs::read_to_string(PathBuf::from(home).join(".ssh").join("config")).ok()?;
    parse_ssh_alias(&config, alias)
}

fn parse_ssh_alias(config: &str, alias: &str) -> Option<String> {
    let mut in_block = false;
    for line in config.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let mut parts = line.split_whitespace();
        match parts.next().map(|k| k.to_ascii_lowercase()).as_deref() {
            Some("host") => in_block = parts.any(|h| h == alias),
            Some("hostname") if in_block => return parts.next().map(|h| h.to_string()),
            _ => {}
        }
    }
    None
}

/// Pick the permalink remote: upstream > origin > first remaining.
pub fn pick_remote(repo: &Repo) -> Option<Remote> {
    let remotes = repo.remotes();
    for name in ["upstream", "origin"] {
        if let Some(url) = remotes.iter().find(|(n, _)| n == name).map(|(_, u)| u) {
            if let Some(r) = normalize_remote(url) {
                return Some(r);
            }
        }
    }
    remotes.iter().find_map(|(_, u)| normalize_remote(u))
}

/// Build the immutable permalink for a file at 1-based lines. Branch is
/// always resolved to a commit SHA (0001 pillar 3.3).
pub fn permalink(repo: &Repo, rel: &Path, start_line: usize, end_line: usize) -> Option<String> {
    let remote = pick_remote(repo)?;
    let sha = repo.head_sha()?;
    let frag = if start_line == end_line {
        format!("#L{start_line}")
    } else {
        format!("#L{start_line}-L{end_line}")
    };
    Some(format!(
        "{}/{}/blob/{}/{}{frag}",
        remote.base,
        remote.owner_repo,
        sha,
        rel.display()
    ))
}

/// 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::*;

    #[test]
    fn ssh_alias_resolves_via_config() {
        let config = "# comment\nHost bbgithub\n  HostName bbgithub.dev.bloomberg.com\n  User git\nHost *\n  ServerAliveInterval 30\n";
        assert_eq!(
            parse_ssh_alias(config, "bbgithub").as_deref(),
            Some("bbgithub.dev.bloomberg.com")
        );
        assert_eq!(parse_ssh_alias(config, "other"), None);
        // wildcard-only blocks don't claim aliases
        assert_eq!(parse_ssh_alias("Host *\n  HostName x", "bbgithub"), None);
    }

    #[test]
    fn scp_without_user_parses_as_bare_host() {
        // unresolved alias falls back to the bare name (matches what git
        // itself would attempt) — but with a config entry it resolves
        let r = normalize_remote("bbgithub:acme/demo.git");
        assert!(r.is_some(), "alias form parses");
    }

    #[test]
    fn reviewer_table() {
        // the first-week report's remote table, verbatim
        for url in [
            "https://github.com/acme/demo.git",
            "ssh://git@github.com/acme/demo.git",
            "git@github.com:acme/demo",
            "git@bbgithub.dev.bloomberg.com:acme/demo.git",
            "https://bbgithub.dev.bloomberg.com/acme/demo.git",
        ] {
            let r = normalize_remote(url);
            assert!(r.is_some(), "should parse: {url}");
        }
        // the ssh host-alias form parses (bare-host fallback; resolves
        // via ~/.ssh/config when an entry exists)
        assert!(normalize_remote("bbgithub:acme/demo.git").is_some());
    }

    #[test]
    fn normalizes_ssh_and_https() {
        let r = normalize_remote("git@github.com:stropdev/strop.git").unwrap();
        assert_eq!(
            (r.base.as_str(), r.owner_repo.as_str()),
            ("https://github.com", "stropdev/strop")
        );
        assert_eq!(r.host, Host::GitHub);
        let r = normalize_remote("https://gitlab.com/org/proj").unwrap();
        assert_eq!(r.host, Host::GitLab);
        assert_eq!(r.owner_repo, "org/proj");
        let r = normalize_remote("ssh://git@bitbucket.org/team/repo.git").unwrap();
        assert_eq!(r.host, Host::Bitbucket);
        assert!(normalize_remote("not a url").is_none());
    }

    /// 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 = blame_file(root, 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 = blame_file(root, 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!(blame_file(dir.path(), Path::new("nope.rs")).is_err());
    }
}