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
use std::{
path::{Path, PathBuf},
process::Command,
};
use anyhow::{Result, anyhow};
pub struct RemoteOperations {
cache_dir: PathBuf,
}
impl RemoteOperations {
pub fn new(cache_dir: PathBuf) -> Self {
Self { cache_dir }
}
/// Clone repository using system git command
fn clone_with_system_git(&self, repo_url: &str, repo_path: &Path, version: &str) -> Result<()> {
// Create parent directory if it doesn't exist
if let Some(parent) = repo_path.parent() {
std::fs::create_dir_all(parent)?;
}
// Clone with system git - shallow clone for speed
// For tags, we need to clone with tags to ensure they're available
let mut clone_args = vec!["clone", "--depth", "1", "--quiet"];
if self.is_immutable_version(version)
&& (version.starts_with('v') || version.chars().next().unwrap_or('a').is_ascii_digit())
{
clone_args.push("--tags");
}
clone_args.push(repo_url);
let output = Command::new("git")
.args(&clone_args)
.arg(repo_path)
.output()?;
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!(
"Failed to clone repository '{}': {}",
repo_url,
error_msg
));
}
// Checkout the specified version
self.checkout_version_system_git(repo_path, version)?;
Ok(())
}
/// Checkout a specific version using system git
fn checkout_version_system_git(&self, repo_path: &Path, version: &str) -> Result<()> {
// Try to checkout the version - git will try branches, tags, and commits
let output = Command::new("git")
.args(["checkout", "--quiet", version])
.current_dir(repo_path)
.output()?;
if !output.status.success() {
// Try fetching the specific branch/tag if it's not local
let fetch_output = Command::new("git")
.args([
"fetch",
"--quiet",
"origin",
&format!("{version}:{version}"),
])
.current_dir(repo_path)
.output()?;
if fetch_output.status.success() {
// Try checkout again
let output = Command::new("git")
.args(["checkout", "--quiet", version])
.current_dir(repo_path)
.output()?;
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!(
"Could not checkout version '{}': {}",
version,
error_msg
));
}
} else {
let error_msg = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!(
"Could not find version '{}' in repository: {}",
version,
error_msg
));
}
}
tracing::info!("Checked out version: {}", version);
Ok(())
}
/// Clone repository (called when it doesn't exist in cache)
pub fn clone_repository(&self, repo_url: &str, repo_name: &str, version: &str) -> Result<()> {
let repo_path = self.cache_dir.join(repo_name);
self.clone_with_system_git(repo_url, &repo_path, version)?;
Ok(())
}
/// Check if version is immutable (tag or commit SHA)
fn is_immutable_version(&self, version: &str) -> bool {
// Tag pattern: v1.0.0, v2.1.3-beta, 1.0.0, etc.
if version.starts_with('v')
&& version.len() > 1
&& let Some(first_char) = version.chars().nth(1)
&& first_char.is_ascii_digit()
{
return true;
}
// Commit SHA pattern: 7+ hex characters (short or full SHA)
if version.len() >= 7
&& version.len() <= 40
&& version.chars().all(|c| c.is_ascii_hexdigit())
{
return true;
}
// Semantic version without 'v' prefix: 1.0.0, 2.1.3-beta
if version.chars().next().unwrap_or('a').is_ascii_digit() && version.contains('.') {
return true;
}
false // Assume it's a mutable branch
}
/// Fetch and reset to remote version (ensures cache matches remote exactly)
pub fn fetch_and_reset(&self, repo_name: &str, version: &str) -> Result<()> {
let repo_path = self.cache_dir.join(repo_name);
tracing::trace!(
"Checking if cache needs update for {} @ {}",
repo_name,
version
);
// Fast path: For immutable versions (tags/commits), just check if we have it locally
if self.is_immutable_version(version) {
tracing::debug!("Version '{}' appears to be immutable (tag/commit)", version);
// Check if we have this version locally
let has_version_output = Command::new("git")
.args(["rev-parse", "--verify", "--quiet", version])
.current_dir(&repo_path)
.output()?;
if has_version_output.status.success() {
// Get the SHA of this version
let version_sha_output = Command::new("git")
.args(["rev-parse", version])
.current_dir(&repo_path)
.output()?;
// Get current HEAD SHA
let head_sha_output = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(&repo_path)
.output()?;
if version_sha_output.status.success() && head_sha_output.status.success() {
let version_sha = String::from_utf8_lossy(&version_sha_output.stdout)
.trim()
.to_string();
let head_sha = String::from_utf8_lossy(&head_sha_output.stdout)
.trim()
.to_string();
if version_sha == head_sha {
tracing::info!(
"Cache already has immutable version: {} ({})",
version,
&version_sha[..8]
);
return Ok(());
} else {
tracing::debug!(
"Cache has version {} but HEAD is different, need to reset",
version
);
// Reset to the correct version (no fetch needed for immutable versions)
let reset_output = Command::new("git")
.args(["reset", "--hard", version])
.current_dir(&repo_path)
.output()?;
if reset_output.status.success() {
tracing::info!(
"Reset cache to immutable version: {} ({})",
version,
&version_sha[..8]
);
return Ok(());
}
}
}
}
tracing::debug!(
"Don't have immutable version {} locally, need to fetch",
version
);
} else {
tracing::debug!(
"Version '{}' appears to be mutable (branch), checking remote",
version
);
// Slow path: For mutable versions (branches), compare with remote
let local_sha_output = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(&repo_path)
.output()?;
let remote_sha_output = Command::new("git")
.args(["ls-remote", "origin", version])
.current_dir(&repo_path)
.output()?;
if local_sha_output.status.success() && remote_sha_output.status.success() {
let local_sha = String::from_utf8_lossy(&local_sha_output.stdout)
.trim()
.to_string();
let remote_output = String::from_utf8_lossy(&remote_sha_output.stdout);
// Parse remote SHA (format: "commit_sha\trefs/heads/branch_name" or just
// "commit_sha")
let remote_sha = remote_output
.lines()
.next()
.and_then(|line| line.split_whitespace().next())
.unwrap_or("")
.to_string();
tracing::trace!(
"Local SHA: {}, Remote SHA: {}",
&local_sha[..8],
&remote_sha[..8]
);
if local_sha == remote_sha && !local_sha.is_empty() {
tracing::info!(
"Cache already up to date: {} ({})",
version,
&local_sha[..8]
);
return Ok(());
}
tracing::debug!(
"Cache needs update: local {} != remote {}",
&local_sha[..8],
&remote_sha[..8]
);
} else {
tracing::trace!("Could not compare SHAs, proceeding with fetch");
}
}
// Fetch only the specific branch/tag we need with depth 1 (just the latest commit)
tracing::debug!("Fetching {} from origin", version);
let mut fetch_args = vec!["fetch", "--depth", "1"];
// For immutable versions that look like tags, fetch tags
if self.is_immutable_version(version)
&& (version.starts_with('v') || version.chars().next().unwrap_or('a').is_ascii_digit())
{
fetch_args.extend_from_slice(&["--tags", "origin"]);
} else {
fetch_args.extend_from_slice(&["origin", version]);
}
let output = Command::new("git")
.args(&fetch_args)
.current_dir(&repo_path)
.output()?;
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!("Failed to fetch from origin: {}", error_msg));
}
tracing::debug!("Fetch completed, resetting to fetched commit");
// Reset to FETCH_HEAD (what we just fetched) - this is guaranteed to work
let reset_output = Command::new("git")
.args(["reset", "--hard", "FETCH_HEAD"])
.current_dir(&repo_path)
.output()?;
if !reset_output.status.success() {
let error_msg = String::from_utf8_lossy(&reset_output.stderr);
return Err(anyhow!(
"Failed to reset to FETCH_HEAD after fetching '{}': {}",
version,
error_msg
));
}
// Clean any untracked files
Command::new("git")
.args(["clean", "-fd"])
.current_dir(&repo_path)
.output()?;
// Get and log the current commit SHA
let sha_output = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(&repo_path)
.output()?;
if sha_output.status.success() {
let sha = String::from_utf8_lossy(&sha_output.stdout)
.trim()
.to_string();
tracing::info!("Reset cache to version: {} ({})", version, &sha[..8]);
// Store the SHA in .guardy directory for later reference
let guardy_dir = PathBuf::from(".guardy");
std::fs::create_dir_all(&guardy_dir)?;
let sha_file = guardy_dir.join(format!("sync_sha_{repo_name}"));
std::fs::write(sha_file, format!("{version}\n{sha}"))?;
} else {
tracing::info!("Reset cache to version: {}", version);
}
Ok(())
}
}