diff options
Diffstat (limited to 'src/git')
| -rw-r--r-- | src/git/mod.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/git/mod.rs b/src/git/mod.rs index a783782..599a94b 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs | |||
| @@ -203,6 +203,82 @@ pub fn delete_ref(repo_path: &Path, ref_name: &str) -> Result<(), String> { | |||
| 203 | Ok(()) | 203 | Ok(()) |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | /// Update a git ref to point to a specific commit | ||
| 207 | /// | ||
| 208 | /// # Arguments | ||
| 209 | /// * `repo_path` - Path to the bare git repository | ||
| 210 | /// * `ref_name` - The ref name to update (e.g., "refs/heads/main") | ||
| 211 | /// * `commit_hash` - The commit hash to set the ref to | ||
| 212 | /// | ||
| 213 | /// # Returns | ||
| 214 | /// Ok(()) if successful, Err with error message otherwise | ||
| 215 | pub fn update_ref(repo_path: &Path, ref_name: &str, commit_hash: &str) -> Result<(), String> { | ||
| 216 | debug!( | ||
| 217 | "Updating ref {} to {} in {}", | ||
| 218 | ref_name, | ||
| 219 | commit_hash, | ||
| 220 | repo_path.display() | ||
| 221 | ); | ||
| 222 | |||
| 223 | let output = Command::new("git") | ||
| 224 | .args(["update-ref", ref_name, commit_hash]) | ||
| 225 | .current_dir(repo_path) | ||
| 226 | .output() | ||
| 227 | .map_err(|e| format!("Failed to execute git update-ref: {}", e))?; | ||
| 228 | |||
| 229 | if !output.status.success() { | ||
| 230 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 231 | return Err(format!("git update-ref failed: {}", stderr)); | ||
| 232 | } | ||
| 233 | |||
| 234 | info!( | ||
| 235 | "Updated ref {} to {} in {}", | ||
| 236 | ref_name, | ||
| 237 | commit_hash, | ||
| 238 | repo_path.display() | ||
| 239 | ); | ||
| 240 | Ok(()) | ||
| 241 | } | ||
| 242 | |||
| 243 | /// List all refs in a repository with their commit hashes | ||
| 244 | /// | ||
| 245 | /// # Arguments | ||
| 246 | /// * `repo_path` - Path to the bare git repository | ||
| 247 | /// | ||
| 248 | /// # Returns | ||
| 249 | /// Vec of (ref_name, commit_hash) tuples | ||
| 250 | pub fn list_refs(repo_path: &Path) -> Result<Vec<(String, String)>, String> { | ||
| 251 | if !repo_path.exists() { | ||
| 252 | return Ok(Vec::new()); | ||
| 253 | } | ||
| 254 | |||
| 255 | let output = Command::new("git") | ||
| 256 | .args(["for-each-ref", "--format=%(refname) %(objectname)"]) | ||
| 257 | .current_dir(repo_path) | ||
| 258 | .output() | ||
| 259 | .map_err(|e| format!("Failed to execute git for-each-ref: {}", e))?; | ||
| 260 | |||
| 261 | if !output.status.success() { | ||
| 262 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 263 | return Err(format!("git for-each-ref failed: {}", stderr)); | ||
| 264 | } | ||
| 265 | |||
| 266 | let stdout = String::from_utf8_lossy(&output.stdout); | ||
| 267 | let refs = stdout | ||
| 268 | .lines() | ||
| 269 | .filter_map(|line| { | ||
| 270 | let parts: Vec<&str> = line.splitn(2, ' ').collect(); | ||
| 271 | if parts.len() == 2 { | ||
| 272 | Some((parts[0].to_string(), parts[1].to_string())) | ||
| 273 | } else { | ||
| 274 | None | ||
| 275 | } | ||
| 276 | }) | ||
| 277 | .collect(); | ||
| 278 | |||
| 279 | Ok(refs) | ||
| 280 | } | ||
| 281 | |||
| 206 | /// Validate refs/nostr/<event-id> ref against expected commit | 282 | /// Validate refs/nostr/<event-id> ref against expected commit |
| 207 | /// | 283 | /// |
| 208 | /// If the ref exists but points to a different commit than expected, | 284 | /// If the ref exists but points to a different commit than expected, |