snyvi 1.20.0

A fast, beautiful viewer for the documents your agents produce
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
//! What git says about a desk's folder: the branch, how much is changed, what
//! is not pushed, the commits of the last few weeks, and where the repository
//! lives on the web. Home reads it for the pick-up card and the log of the
//! days, and the desk's rail for its repo link.
//!
//! Read-only and local. snyvi holds no tokens and calls nothing: this runs
//! `git status` and `git log` in a folder the reader gave a desk, with the same
//! care `project::modified` takes -- no fsmonitor hook runs, no index lock is
//! taken, no window flashes on Windows -- and gives up after `WAIT`. Never in
//! the home directory: a dotfiles repository there would have `git status`
//! walk every file the reader owns, and snyvi does not look through `~`.
//!
//! Home is drawn again on every event it shows, so the answers are kept for
//! `FRESH` per folder. A commit made a moment ago shows within that.

use serde::Serialize;
use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// How long one git command may take before its answer is not worth waiting for.
const WAIT: Duration = Duration::from_secs(2);
/// How long an answer is kept.
const FRESH: Duration = Duration::from_secs(30);
/// How far back the commits go: the eight weeks Home's rhythm shows.
pub const WEEKS: i64 = 8;
/// At most this many commits are read, however busy the weeks were.
const MOST: usize = 400;

#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub struct State {
    /// The branch, or the short commit a detached head is on.
    pub branch: String,
    /// Files changed, staged or not, untracked included.
    pub changed: usize,
    /// Commits on this branch its upstream does not have; `None` when the
    /// branch has no upstream to compare with.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ahead: Option<usize>,
    /// The newest commit on HEAD, whenever it was.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last: Option<Commit>,
    /// The commits on HEAD of the last `WEEKS` weeks, oldest first. Not sent
    /// as they are: Home turns them into the log and the rhythm.
    #[serde(skip)]
    pub commits: Vec<Commit>,
    /// The repository's page on the web, from the `origin` remote (or the
    /// first remote when there is no `origin`): `https://github.com/o/r`.
    /// `None` with no remote, or one that is a path on this machine.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Commit {
    pub at: i64,
    pub hash: String,
    pub subject: String,
}

/// One folder's answer, and when it was read.
type Reading = (Instant, Option<Arc<State>>);

/// The answers kept per folder, and when each was read.
#[derive(Default)]
pub struct Cache(Mutex<HashMap<PathBuf, Reading>>);

impl Cache {
    /// What git says about `root`, from the cache while it is fresh. Blocking:
    /// call it off the async runtime. `None` for a folder that is not in a
    /// repository, is the home directory's, or where git did not answer.
    pub fn read(&self, root: &Path, now: i64) -> Option<Arc<State>> {
        let key = root.to_path_buf();
        if let Some((at, s)) = self.0.lock().unwrap_or_else(|e| e.into_inner()).get(&key) {
            if at.elapsed() < FRESH {
                return s.clone();
            }
        }
        let s = read(root, now).map(Arc::new);
        let mut map = self.0.lock().unwrap_or_else(|e| e.into_inner());
        // Folders a desk no longer has are not kept forever.
        map.retain(|_, (at, _)| at.elapsed() < FRESH * 4);
        map.insert(key, (Instant::now(), s.clone()));
        s
    }
}

/// Read a folder's state now, without the cache.
pub fn read(root: &Path, now: i64) -> Option<State> {
    let top = repository(root)?;
    if dirs::home_dir().is_some_and(|h| same(&h, &top)) {
        return None;
    }
    let status = run(root, &["status", "--porcelain=v2", "--branch"])?;
    let mut s = parse_status(&status);
    let since = now - WEEKS * 7 * 86_400;
    let log = run(
        root,
        &[
            "log",
            "HEAD",
            "--no-merges",
            // More than eight digits is seconds since 1970 to git.
            &format!("--since={since}"),
            &format!("-n{MOST}"),
            "--format=%ct%x09%h%x09%s",
        ],
    )
    .unwrap_or_default();
    s.commits = parse_log(&log);
    s.last = s.commits.last().cloned().or_else(|| {
        run(root, &["log", "-1", "--format=%ct%x09%h%x09%s"]).and_then(|l| parse_log(&l).pop())
    });
    // Read from the config, never asked of the remote: no network. A repo
    // with no remote at all makes `git config` exit 1, which is `None`.
    s.remote = run(root, &["config", "--get-regexp", r"^remote\..*\.url$"])
        .and_then(|out| remote(&out))
        .and_then(|url| web_url(&url));
    Some(s)
}

/// The URL of `origin`, or of the first remote when there is none, out of
/// `git config --get-regexp` lines: `remote.<name>.url <url>`.
fn remote(out: &str) -> Option<String> {
    let urls: Vec<(&str, &str)> = out
        .lines()
        .filter_map(|l| {
            let (key, url) = l.split_once(' ')?;
            let name = key.strip_prefix("remote.")?.strip_suffix(".url")?;
            Some((name, url.trim()))
        })
        .collect();
    urls.iter()
        .find(|(n, _)| *n == "origin")
        .or(urls.first())
        .map(|(_, u)| u.to_string())
}

/// A remote's URL as the repository's web page: `git@github.com:o/r.git`,
/// `ssh://git@host:22/o/r` and `https://user:token@host/o/r.git` are all
/// `https://host/o/r`. What it never keeps is a user, a password or a token,
/// a query or an ssh port. `None` for a path on this machine or anything that
/// does not read as a host and a path.
pub fn web_url(url: &str) -> Option<String> {
    let url = url.trim();
    let (scheme, rest) = match url.split_once("://") {
        Some((scheme, rest)) => (scheme.to_ascii_lowercase(), rest),
        // scp-like, `[user@]host:path`: a colon before any slash. A Windows
        // drive (`C:\...`, `C:/...`) is a one-letter host, and a path.
        None => {
            let colon = url.find(':')?;
            if url[..colon].contains('/') || colon < 2 {
                return None;
            }
            ("ssh".to_string(), url)
        }
    };
    let web = match scheme.as_str() {
        "http" => "http",
        "https" | "ssh" | "git" | "git+ssh" | "ssh+git" => "https",
        _ => return None,
    };
    let rest = rest.split(['?', '#']).next().unwrap_or("");
    let (authority, path) = if url.contains("://") {
        rest.split_once('/')?
    } else {
        rest.split_once(':')?
    };
    // Whatever is before the last `@` is credentials: dropped, never shown.
    let host = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
    // A port is the web's only for http(s); an ssh port says nothing of it.
    let host = match host.rsplit_once(':') {
        Some((h, port))
            if !scheme.starts_with("http") && port.bytes().all(|b| b.is_ascii_digit()) =>
        {
            h
        }
        _ => host,
    };
    let path = path.trim_matches('/');
    let path = path
        .strip_suffix(".git")
        .unwrap_or(path)
        .trim_end_matches('/');
    let ok = |s: &str| !s.is_empty() && !s.contains(char::is_whitespace);
    if !ok(host) || !ok(path) || host.contains(['/', '\\']) {
        return None;
    }
    Some(format!("{web}://{host}/{path}"))
}

/// The top of the repository `dir` is in, found by looking for `.git` on the
/// way up -- no process for a folder that is in none.
fn repository(dir: &Path) -> Option<PathBuf> {
    let dir = dir.canonicalize().ok()?;
    dir.ancestors()
        .find(|d| d.join(".git").exists())
        .map(Path::to_path_buf)
}

fn same(a: &Path, b: &Path) -> bool {
    a.canonicalize().ok().as_deref().unwrap_or(a) == b
}

/// One git command in `dir`, its output when it succeeded within `WAIT`.
fn run(dir: &Path, args: &[&str]) -> Option<String> {
    let mut cmd = Command::new("git");
    cmd.args(["-c", "core.fsmonitor=false", "--no-optional-locks"])
        .args(args)
        .current_dir(dir)
        .env("GIT_TERMINAL_PROMPT", "0")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null());
    #[cfg(windows)]
    {
        use std::os::windows::process::CommandExt;
        cmd.creation_flags(crate::platform::CREATE_NO_WINDOW);
    }
    let mut child = cmd.spawn().ok()?;
    // Read on a thread, so a long answer cannot fill the pipe and stall the
    // child while this one waits on it.
    let mut out = child.stdout.take()?;
    let reader = std::thread::spawn(move || {
        let mut s = String::new();
        let _ = out.read_to_string(&mut s);
        s
    });
    let start = Instant::now();
    let status = loop {
        match child.try_wait() {
            Ok(Some(st)) => break st,
            Ok(None) if start.elapsed() < WAIT => std::thread::sleep(Duration::from_millis(10)),
            _ => {
                let _ = child.kill();
                let _ = child.wait();
                return None;
            }
        }
    };
    let text = reader.join().ok()?;
    status.success().then_some(text)
}

/// `git status --porcelain=v2 --branch`: the headers say the branch and how far
/// ahead it is, and every other line is one changed file.
fn parse_status(out: &str) -> State {
    let mut s = State::default();
    let mut oid = String::new();
    for line in out.lines() {
        if let Some(h) = line.strip_prefix("# branch.head ") {
            s.branch = h.to_string();
        } else if let Some(o) = line.strip_prefix("# branch.oid ") {
            oid = o.chars().take(7).collect();
        } else if let Some(ab) = line.strip_prefix("# branch.ab ") {
            s.ahead = ab
                .split_whitespace()
                .next()
                .and_then(|a| a.strip_prefix('+'))
                .and_then(|a| a.parse().ok());
        } else if !line.starts_with('#') && !line.is_empty() {
            s.changed += 1;
        }
    }
    if s.branch == "(detached)" {
        s.branch = oid;
    }
    s
}

/// `%ct\t%h\t%s` per line, newest first as git gives it; oldest first back.
fn parse_log(out: &str) -> Vec<Commit> {
    let mut v: Vec<Commit> = out
        .lines()
        .filter_map(|l| {
            let mut f = l.splitn(3, '\t');
            Some(Commit {
                at: f.next()?.parse().ok()?,
                hash: f.next()?.to_string(),
                subject: f.next().unwrap_or("").to_string(),
            })
        })
        .collect();
    v.reverse();
    v
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn status_says_the_branch_what_is_ahead_and_how_much_changed() {
        let out = "# branch.oid 9adceca1234\n# branch.head claude/limits\n# branch.upstream origin/claude/limits\n# branch.ab +2 -0\n1 .M N... 100644 100644 100644 a b src/a.rs\n? notes.md\n";
        let s = parse_status(out);
        assert_eq!(s.branch, "claude/limits");
        assert_eq!(s.ahead, Some(2));
        assert_eq!(s.changed, 2);
    }

    #[test]
    fn a_branch_with_no_upstream_has_nothing_to_be_ahead_of() {
        let s = parse_status("# branch.oid 9adceca1234\n# branch.head main\n");
        assert_eq!(s.ahead, None);
        assert_eq!(s.changed, 0);
    }

    #[test]
    fn a_detached_head_is_the_commit_it_is_on() {
        let s = parse_status("# branch.oid 9adceca1234\n# branch.head (detached)\n");
        assert_eq!(s.branch, "9adceca");
    }

    #[test]
    fn the_log_comes_back_oldest_first_and_keeps_tabs_in_a_subject() {
        let v = parse_log("200\tb2\tsecond\twith a tab\n100\ta1\tfirst\n");
        assert_eq!(v.len(), 2);
        assert_eq!(v[0].hash, "a1");
        assert_eq!(v[1].subject, "second\twith a tab");
    }

    #[test]
    fn a_remote_is_its_web_page_and_never_its_credentials() {
        let cases = [
            (
                "git@github.com:snymrova/snyvi.git",
                Some("https://github.com/snymrova/snyvi"),
            ),
            ("github.com:o/r", Some("https://github.com/o/r")),
            (
                "ssh://git@gitlab.com:22/group/sub/repo.git",
                Some("https://gitlab.com/group/sub/repo"),
            ),
            (
                "git+ssh://git@codeberg.org/o/r",
                Some("https://codeberg.org/o/r"),
            ),
            ("https://github.com/o/r.git", Some("https://github.com/o/r")),
            ("https://github.com/o/r/", Some("https://github.com/o/r")),
            (
                "https://x-access-token:ghp_secret@github.com/o/r.git",
                Some("https://github.com/o/r"),
            ),
            (
                "https://user@bitbucket.org/o/r.git?x=1",
                Some("https://bitbucket.org/o/r"),
            ),
            (
                "https://git.example.com:8443/o/r",
                Some("https://git.example.com:8443/o/r"),
            ),
            (
                "http://localhost:3000/o/r.git",
                Some("http://localhost:3000/o/r"),
            ),
            ("git://example.org/o/r.git", Some("https://example.org/o/r")),
            ("file:///home/me/r.git", None),
            ("/home/me/r.git", None),
            ("../r", None),
            ("C:\\repos\\r", None),
            ("C:/repos/r", None),
            ("https://github.com", None),
            ("", None),
        ];
        for (url, want) in cases {
            assert_eq!(web_url(url).as_deref(), want, "{url}");
        }
        for (url, _) in cases {
            assert!(
                !web_url(url).unwrap_or_default().contains("secret"),
                "{url}"
            );
        }
    }

    #[test]
    fn origin_is_the_remote_and_else_the_first() {
        let out = "remote.upstream.url https://github.com/a/b\nremote.origin.url git@github.com:o/r.git\n";
        assert_eq!(remote(out).as_deref(), Some("git@github.com:o/r.git"));
        assert_eq!(
            remote("remote.fork.url https://github.com/f/r\nremote.up.url x\n").as_deref(),
            Some("https://github.com/f/r")
        );
        assert_eq!(remote(""), None);
    }

    #[test]
    fn a_repository_says_its_remote_without_asking_it() {
        let dir = crate::store::tempdir::Dir::new("snyvi-git-remote");
        let git = |args: &[&str]| run(&dir.path, args).is_some();
        if !git(&["init", "-q"]) {
            return; // no git here
        }
        assert_eq!(read(&dir.path, 0).and_then(|s| s.remote), None, "no remote");
        // A remote that does not exist: nothing is fetched, so nothing fails.
        assert!(git(&[
            "remote",
            "add",
            "origin",
            "https://tok@github.invalid/o/r.git"
        ]));
        assert_eq!(
            read(&dir.path, 0).and_then(|s| s.remote).as_deref(),
            Some("https://github.invalid/o/r")
        );
    }

    #[test]
    fn a_folder_in_no_repository_is_not_read() {
        let dir = crate::store::tempdir::Dir::new("snyvi-git");
        if repository(&dir.path).is_none() {
            assert!(read(&dir.path, 0).is_none());
        }
    }
}