aboutsummaryrefslogtreecommitdiff
path: root/src/workspace/git/discovery.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/workspace/git/discovery.rs')
-rw-r--r--src/workspace/git/discovery.rs126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/workspace/git/discovery.rs b/src/workspace/git/discovery.rs
index de4b090c..7e973bb9 100644
--- a/src/workspace/git/discovery.rs
+++ b/src/workspace/git/discovery.rs
@@ -111,6 +111,11 @@ fn git_common_dir_for_git_dir(git_dir: &Path) -> PathBuf {
pub fn git_branch(cwd: &Path) -> Option<String> {
let repo_root = git_repo_root(cwd)?;
let git_dir = git_dir_for_repo_root(&repo_root)?;
+ let git_common_dir = git_common_dir_for_git_dir(&git_dir);
+ if git_ref_storage_is_reftable(&git_common_dir) {
+ return git_symbolic_head_short(&repo_root);
+ }
+
let head = std::fs::read_to_string(git_dir.join("HEAD")).ok()?;
parse_git_head_branch(&head)
}
@@ -131,11 +136,86 @@ pub(super) fn git_dir_for_repo_root(repo_root: &Path) -> Option<PathBuf> {
})
}
+pub(super) fn git_symbolic_head_full(repo_root: &Path) -> Option<String> {
+ git_trimmed_stdout(repo_root, &["symbolic-ref", "--quiet", "HEAD"])
+}
+
+fn git_symbolic_head_short(repo_root: &Path) -> Option<String> {
+ git_trimmed_stdout(repo_root, &["symbolic-ref", "--quiet", "--short", "HEAD"])
+}
+
+pub(super) fn git_rev_parse_verify(repo_root: &Path, revision: &str) -> Option<String> {
+ git_trimmed_stdout(repo_root, &["rev-parse", "--verify", revision])
+}
+
+pub(super) fn git_ref_storage_is_reftable(git_common_dir: &Path) -> bool {
+ read_git_config_value(&git_common_dir.join("config"), "extensions", "refstorage")
+ .is_some_and(|value| value.eq_ignore_ascii_case("reftable"))
+}
+
fn parse_git_head_branch(head: &str) -> Option<String> {
let branch = head.trim().strip_prefix("ref: refs/heads/")?;
(!branch.is_empty()).then(|| branch.to_string())
}
+fn read_git_config_value(path: &Path, section: &str, key: &str) -> Option<String> {
+ let contents = std::fs::read_to_string(path).ok()?;
+ let mut in_section = false;
+ for raw_line in contents.lines() {
+ let line = raw_line.trim();
+ if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
+ continue;
+ }
+ if let Some(section_name) = simple_git_config_section(line) {
+ in_section = section_name.eq_ignore_ascii_case(section);
+ continue;
+ }
+ if !in_section {
+ continue;
+ }
+ let Some((name, value)) = line.split_once('=') else {
+ continue;
+ };
+ if name.trim().eq_ignore_ascii_case(key) {
+ return Some(strip_git_config_comment(value).trim().to_string());
+ }
+ }
+ None
+}
+
+fn simple_git_config_section(line: &str) -> Option<&str> {
+ let section = line.strip_prefix('[')?.split_once(']')?.0.trim();
+ (!section.contains('"')).then_some(section)
+}
+
+fn strip_git_config_comment(value: &str) -> &str {
+ let value = value.trim();
+ for marker in ['#', ';'] {
+ if let Some((prefix, _)) = value.split_once(marker) {
+ if prefix.chars().next_back().is_some_and(char::is_whitespace) {
+ return prefix;
+ }
+ }
+ }
+ value
+}
+
+fn git_trimmed_stdout(repo_root: &Path, args: &[&str]) -> Option<String> {
+ let output = std::process::Command::new("git")
+ .arg("-C")
+ .arg(repo_root)
+ .args(args)
+ .output()
+ .ok()?;
+ if !output.status.success() {
+ return None;
+ }
+
+ let stdout = String::from_utf8(output.stdout).ok()?;
+ let stdout = stdout.trim();
+ (!stdout.is_empty()).then(|| stdout.to_string())
+}
+
pub(super) fn git_repo_root(start: &Path) -> Option<PathBuf> {
let mut current = if start.is_dir() {
start.to_path_buf()
@@ -187,6 +267,7 @@ mod tests {
use std::time::{SystemTime, UNIX_EPOCH};
use super::*;
+ use crate::workspace::git::test_support::run_git;
fn temp_test_dir(name: &str) -> PathBuf {
let unique = format!(
@@ -239,6 +320,24 @@ mod tests {
}
#[test]
+ fn git_branch_reads_symbolic_head_from_reftable_repo() {
+ let root = temp_test_dir("reftable-branch");
+ let root_arg = root.to_string_lossy().to_string();
+ let output = std::process::Command::new("git")
+ .args(["init", "--ref-format=reftable", "-b", "main", &root_arg])
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ std::fs::remove_dir_all(root).unwrap();
+ return;
+ }
+
+ assert_eq!(git_branch(&root).as_deref(), Some("main"));
+
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
+ #[test]
fn git_repo_root_ignores_invalid_git_marker() {
let base = temp_test_dir("invalid-git-root");
let cwd = base.join("workspace");
@@ -275,4 +374,31 @@ mod tests {
std::fs::remove_dir_all(root).unwrap();
}
+
+ #[test]
+ fn git_rev_parse_verify_reads_reftable_refs() {
+ let root = temp_test_dir("reftable-ref-oid");
+ let root_arg = root.to_string_lossy().to_string();
+ let output = std::process::Command::new("git")
+ .args(["init", "--ref-format=reftable", "-b", "main", &root_arg])
+ .output()
+ .unwrap();
+ if !output.status.success() {
+ std::fs::remove_dir_all(root).unwrap();
+ return;
+ }
+
+ run_git(&root, &["config", "user.email", "herdr@example.invalid"]);
+ run_git(&root, &["config", "user.name", "Herdr Test"]);
+ run_git(&root, &["commit", "--allow-empty", "-m", "initial"]);
+
+ let head_oid = git_rev_parse_verify(&root, "HEAD").unwrap();
+
+ assert_eq!(
+ git_rev_parse_verify(&root, "refs/heads/main").as_deref(),
+ Some(head_oid.as_str())
+ );
+
+ std::fs::remove_dir_all(root).unwrap();
+ }
}