mars_agents/source/
git_cli.rs1use std::path::{Path, PathBuf};
4
5use crate::error::MarsError;
6use crate::platform::cache::git_cache_component;
7use crate::platform::process::{display_command, run_git, run_git_with_ref};
8use crate::source::{AvailableVersion, GlobalCache};
9
10use super::git::parse_semver_tag;
11
12pub(crate) fn ls_remote_ref(url: &str, reference: &str) -> Result<String, MarsError> {
13 let command_display = display_command(&["ls-remote", url, reference]);
14 let output = run_git_with_ref(
15 &["ls-remote", url],
16 reference,
17 Path::new("."),
18 "resolve remote git reference",
19 )?;
20
21 for line in output.lines() {
22 if let Some((sha, _)) = line.split_once('\t')
23 && !sha.trim().is_empty()
24 {
25 return Ok(sha.trim().to_string());
26 }
27 }
28
29 Err(MarsError::GitCli {
30 command: command_display,
31 message: format!("reference `{reference}` not found"),
32 })
33}
34
35pub fn ls_remote_tags(url: &str) -> Result<Vec<AvailableVersion>, MarsError> {
37 let output = run_git(
38 &["ls-remote", "--tags", url],
39 Path::new("."),
40 "list remote git tags",
41 )?;
42 let mut versions = Vec::new();
43
44 for line in output.lines() {
45 let Some((sha, reference)) = line.split_once('\t') else {
46 continue;
47 };
48 let Some(tag) = reference.strip_prefix("refs/tags/") else {
49 continue;
50 };
51
52 if tag.ends_with("^{}") {
55 continue;
56 }
57
58 let Some(version) = parse_semver_tag(tag) else {
59 continue;
60 };
61
62 versions.push(AvailableVersion {
63 tag: tag.to_string(),
64 version,
65 commit_id: sha.trim().to_string(),
66 });
67 }
68
69 versions.sort_by(|a, b| a.version.cmp(&b.version));
70 Ok(versions)
71}
72
73pub fn ls_remote_head(url: &str) -> Result<String, MarsError> {
75 ls_remote_ref(url, "HEAD")
76}
77
78pub(crate) fn fetch_git_clone(
79 url: &str,
80 tag: Option<&str>,
81 sha: Option<&str>,
82 cache: &GlobalCache,
83) -> Result<PathBuf, MarsError> {
84 let cache_name = git_cache_component(url)?;
85 let cache_path = cache.git_dir().join(cache_name);
86
87 let lock_path = cache_path.with_extension("lock");
90 let _lock = crate::fs::FileLock::acquire(&lock_path)?;
91
92 let cache_path_display = cache_path.to_string_lossy().to_string();
93 let was_cached = cache_path.exists();
94
95 if !was_cached {
96 let mut args = vec!["clone", "--depth", "1"];
97 if let Some(tag_name) = tag {
98 args.push("--branch");
99 args.push(tag_name);
100 }
101 args.push(url);
102 args.push(&cache_path_display);
103
104 run_git(&args, &cache.git_dir(), "clone git source into cache")?;
105 } else {
106 run_git(
107 &["fetch", "--depth", "1", "origin"],
108 &cache_path,
109 "fetch cached git source",
110 )?;
111 }
112
113 if was_cached {
114 if let Some(tag_name) = tag {
115 run_git(
116 &["checkout", tag_name],
117 &cache_path,
118 "checkout cached git tag",
119 )?;
120 }
121
122 if let Some(sha) = sha {
123 run_git(
124 &["checkout", sha],
125 &cache_path,
126 "checkout cached git commit",
127 )?;
128 } else if tag.is_none() {
129 run_git(
130 &["checkout", "origin/HEAD"],
131 &cache_path,
132 "checkout cached git default head",
133 )?;
134 }
135 } else if let Some(sha) = sha {
136 run_git(
137 &["checkout", sha],
138 &cache_path,
139 "checkout cloned git commit",
140 )?;
141 }
142
143 Ok(cache_path)
144}