-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrefs.rs
More file actions
81 lines (68 loc) · 2.32 KB
/
Copy pathrefs.rs
File metadata and controls
81 lines (68 loc) · 2.32 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
use std::fs;
use crate::repo::Repo;
use anyhow::{Result, anyhow};
/// Finds and resolves a Git reference to its commit hash.
///
/// # Arguments
/// * reference - The reference path to look up, prefixed with the type of reference (e.g. refs/heads)
/// * repo - The repository to search in
///
/// # Returns
/// * The commit hash as a string if found
pub fn find_ref(reference: &str, repo: &Repo) -> Result<String> {
let path = repo.git_dir().join(reference);
if !path.exists() {
return Err(anyhow!("Reference not found: {reference}"));
}
let content = fs::read_to_string(path)?;
if content.starts_with("ref: ") {
let target = content.trim_start_matches("ref: ").trim_end();
return find_ref(target, repo);
}
Ok(content.trim_end().to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
struct TestRepo {
dir: tempfile::TempDir,
repo: Repo,
}
impl TestRepo {
fn new() -> Self {
let dir = tempdir().unwrap();
fs::create_dir_all(dir.path().join(".git").join("refs").join("heads")).unwrap();
let repo = Repo::new(dir.path());
TestRepo { dir, repo }
}
fn create_ref(&self, name: &str, content: &str) {
let ref_path = self.dir.path().join(".git/refs/heads").join(name);
fs::write(ref_path, content).unwrap();
}
}
#[test]
fn test_find_ref_existing() {
let test_repo = TestRepo::new();
test_repo.create_ref("main", "commit_hash\n");
let result = find_ref("refs/heads/main", &test_repo.repo);
assert_eq!(result.unwrap(), "commit_hash");
}
#[test]
fn test_find_ref_non_existing() {
let test_repo = TestRepo::new();
let result = find_ref("non_existing", &test_repo.repo);
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert_eq!(err, "Reference not found: non_existing");
}
#[test]
fn test_find_ref_with_reference() {
let test_repo = TestRepo::new();
test_repo.create_ref("main", "ref: refs/heads/feature\n");
test_repo.create_ref("feature", "commit_hash");
let result = find_ref("refs/heads/main", &test_repo.repo);
assert_eq!(result.unwrap(), "commit_hash");
}
}