ghtui 0.1.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Repository selection, fetching and object prefetching.
//!
//! A PR's diff is computed in the user's own clone when the current
//! directory is one (any remote pointing at the base repo counts, which covers
//! fork clones). There ghtui only writes refs under `refs/ghtui/` and never
//! touches branches, HEAD, the index or the working tree. Otherwise it keeps a
//! bare partial clone (`--filter=blob:none`) per repository in the cache.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex, OnceLock};

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;

use crate::git::blobs::BlobReader;
use crate::git::credentials::Credentials;
use crate::git::files::{ChangedFile, parse_raw};
use crate::git::{GitError, Oid, Version, git, github_repo_from_url, piped, stdout};

/// `GIT_NO_LAZY_FETCH` lets us ask which objects are missing without
/// fetching them.
const NO_LAZY_FETCH_VERSION: Version = Version(2, 44, 0);
/// `git check-attr --source` reads attributes from a commit (needed in bare
/// repositories).
const CHECK_ATTR_SOURCE_VERSION: Version = Version(2, 40, 0);

/// Progress lines from long-running git commands (clone, fetch).
pub type Progress<'a> = &'a (dyn Fn(String) + Send + Sync);

#[derive(Debug, Clone)]
pub struct Repo {
    pub path: PathBuf,
    pub remote: String,
    /// A promisor (partial clone) remote: blobs may be missing locally.
    pub partial: bool,
    version: Version,
    credentials: Credentials,
}

/// git's empty tree: what a root commit is diffed against.
pub const EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";

/// Refs fetched for a PR (or a commit and its parent).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrRefs {
    pub head: Oid,
    pub base: Oid,
    /// `merge-base(base, head)`: the diff runs from here to `head`, like
    /// GitHub's three-dot comparison.
    pub merge_base: Oid,
}

/// A commit in a PR.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Commit {
    pub oid: Oid,
    pub subject: String,
}

/// One lock per repository path: concurrent fetches into one repository
/// conflict on ref and pack locks.
fn repo_lock(path: &Path) -> Arc<tokio::sync::Mutex<()>> {
    static LOCKS: OnceLock<Mutex<HashMap<PathBuf, Arc<tokio::sync::Mutex<()>>>>> = OnceLock::new();
    let mut locks = LOCKS
        .get_or_init(Mutex::default)
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    locks.entry(path.to_owned()).or_default().clone()
}

impl Repo {
    /// The user's clone containing `dir`, if it has a remote for
    /// `owner/name`.
    pub async fn find_local(dir: &Path, owner: &str, name: &str) -> Result<Option<Repo>, GitError> {
        let Ok(top) = crate::git::run(Some(dir), &["rev-parse", "--show-toplevel"]).await else {
            return Ok(None);
        };
        let top = PathBuf::from(top.trim());
        let remotes = crate::git::remotes(&top).await?;
        let Some(remote) = remotes.iter().find(|r| {
            github_repo_from_url(&r.url).is_some_and(|repo| {
                repo.owner.eq_ignore_ascii_case(owner) && repo.name.eq_ignore_ascii_case(name)
            })
        }) else {
            return Ok(None);
        };
        Self::open_local(&top, &remote.name).await.map(Some)
    }

    /// The user's clone at `top`, fetching from `remote`.
    pub async fn open_local(top: &Path, remote: &str) -> Result<Repo, GitError> {
        let version = crate::git::version().await?;
        Self::open(top.to_owned(), remote, version, Credentials::Ambient).await
    }

    /// Opens (cloning on first use) the cache clone of `url` at
    /// `<cache_root>/repos/<owner>/<name>.git`.
    pub async fn open_cache(
        cache_root: &Path,
        owner: &str,
        name: &str,
        url: &str,
        credentials: Credentials,
        progress: Progress<'_>,
    ) -> Result<Repo, GitError> {
        let path = cache_root
            .join("repos")
            .join(owner.to_ascii_lowercase())
            .join(format!("{}.git", name.to_ascii_lowercase()));
        let _guard = repo_lock(&path).lock_owned().await;
        let version = crate::git::version().await?;
        if !path.join("HEAD").exists() {
            // Clone next to the target and rename, so an interrupted clone
            // never leaves a half-made repository behind.
            let parent = path.parent().ok_or_else(|| {
                std::io::Error::other(format!("no parent for {}", path.display()))
            })?;
            std::fs::create_dir_all(parent)?;
            let tmp = parent.join(format!(".{name}.git.tmp-{}", std::process::id()));
            if tmp.exists() {
                std::fs::remove_dir_all(&tmp)?;
            }
            progress(format!("Cloning {owner}/{name}"));
            let mut cmd = git(None);
            credentials.apply(&mut cmd);
            cmd.args(["clone", "--bare", "--filter=blob:none", "--progress", url])
                .arg(&tmp);
            if let Err(err) = run_with_progress(cmd, "clone", None, progress).await {
                // The clone's error is the one to report; a half-made
                // directory left behind is removed on the next try.
                let _ = std::fs::remove_dir_all(&tmp);
                return Err(err);
            }
            std::fs::rename(&tmp, &path)?;
        }
        Self::open(path, "origin", version, credentials).await
    }

    async fn open(
        path: PathBuf,
        remote: &str,
        version: Version,
        credentials: Credentials,
    ) -> Result<Repo, GitError> {
        let mut repo = Repo {
            path,
            remote: remote.to_owned(),
            partial: false,
            version,
            credentials,
        };
        repo.partial = repo.is_promisor().await;
        Ok(repo)
    }

    fn cmd(&self) -> Command {
        let mut cmd = git(Some(&self.path));
        self.credentials.apply(&mut cmd);
        cmd
    }

    async fn run(&self, args: &[&str]) -> Result<String, GitError> {
        stdout(&self.cmd().args(args).output().await?, &args.join(" "))
    }

    async fn is_promisor(&self) -> bool {
        let key = format!("remote.{}.promisor", self.remote);
        self.run(&["config", "--get", &key])
            .await
            .is_ok_and(|v| v.trim() == "true")
    }

    fn ref_name(number: u64, what: &str) -> String {
        format!("refs/ghtui/pr/{number}/{what}")
    }

    /// Fetches the PR head and its base branch into `refs/ghtui/pr/<N>/`
    /// and computes the merge base.
    ///
    /// `base_oid` is the base the PR had, when it's no longer its branch's
    /// tip: once merged with a merge commit, a PR's head is in its base
    /// branch, so the tip would leave nothing to diff.
    pub async fn fetch_pr(
        &self,
        number: u64,
        base_branch: &str,
        base_oid: Option<&Oid>,
        progress: Progress<'_>,
    ) -> Result<PrRefs, GitError> {
        let head_ref = Self::ref_name(number, "head");
        let base_ref = Self::ref_name(number, "base");
        progress(format!("Fetching #{number}"));
        let head_spec = format!("+refs/pull/{number}/head:{head_ref}");
        let base_spec = format!("+refs/heads/{base_branch}:{base_ref}");
        self.fetch(&[&head_spec, &base_spec], progress).await?;
        let head = self.rev_parse(&head_ref).await?;
        let base = match base_oid {
            Some(oid) => self.ensure(oid, progress).await.map(|()| oid.clone())?,
            None => self.rev_parse(&base_ref).await?,
        };
        let merge_base = self.merge_base(&base, &head).await?;
        Ok(PrRefs {
            head,
            base,
            merge_base,
        })
    }

    pub async fn merge_base(&self, a: &str, b: &str) -> Result<Oid, GitError> {
        Oid::parse(self.run(&["merge-base", a, b]).await?.trim())
    }

    /// Commits in `from..to`, oldest first.
    pub async fn commits(&self, from: &str, to: &str) -> Result<Vec<Commit>, GitError> {
        let out = self
            .run(&[
                "log",
                "--reverse",
                "--format=%H%x1f%s",
                &format!("{from}..{to}"),
            ])
            .await?;
        Ok(out
            .lines()
            .filter_map(|l| l.split_once('\u{1f}'))
            .filter_map(|(sha, subject)| {
                Some(Commit {
                    oid: Oid::parse(sha).ok()?,
                    subject: subject.to_owned(),
                })
            })
            .collect())
    }

    pub async fn rev_parse(&self, rev: &str) -> Result<Oid, GitError> {
        let out = self
            .run(&[
                "rev-parse",
                "--verify",
                "--quiet",
                &format!("{rev}^{{commit}}"),
            ])
            .await?;
        Oid::parse(out.trim())
    }

    /// Pins `sha` as `refs/ghtui/pr/<N>/seen/<sha>` so force-pushes and
    /// `git gc` can't remove a head we've shown.
    pub async fn pin_seen(&self, number: u64, sha: &str) -> Result<(), GitError> {
        let name = Self::ref_name(number, &format!("seen/{sha}"));
        self.run(&["update-ref", &name, sha]).await.map(drop)
    }

    /// Whether `rev` (e.g. `<sha>:<path>`) resolves locally, without lazy
    /// fetching.
    pub async fn has(&self, rev: &str) -> bool {
        self.cmd()
            .env("GIT_NO_LAZY_FETCH", "1")
            .args(["cat-file", "-e", rev])
            .status()
            .await
            .is_ok_and(|s| s.success())
    }

    /// A commit against its first parent, the empty tree for a root commit;
    /// fetched by SHA if it isn't here.
    pub async fn commit_refs(&self, oid: &Oid, progress: Progress<'_>) -> Result<PrRefs, GitError> {
        self.ensure(oid, progress).await?;
        let parent = match self.rev_parse(&format!("{oid}^")).await {
            Ok(parent) => parent,
            Err(_) => Oid::parse(EMPTY_TREE)?,
        };
        Ok(PrRefs {
            head: oid.clone(),
            base: parent.clone(),
            merge_base: parent,
        })
    }

    /// The diff from one commit to another, each fetched by SHA if it
    /// isn't here.
    pub async fn range_refs(
        &self,
        from: &Oid,
        to: &Oid,
        progress: Progress<'_>,
    ) -> Result<PrRefs, GitError> {
        self.ensure(from, progress).await?;
        self.ensure(to, progress).await?;
        Ok(PrRefs {
            head: to.clone(),
            base: from.clone(),
            merge_base: from.clone(),
        })
    }
    /// Fetches the remote's default branch into `into` (a ref name).
    pub async fn fetch_head(&self, into: &str, progress: Progress<'_>) -> Result<(), GitError> {
        self.fetch(&[&format!("+HEAD:{into}")], progress).await
    }

    /// Fetches `refspecs` (refs, or commits by SHA) from the remote,
    /// reporting git's progress.
    async fn fetch(&self, refspecs: &[&str], progress: Progress<'_>) -> Result<(), GitError> {
        let _guard = repo_lock(&self.path).lock_owned().await;
        let mut cmd = self.cmd();
        cmd.args([
            "fetch",
            "--no-tags",
            "--no-write-fetch-head",
            "--progress",
            &self.remote,
        ])
        .args(refspecs);
        run_with_progress(cmd, "fetch", None, progress)
            .await
            .map(drop)
    }

    /// Every file's path at `rev`.
    pub async fn file_names(&self, rev: &str) -> Result<Vec<String>, GitError> {
        let out = self
            .run(&["ls-tree", "-r", "-z", "--name-only", rev])
            .await?;
        Ok(out
            .split('\0')
            .filter(|p| !p.is_empty())
            .map(str::to_owned)
            .collect())
    }

    /// A file's text at `rev` (fetched, in a partial clone, if it isn't
    /// here).
    pub async fn file_text(&self, rev: &str, path: &str) -> Result<String, GitError> {
        self.run(&["cat-file", "blob", &format!("{rev}:{path}")])
            .await
    }
    /// Makes sure a commit is here, fetching it by SHA if it isn't (e.g. a
    /// head that was force-pushed away). GitHub serves commits it still has
    /// even when no ref points at them.
    pub async fn ensure(&self, oid: &Oid, progress: Progress<'_>) -> Result<(), GitError> {
        if self.has(oid).await {
            return Ok(());
        }
        progress(format!("Fetching {}", oid.get(..7).unwrap_or(oid)));
        self.fetch(&[oid], progress).await
    }

    /// Files changed from `from` to `to`, with rename detection. In a
    /// partial clone git fetches the blobs rename detection needs in one
    /// batch.
    pub async fn changed_files(&self, from: &str, to: &str) -> Result<Vec<ChangedFile>, GitError> {
        let out = self
            .run(&["diff", "--raw", "-z", "--no-abbrev", "-M", from, to])
            .await?;
        parse_raw(&out)
    }

    /// Which of `oids` aren't present locally. Without `GIT_NO_LAZY_FETCH`
    /// (git < 2.44) this can't be asked safely, so everything counts as
    /// missing.
    pub async fn missing(&self, oids: &[String]) -> Result<Vec<String>, GitError> {
        if oids.is_empty() {
            return Ok(Vec::new());
        }
        if self.version < NO_LAZY_FETCH_VERSION {
            return Ok(oids.to_vec());
        }
        let mut cmd = self.cmd();
        cmd.env("GIT_NO_LAZY_FETCH", "1")
            .args(["cat-file", "--batch-check=%(objectname)"]);
        let out = run_with_stdin(cmd, "cat-file --batch-check", oids.join("\n") + "\n").await?;
        Ok(out
            .lines()
            .filter_map(|l| l.strip_suffix(" missing"))
            .map(str::to_owned)
            .collect())
    }

    /// Fetches the given blobs in a single request if any are missing.
    /// Returns how many were fetched. No-op outside partial clones.
    pub async fn prefetch(&self, oids: &[String]) -> Result<usize, GitError> {
        if !self.partial {
            return Ok(0);
        }
        let mut unique: Vec<String> = oids.to_vec();
        unique.sort();
        unique.dedup();
        let missing = self.missing(&unique).await?;
        if missing.is_empty() {
            return Ok(0);
        }
        let _guard = repo_lock(&self.path).lock_owned().await;
        // The same invocation git uses for its own lazy fetches, but for all
        // the blobs at once.
        let mut cmd = self.cmd();
        cmd.args([
            "-c",
            "fetch.negotiationAlgorithm=noop",
            "fetch",
            "--no-tags",
            "--no-write-fetch-head",
            "--recurse-submodules=no",
            "--filter=blob:none",
            "--progress",
            "--stdin",
            &self.remote,
        ]);
        let input = Some(missing.join("\n") + "\n");
        run_with_progress(cmd, "fetch --stdin", input, &|_| {}).await?;
        Ok(missing.len())
    }

    /// Paths marked `linguist-generated` or `linguist-vendored` in the
    /// `.gitattributes` at `commit`. Empty on git < 2.40.
    pub async fn generated_paths(
        &self,
        commit: &str,
        paths: &[&str],
    ) -> Result<HashSet<String>, GitError> {
        if paths.is_empty() || self.version < CHECK_ATTR_SOURCE_VERSION {
            return Ok(HashSet::new());
        }
        let mut cmd = self.cmd();
        cmd.args([
            "check-attr",
            "-z",
            "--stdin",
            &format!("--source={commit}"),
            "linguist-generated",
            "linguist-vendored",
        ]);
        let mut input = paths.join("\0");
        input.push('\0');
        let out = run_with_stdin(cmd, "check-attr", input).await?;
        let fields: Vec<&str> = out.split('\0').collect();
        let (triples, _) = fields.as_chunks::<3>();
        Ok(triples
            .iter()
            .filter(|[_, _, value]| matches!(*value, "set" | "true"))
            .map(|[path, _, _]| (*path).to_owned())
            .collect())
    }

    pub fn blob_reader(&self) -> Result<BlobReader, GitError> {
        let repo = self.clone();
        BlobReader::spawn(move || repo.cmd())
    }
}

async fn run_with_stdin(mut cmd: Command, what: &str, input: String) -> Result<String, GitError> {
    cmd.stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let mut child = cmd.spawn()?;
    let writer = feed(&mut child, input)?;
    let output = child.wait_with_output().await?;
    let _ = writer.await;
    stdout(&output, what)
}

/// Writes `input` to `child`'s piped stdin from its own task, so git can
/// fill its output pipes while it reads.
fn feed(child: &mut Child, input: String) -> Result<JoinHandle<()>, GitError> {
    let mut stdin = piped(child.stdin.take(), "stdin")?;
    Ok(tokio::spawn(async move {
        let _ = stdin.write_all(input.as_bytes()).await;
    }))
}

/// Runs `cmd` (writing `input` to its stdin, if any), reporting the
/// latest `--progress` line from stderr. It isn't timed: the transfer gives
/// up when it goes quiet (see [`crate::git::git`]), and git's silence says
/// nothing, since it prints nothing while a ref list or a packet of the
/// pack is on its way.
async fn run_with_progress(
    mut cmd: Command,
    what: &str,
    input: Option<String>,
    progress: Progress<'_>,
) -> Result<String, GitError> {
    if input.is_some() {
        cmd.stdin(Stdio::piped());
    }
    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = cmd.spawn()?;
    let writer = input.map(|input| feed(&mut child, input)).transpose()?;
    let mut stderr = piped(child.stderr.take(), "stderr")?;
    let mut stdout = piped(child.stdout.take(), "stdout")?;
    let out_task = tokio::spawn(async move {
        let mut buf = Vec::new();
        let _ = stdout.read_to_end(&mut buf).await;
        buf
    });
    let mut all = Vec::new();
    let mut chunk = [0u8; 4096];
    let mut line = Vec::new();
    loop {
        let n = stderr.read(&mut chunk).await?;
        let Some(read) = chunk.get(..n).filter(|read| !read.is_empty()) else {
            break;
        };
        all.extend_from_slice(read);
        for &b in read {
            if b == b'\r' || b == b'\n' {
                let text = String::from_utf8_lossy(&line).trim().to_owned();
                if !text.is_empty() {
                    progress(text);
                }
                line.clear();
            } else {
                line.push(b);
            }
        }
    }
    if let Some(writer) = writer {
        let _ = writer.await;
    }
    let status = child.wait().await?;
    let stdout = out_task.await.unwrap_or_default();
    if !status.success() {
        let stderr = String::from_utf8_lossy(&all);
        // Progress output is noise in an error; keep the real messages.
        let message: Vec<&str> = stderr
            .split(['\r', '\n'])
            .map(str::trim)
            .filter(|l| !l.is_empty() && !l.contains('%'))
            .collect();
        return Err(GitError::Failed {
            args: what.to_owned(),
            stderr: message.join("; "),
        });
    }
    Ok(String::from_utf8_lossy(&stdout).into_owned())
}

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

    /// A transfer is given up on when nothing arrives, however slowly it
    /// moves otherwise: never for its speed.
    #[test]
    fn git_gives_up_only_on_quiet_connections() {
        let cmd = git(None);
        let envs: HashMap<_, _> = cmd.as_std().get_envs().collect();
        let env = |key: &str| envs.get(std::ffi::OsStr::new(key)).copied().flatten();
        if std::env::var_os("GIT_HTTP_LOW_SPEED_LIMIT").is_none() {
            assert_eq!(env("GIT_HTTP_LOW_SPEED_LIMIT"), Some("1".as_ref()));
        }
    }
}