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
//! Repository discovery: "is this path in a git repository, and where is
//! its root?"
//!
//! One `git rev-parse` invocation answers all of it, because each spawn is
//! a process and the *open repository* flow runs this per typed path.
use std::path::{Path, PathBuf};
use gitpoulpi_core::domain::{DiscoveredRepo, InitOptions, RepoRef};
use gitpoulpi_core::{Error, Result};
use crate::process::run;
/// The directory `git` should be run in for `path`.
///
/// `git -C <file>` fails; `git -C <missing dir>` fails too. A path the
/// user typed can be either, and the fix for the first (use the parent)
/// must not be applied to the second, or a nonexistent path inside a
/// repository would silently discover that repository.
fn start_dir(path: &Path) -> Result<PathBuf> {
let meta = std::fs::metadata(path).map_err(|_| Error::RepoNotFound(path.to_path_buf()))?;
if meta.is_dir() {
Ok(path.to_path_buf())
} else {
// A file inside a repository discovers the repository containing
// it; a file that is nowhere near one still fails below.
Ok(path
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| path.to_path_buf()))
}
}
pub fn discover(path: &Path) -> Result<DiscoveredRepo> {
let start = start_dir(path)?;
// `--show-toplevel` is empty for a bare repository, so the bare case is
// answered by `--is-bare-repository` and the root taken from
// `--absolute-git-dir`. One command keeps the answers from different
// repositories if the tree changes underneath us.
let raw = run(
&start,
&[
"rev-parse",
"--is-bare-repository",
"--is-inside-work-tree",
"--absolute-git-dir",
"--show-toplevel",
],
)
.map_err(|_| Error::RepoNotFound(path.to_path_buf()))?;
let mut lines = raw.lines();
let is_bare = lines.next().unwrap_or("false").trim() == "true";
let _is_inside_work_tree = lines.next().unwrap_or("false").trim() == "true";
let git_dir = PathBuf::from(lines.next().unwrap_or("").trim());
let toplevel = lines.next().unwrap_or("").trim().to_string();
let root = if is_bare || toplevel.is_empty() {
git_dir.clone()
} else {
PathBuf::from(toplevel)
};
if root.as_os_str().is_empty() {
return Err(Error::RepoNotFound(path.to_path_buf()));
}
// A linked worktree's git dir is `<main>/.git/worktrees/<name>`,
// never the repository's own `.git`. `--git-common-dir` names the
// shared one, so the two differing *is* the definition of "this is a
// linked worktree" -- more reliable than testing whether `.git` is a
// file, which is also true of a submodule.
let common = run(&root, &["rev-parse", "--git-common-dir"])
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.map(|s| absolutize(&root, Path::new(&s)));
let is_worktree = match common {
Some(common) => !same_path(&common, &git_dir),
None => false,
};
// `--abbrev-ref HEAD` prints the literal `HEAD` on a detached head,
// and fails outright in a repository with no commits. Both mean "no
// branch", and both have to come out as `None` rather than as a
// branch name nothing can check out.
let head_branch = run(&root, &["rev-parse", "--abbrev-ref", "HEAD"])
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty() && s != "HEAD");
Ok(DiscoveredRepo {
repo: RepoRef::from_root(&root),
name: DiscoveredRepo::name_for_root(&root),
is_bare,
is_worktree,
head_branch,
})
}
/// git may answer `--git-common-dir` relatively (`.git`), so it is
/// resolved against the directory the question was asked in before being
/// compared with the absolute `--absolute-git-dir`.
fn absolutize(base: &Path, path: &Path) -> PathBuf {
if path.is_absolute() {
path.to_path_buf()
} else {
base.join(path)
}
}
/// Compares two paths the way the filesystem would.
///
/// Canonicalisation matters on Windows in both directions: `C:\Users\…`
/// vs `\\?\C:\Users\…`, and a temp dir reached through a short (8.3) name
/// vs its long form. The raw-comparison fallback keeps a deleted path from
/// being reported as "not a worktree" because canonicalize failed.
fn same_path(a: &Path, b: &Path) -> bool {
match (a.canonicalize(), b.canonicalize()) {
(Ok(a), Ok(b)) => a == b,
_ => a == b,
}
}
/// `GitBackend::init` over `git init`.
///
/// Beyond spawning git it creates the directory chain, refuses a path that
/// is already a repository root, and reports the root by re-`discover`ing
/// the result, so the `RepoRef` the daemon keys on equals the one a later
/// `discover` of the same directory produces.
pub fn init(path: &Path, opts: &InitOptions) -> Result<RepoRef> {
if already_a_repository_root(path) {
return Err(Error::Backend(format!(
"{} is already a git repository",
path.display()
)));
}
// `git init <path>` would create it too, but the cwd the child is
// spawned in must exist regardless, and `process::git` spawns in the
// path it is given.
std::fs::create_dir_all(path)
.map_err(|e| Error::Backend(format!("could not create {}: {e}", path.display())))?;
let mut args: Vec<String> = vec!["init".to_string()];
if opts.bare {
args.push("--bare".to_string());
}
if let Some(branch) = &opts.initial_branch {
args.push("--initial-branch".to_string());
args.push(branch.clone());
}
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
run(path, &args_ref)?;
Ok(discover(path)?.repo)
}
/// Whether `path` *is* a repository root, as opposed to being inside one:
/// re-initializing a repository is refused, creating a nested one is not.
///
/// Duplicated in the git2 backend rather than hoisted into
/// `gitpoulpi-core`, which must not know how either backend discovers
/// anything.
fn already_a_repository_root(path: &Path) -> bool {
match discover(path) {
Ok(found) => found.repo.root == RepoRef::from_root(path).root,
Err(_) => false,
}
}