-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.rs
More file actions
216 lines (180 loc) · 5.98 KB
/
git.rs
File metadata and controls
216 lines (180 loc) · 5.98 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
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
//! Git operations using system git commands for maximum compatibility
use crate::config::Repository;
use anyhow::{Context, Result};
use colored::*;
use std::path::Path;
use std::process::Command;
#[derive(Default)]
pub struct Logger;
impl Logger {
pub fn info(&self, repo: &Repository, msg: &str) {
println!("{} | {}", repo.name.cyan().bold(), msg);
}
pub fn success(&self, repo: &Repository, msg: &str) {
println!("{} | {}", repo.name.cyan().bold(), msg.green());
}
pub fn warn(&self, repo: &Repository, msg: &str) {
println!("{} | {}", repo.name.cyan().bold(), msg.yellow());
}
#[allow(dead_code)]
pub fn error(&self, repo: &Repository, msg: &str) {
eprintln!("{} | {}", repo.name.cyan().bold(), msg.red());
}
}
pub fn clone_repository(repo: &Repository) -> Result<()> {
let logger = Logger;
let target_dir = repo.get_target_dir();
// Check if directory already exists
if Path::new(&target_dir).exists() {
logger.warn(repo, "Repository directory already exists, skipping");
return Ok(());
}
let mut args = vec!["clone"];
// Add branch flag if a branch is specified
if let Some(branch) = &repo.branch {
args.extend_from_slice(&["-b", branch]);
logger.info(
repo,
&format!("Cloning branch '{}' from {}", branch, repo.url),
);
} else {
logger.info(repo, &format!("Cloning default branch from {}", repo.url));
}
// Add repository URL and target directory
args.push(&repo.url);
args.push(&target_dir);
let output = Command::new("git")
.args(&args)
.output()
.context("Failed to execute git clone command")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("Failed to clone repository: {}", stderr);
}
logger.success(repo, "Successfully cloned");
Ok(())
}
pub fn remove_repository(repo: &Repository) -> Result<()> {
let target_dir = repo.get_target_dir();
if Path::new(&target_dir).exists() {
std::fs::remove_dir_all(&target_dir).context("Failed to remove repository directory")?;
Ok(())
} else {
anyhow::bail!("Repository directory does not exist: {}", target_dir);
}
}
pub fn has_changes(repo_path: &str) -> Result<bool> {
// Check if there are any uncommitted changes using git status
let output = Command::new("git")
.arg("status")
.arg("--porcelain")
.current_dir(repo_path)
.output()
.context("Failed to execute git status command")?;
if !output.status.success() {
anyhow::bail!(
"Failed to check repository status: {}",
String::from_utf8_lossy(&output.stderr)
);
}
// If output is empty, there are no changes
Ok(!output.stdout.is_empty())
}
pub fn create_and_checkout_branch(repo_path: &str, branch_name: &str) -> Result<()> {
// Create and checkout a new branch using git checkout -b
let output = Command::new("git")
.arg("checkout")
.arg("-b")
.arg(branch_name)
.current_dir(repo_path)
.output()
.context("Failed to execute git checkout command")?;
if !output.status.success() {
anyhow::bail!(
"Failed to create and checkout branch '{}': {}",
branch_name,
String::from_utf8_lossy(&output.stderr)
);
}
Ok(())
}
pub fn add_all_changes(repo_path: &str) -> Result<()> {
// Add all changes using git add .
let output = Command::new("git")
.arg("add")
.arg(".")
.current_dir(repo_path)
.output()
.context("Failed to execute git add command")?;
if !output.status.success() {
anyhow::bail!(
"Failed to add changes: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(())
}
pub fn commit_changes(repo_path: &str, message: &str) -> Result<()> {
// Commit changes using git commit
let output = Command::new("git")
.arg("commit")
.arg("-m")
.arg(message)
.current_dir(repo_path)
.output()
.context("Failed to execute git commit command")?;
if !output.status.success() {
anyhow::bail!(
"Failed to commit changes: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(())
}
pub fn push_branch(repo_path: &str, branch_name: &str) -> Result<()> {
// Push branch using git push
let output = Command::new("git")
.arg("push")
.arg("--set-upstream")
.arg("origin")
.arg(branch_name)
.current_dir(repo_path)
.output()
.context("Failed to execute git push command")?;
if !output.status.success() {
anyhow::bail!(
"Failed to push branch: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(())
}
pub fn get_default_branch(repo_path: &str) -> Result<String> {
// Try to get the default branch using git symbolic-ref
let output = Command::new("git")
.args(["symbolic-ref", "refs/remotes/origin/HEAD"])
.current_dir(repo_path)
.output();
if let Ok(output) = output
&& output.status.success()
{
let branch_ref = String::from_utf8_lossy(&output.stdout).trim().to_string();
if let Some(branch) = branch_ref.strip_prefix("refs/remotes/origin/") {
return Ok(branch.to_string());
}
}
// Fallback: try to get the current branch
let output = Command::new("git")
.args(["branch", "--show-current"])
.current_dir(repo_path)
.output()
.context("Failed to execute git branch command")?;
if output.status.success() {
let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !current_branch.is_empty() {
return Ok(current_branch);
}
}
// Final fallback to default branch
Ok(crate::constants::git::FALLBACK_BRANCH.to_string())
}