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
//! Git workspace identity for session paths: which checkout and which
//! repository a directory belongs to. Shared by session search scopes and the
//! `/sessions` hub grouping.
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Workspace {
/// Checkout root: the directory holding `.git`, or the directory itself
/// outside Git.
pub(crate) worktree: PathBuf,
/// Shared Git directory, identical for every worktree of one repository.
/// Equals `worktree` outside Git.
pub(crate) repo: PathBuf,
}
impl Workspace {
/// Whether the directory sits inside a Git checkout.
pub(crate) fn is_git(&self) -> bool {
self.repo != self.worktree
}
/// Resolve Git administrative paths without running project configuration,
/// hooks or subprocesses. A non-Git directory is its own local scope. A
/// `.git` file naming no Git directory there is passed over for the
/// checkout above it. `rho/fs/look` names a folder's project from this
/// too, so it groups folders as sessions are grouped.
pub(crate) fn resolve(cwd: &Path) -> Self {
let cwd = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
for ancestor in cwd.ancestors() {
let Some(git_dir) = crate::git::git_dir_at(ancestor) else {
continue;
};
// A linked worktree's Git directory names the one its
// repository's worktrees share, relative to itself.
let common = crate::git::read_small(&git_dir.join("commondir"))
.map(|relative| git_dir.join(relative.trim()))
.unwrap_or(git_dir);
if let Ok(repo) = common.canonicalize() {
return Self {
worktree: ancestor.to_path_buf(),
repo,
};
}
}
Self {
worktree: cwd.clone(),
repo: cwd,
}
}
}