-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.rs
More file actions
422 lines (382 loc) · 13.5 KB
/
git.rs
File metadata and controls
422 lines (382 loc) · 13.5 KB
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
//! Git config / remote-url helpers — Rust port of `packages/reader/src/git.ts`.
//!
//! Three pieces:
//!
//! - [`parse_git_config`] reads `.git/config` text into a section map,
//! handling `[remote "origin"]`-style subsections and stripping inline
//! `#` / `;` comments outside quotes.
//! - [`canonicalize_remote_url`] normalizes scp / https / ssh remote URLs
//! into a stable `host/path` key, lowercasing the host but preserving
//! owner/repo case.
//! - [`ProjectResolver`] walks up from a cwd, opens `.git/config` (or
//! follows a `.git` worktree pointer), and returns `{project, project_key}`
//! with a per-instance cache. The free [`resolve_project`] uses a process
//! global resolver for the common case; tests construct their own to
//! avoid sharing global state.
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{LazyLock, Mutex};
use regex::Regex;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedProject {
pub project: String,
pub project_key: Option<String>,
}
/// Cached resolver. Construct one per scope (CLI, test, embedding) to avoid
/// sharing a process-global cache.
#[derive(Debug, Default)]
pub struct ProjectResolver {
cache: Mutex<HashMap<String, ResolvedProject>>,
}
impl ProjectResolver {
pub fn new() -> Self {
Self::default()
}
/// Resolve a project for `cwd`, consulting (and populating) the cache.
/// Holds the cache lock across `resolve_uncached` so concurrent callers
/// with the same `cwd` only do the filesystem walk once.
pub fn resolve(&self, cwd: &str) -> ResolvedProject {
let mut cache = self.cache.lock().unwrap();
if let Some(hit) = cache.get(cwd) {
return hit.clone();
}
let result = resolve_uncached(cwd);
cache.insert(cwd.to_string(), result.clone());
result
}
pub fn clear(&self) {
self.cache.lock().unwrap().clear();
}
}
static GLOBAL_RESOLVER: LazyLock<ProjectResolver> = LazyLock::new(ProjectResolver::new);
/// Convenience wrapper around a process-global [`ProjectResolver`]. Embedders
/// that want their own cache (notably tests) should construct their own
/// `ProjectResolver` directly.
pub fn resolve_project(cwd: &str) -> ResolvedProject {
GLOBAL_RESOLVER.resolve(cwd)
}
fn resolve_uncached(cwd: &str) -> ResolvedProject {
let fallback = || ResolvedProject {
project: cwd.to_string(),
project_key: None,
};
let Some(git_dir) = find_git_dir(Path::new(cwd)) else {
return fallback();
};
let Ok(text) = fs::read_to_string(git_dir.join("config")) else {
return fallback();
};
let key = parse_git_config(&text)
.get("remote \"origin\"")
.and_then(|m| m.get("url"))
.and_then(|url| canonicalize_remote_url(url));
ResolvedProject {
project: cwd.to_string(),
project_key: key,
}
}
fn find_git_dir(start: &Path) -> Option<PathBuf> {
let mut dir: PathBuf = start.canonicalize().unwrap_or_else(|_| start.to_path_buf());
for _ in 0..100 {
let candidate = dir.join(".git");
if let Ok(meta) = fs::metadata(&candidate) {
if meta.is_dir() {
return Some(candidate);
}
if meta.is_file() {
if let Some(resolved) = resolve_worktree_git_dir(&candidate) {
return Some(resolved);
}
}
}
match dir.parent() {
Some(parent) if parent != dir => dir = parent.to_path_buf(),
_ => return None,
}
}
None
}
fn resolve_worktree_git_dir(git_file: &Path) -> Option<PathBuf> {
static GITDIR_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?m)^gitdir:\s*(.+?)\s*$").unwrap());
let text = fs::read_to_string(git_file).ok()?;
let raw = GITDIR_RE.captures(&text)?.get(1)?.as_str().to_string();
let raw_path = Path::new(&raw);
let gitdir = if raw_path.is_absolute() {
raw_path.to_path_buf()
} else {
git_file.parent()?.join(raw_path)
};
if let Ok(text) = fs::read_to_string(gitdir.join("commondir")) {
let trimmed = text.trim();
if !trimmed.is_empty() {
let p = Path::new(trimmed);
return Some(if p.is_absolute() {
p.to_path_buf()
} else {
gitdir.join(p)
});
}
}
Some(gitdir)
}
/// Parse `.git/config` text into `{section -> {key -> value}}`. Handles
/// `[section]` and `[section "subsection"]` headers and ignores `#` / `;`
/// comments + blank lines. Inline comments outside quotes are stripped.
pub fn parse_git_config(text: &str) -> HashMap<String, HashMap<String, String>> {
static SUBSECTION_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^([A-Za-z0-9._-]+)\s+"(.*)"$"#).unwrap());
let mut out: HashMap<String, HashMap<String, String>> = HashMap::new();
let mut current: Option<String> = None;
for raw_line in text.split('\n') {
let line = raw_line
.trim_end_matches('\r')
.trim_start_matches([' ', '\t'])
.trim_end_matches([' ', '\t']);
if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
continue;
}
if line.starts_with('[') && line.ends_with(']') {
let raw = line[1..line.len() - 1].trim();
let name = match SUBSECTION_RE.captures(raw) {
Some(c) => format!("{} \"{}\"", &c[1], &c[2]),
None => raw.to_string(),
};
out.entry(name.clone()).or_default();
current = Some(name);
continue;
}
let Some(section) = current.as_ref() else {
continue;
};
let Some(eq) = line.find('=') else { continue };
let key = line[..eq].trim().to_string();
let value = strip_inline_comment(line[eq + 1..].trim());
if key.is_empty() {
continue;
}
out.get_mut(section).unwrap().insert(key, value);
}
out
}
fn strip_inline_comment(value: &str) -> String {
let mut out = String::new();
let mut in_quotes = false;
for ch in value.chars() {
if ch == '"' {
in_quotes = !in_quotes;
continue;
}
if !in_quotes && (ch == '#' || ch == ';') {
break;
}
out.push(ch);
}
out.trim().to_string()
}
/// Canonicalize a remote URL into `host/path` form. Returns `None` for inputs
/// that aren't recognizable git URLs.
pub fn canonicalize_remote_url(url: &str) -> Option<String> {
static SCP_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?:[A-Za-z0-9_-]+)@([^:\s]+):(.+)$").unwrap());
static SCHEME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([A-Za-z][A-Za-z0-9+.\-]*)://(.+)$").unwrap());
let trimmed = url.trim();
if trimmed.is_empty() {
return None;
}
if let Some(c) = SCP_RE.captures(trimmed) {
let host = c.get(1)?.as_str().to_lowercase();
let path_part = strip_dot_git(
c.get(2)?
.as_str()
.trim_start_matches('/')
.trim_end_matches('/'),
);
if path_part.is_empty() {
return None;
}
return Some(format!("{host}/{path_part}"));
}
if let Some(c) = SCHEME_RE.captures(trimmed) {
let rest = c.get(2)?.as_str();
let after_auth = match rest.find('@') {
Some(idx) => &rest[idx + 1..],
None => rest,
};
let slash = after_auth.find('/')?;
let host = strip_port(&after_auth[..slash]).to_lowercase();
if host.is_empty() {
return None;
}
let path_part = strip_dot_git(after_auth[slash + 1..].trim_end_matches('/'));
if path_part.is_empty() {
return None;
}
return Some(format!("{host}/{path_part}"));
}
None
}
fn strip_dot_git(p: &str) -> String {
p.strip_suffix(".git").unwrap_or(p).to_string()
}
fn strip_port(host: &str) -> &str {
match host.find(':') {
Some(idx) => &host[..idx],
None => host,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn canonicalize_scp_form() {
assert_eq!(
canonicalize_remote_url("git@github.com:AgentWorkforce/burn.git").as_deref(),
Some("github.com/AgentWorkforce/burn"),
);
}
#[test]
fn canonicalize_https_with_dot_git() {
assert_eq!(
canonicalize_remote_url("https://github.com/AgentWorkforce/burn.git").as_deref(),
Some("github.com/AgentWorkforce/burn"),
);
}
#[test]
fn canonicalize_https_without_dot_git_with_subgroup() {
assert_eq!(
canonicalize_remote_url("https://gitlab.com/group/sub/repo").as_deref(),
Some("gitlab.com/group/sub/repo"),
);
}
#[test]
fn canonicalize_https_with_user() {
assert_eq!(
canonicalize_remote_url("https://user:token@github.com/foo/bar.git").as_deref(),
Some("github.com/foo/bar"),
);
}
#[test]
fn canonicalize_ssh_with_port() {
assert_eq!(
canonicalize_remote_url("ssh://git@github.com:22/AgentWorkforce/burn.git").as_deref(),
Some("github.com/AgentWorkforce/burn"),
);
}
#[test]
fn canonicalize_lowercases_host_only() {
assert_eq!(
canonicalize_remote_url("git@GitHub.COM:AgentWorkforce/Burn.git").as_deref(),
Some("github.com/AgentWorkforce/Burn"),
);
}
#[test]
fn canonicalize_returns_none_on_junk() {
assert_eq!(canonicalize_remote_url(""), None);
assert_eq!(canonicalize_remote_url("not a url"), None);
assert_eq!(canonicalize_remote_url("https://example.com/"), None);
}
#[test]
fn canonicalize_strips_trailing_slash() {
assert_eq!(
canonicalize_remote_url("https://github.com/foo/bar/").as_deref(),
Some("github.com/foo/bar"),
);
}
#[test]
fn parse_simple_sections() {
let cfg = parse_git_config(
"\n[core]\n\trepositoryformatversion = 0\n[remote \"origin\"]\n\turl = git@github.com:foo/bar.git\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n",
);
assert_eq!(cfg["core"]["repositoryformatversion"], "0");
assert_eq!(
cfg["remote \"origin\""]["url"],
"git@github.com:foo/bar.git"
);
}
#[test]
fn parse_ignores_comments_and_blanks() {
let cfg = parse_git_config(
"\n# a comment\n; another comment\n[remote \"origin\"]\n\turl = https://github.com/foo/bar ; inline comment\n",
);
assert_eq!(
cfg["remote \"origin\""]["url"],
"https://github.com/foo/bar"
);
}
#[test]
fn resolve_project_no_git() {
let resolver = ProjectResolver::new();
let dir = tempdir().unwrap();
let path = dir.path().to_string_lossy().to_string();
let got = resolver.resolve(&path);
assert_eq!(got.project, path);
assert_eq!(got.project_key, None);
}
#[test]
fn resolve_project_with_git_dir() {
let resolver = ProjectResolver::new();
let root = tempdir().unwrap();
let git_dir = root.path().join(".git");
fs::create_dir_all(&git_dir).unwrap();
fs::write(
git_dir.join("config"),
"[remote \"origin\"]\n\turl = git@github.com:foo/bar.git\n",
)
.unwrap();
let nested = root.path().join("packages").join("a");
fs::create_dir_all(&nested).unwrap();
let got = resolver.resolve(&nested.to_string_lossy());
assert_eq!(got.project_key.as_deref(), Some("github.com/foo/bar"));
}
#[test]
fn resolve_project_worktree() {
let resolver = ProjectResolver::new();
let root = tempdir().unwrap();
let common_git = root.path().join("main").join(".git");
fs::create_dir_all(&common_git).unwrap();
fs::write(
common_git.join("config"),
"[remote \"origin\"]\n\turl = https://github.com/foo/bar\n",
)
.unwrap();
let worktree_dir = common_git.join("worktrees").join("branch-a");
fs::create_dir_all(&worktree_dir).unwrap();
fs::write(worktree_dir.join("commondir"), "../..\n").unwrap();
let worktree = root.path().join("worktree-a");
fs::create_dir_all(&worktree).unwrap();
fs::write(
worktree.join(".git"),
format!("gitdir: {}\n", worktree_dir.display()),
)
.unwrap();
let got = resolver.resolve(&worktree.to_string_lossy());
assert_eq!(got.project_key.as_deref(), Some("github.com/foo/bar"));
}
#[test]
fn resolve_project_memoizes() {
let resolver = ProjectResolver::new();
let root = tempdir().unwrap();
let git_dir = root.path().join(".git");
fs::create_dir_all(&git_dir).unwrap();
fs::write(
git_dir.join("config"),
"[remote \"origin\"]\n\turl = git@github.com:foo/bar.git\n",
)
.unwrap();
let key = root.path().to_string_lossy().to_string();
let a = resolver.resolve(&key);
// Mutating the config should not change the cached result.
fs::write(
git_dir.join("config"),
"[remote \"origin\"]\n\turl = git@github.com:zzz/zzz.git\n",
)
.unwrap();
let b = resolver.resolve(&key);
assert_eq!(a.project_key, b.project_key);
}
}