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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use color_eyre::Result;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;
/// Represents a Git repository with its path
#[derive(Debug, Clone)]
pub struct GitRepo {
path: PathBuf,
branch: String,
remote_status: Option<String>,
status: Option<String>,
missing: bool,
remote_url: Option<String>,
}
impl GitRepo {
/// Create a new GitRepo from a path (branch only, async fields are None)
pub fn new(path: PathBuf) -> Self {
let branch = Self::read_branch(&path);
let remote_url = Self::read_remote_url(&path);
Self {
path,
branch,
remote_status: None,
status: None,
missing: false,
remote_url,
}
}
/// Create a new missing GitRepo (exists in cache but not on disk)
pub fn new_missing(path: PathBuf, remote_url: Option<String>) -> Self {
Self {
path,
branch: String::new(),
remote_status: None,
status: None,
missing: true,
remote_url,
}
}
/// Check if this repo is missing from disk
pub fn is_missing(&self) -> bool {
self.missing
}
/// Mark this repository as missing (deleted)
pub fn set_missing(&mut self) {
self.missing = true;
}
/// Update the remote status
pub fn set_remote_status(&mut self, remote_status: String) {
self.remote_status = Some(remote_status);
}
/// Update the working tree status
pub fn set_status(&mut self, status: String) {
self.status = Some(status);
}
/// Check if async data is loaded
pub fn is_loaded(&self) -> bool {
self.remote_status.is_some() && self.status.is_some()
}
/// Get the repository path
pub fn path(&self) -> &Path {
&self.path
}
/// Get the repository name
pub fn name(&self) -> Option<&str> {
self.path.file_name()?.to_str()
}
/// Get the parent directory name
pub fn parent_name(&self) -> Option<&str> {
self.path.parent()?.file_name()?.to_str()
}
/// Get a formatted display string in the form "parent/repo"
pub fn display_short(&self) -> String {
match (self.parent_name(), self.name()) {
(Some(parent), Some(name)) => format!("{}/{}", parent, name),
(None, Some(name)) => name.to_string(),
_ => self.path.display().to_string(),
}
}
/// Get the current branch name
pub fn branch(&self) -> &str {
&self.branch
}
/// Get the remote tracking status (ahead/behind)
pub fn remote_status(&self) -> &str {
self.remote_status.as_deref().unwrap_or("loading...")
}
/// Get the working tree status
pub fn status(&self) -> &str {
self.status.as_deref().unwrap_or("loading...")
}
/// Get the cached remote URL (origin)
pub fn get_remote_url(&self) -> Option<String> {
self.remote_url.clone()
}
/// Clone this repository to its expected path
pub fn clone_repository(&self) -> Result<()> {
if !self.missing {
return Err(color_eyre::eyre::eyre!("Repository already exists"));
}
let remote_url = self
.remote_url
.as_ref()
.ok_or_else(|| color_eyre::eyre::eyre!("No remote URL for repository"))?;
// Create parent directory if it doesn't exist
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)?;
}
// Check if it's a GitHub repository
let is_github = remote_url.contains("github.com");
let output = if is_github {
// Use gh repo clone for GitHub repos
Command::new("gh")
.args(["repo", "clone", remote_url, &self.path.to_string_lossy()])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.output()
} else {
// Use git clone for non-GitHub repos
Command::new("git")
.args(["clone", remote_url, &self.path.to_string_lossy()])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.output()
}?;
if !output.status.success() {
return Err(color_eyre::eyre::eyre!("Failed to clone repository"));
}
Ok(())
}
/// Read the remote URL from git config
fn read_remote_url(path: &Path) -> Option<String> {
Command::new("git")
.args(["remote", "get-url", "origin"])
.current_dir(path)
.output()
.ok()
.and_then(|output| {
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
None
}
})
}
/// Read the current branch name from .git/HEAD
fn read_branch(path: &Path) -> String {
// Try to read .git/HEAD to get the current branch
let head_path = path.join(".git").join("HEAD");
if let Ok(content) = fs::read_to_string(&head_path) {
let content = content.trim();
// HEAD typically contains "ref: refs/heads/branch-name"
if let Some(branch_ref) = content.strip_prefix("ref: refs/heads/") {
return branch_ref.to_string();
}
// If it's a detached HEAD, show first 7 chars of commit hash
if content.len() >= 7 {
return format!("detached@{}", &content[..7]);
}
}
// Fallback if we can't determine the branch
"unknown".to_string()
}
/// Read the remote tracking status (ahead/behind)
pub fn read_remote_status(path: &Path) -> String {
// Check if there are any remotes configured
let has_remote = Command::new("git")
.args(["remote"])
.current_dir(path)
.output()
.ok()
.and_then(|output| {
if output.status.success() {
Some(!output.stdout.is_empty())
} else {
None
}
})
.unwrap_or(false);
if !has_remote {
return "local-only".to_string();
}
// Get ahead/behind count
let output = Command::new("git")
.args(["rev-list", "--left-right", "--count", "HEAD...@{upstream}"])
.current_dir(path)
.output();
if let Ok(output) = output
&& output.status.success()
{
let stdout = String::from_utf8_lossy(&output.stdout);
let parts: Vec<&str> = stdout.split_whitespace().collect();
if parts.len() == 2
&& let (Ok(ahead), Ok(behind)) = (parts[0].parse::<i32>(), parts[1].parse::<i32>())
{
if ahead == 0 && behind == 0 {
return "up-to-date".to_string();
}
return format!("↑{} ↓{}", ahead, behind);
}
}
// No tracking branch or error
"no-tracking".to_string()
}
/// Read the working tree status (clean/dirty)
pub fn read_status(path: &Path) -> String {
// Run git status --porcelain to check for changes
let output = Command::new("git")
.args(["status", "--porcelain"])
.current_dir(path)
.output();
if let Ok(output) = output
&& output.status.success()
{
let stdout = String::from_utf8_lossy(&output.stdout);
if stdout.trim().is_empty() {
return "clean".to_string();
}
// Count staged and unstaged changes
let mut staged = 0;
let mut unstaged = 0;
for line in stdout.lines() {
if line.len() >= 2 {
let index_status = &line[0..1];
let work_tree_status = &line[1..2];
if index_status != " " && index_status != "?" {
staged += 1;
}
if work_tree_status != " " {
unstaged += 1;
}
}
}
match (staged, unstaged) {
(0, u) if u > 0 => format!("{}M", u),
(s, 0) if s > 0 => format!("{}S", s),
(s, u) if s > 0 && u > 0 => format!("{}S {}M", s, u),
_ => "dirty".to_string(),
}
} else {
"unknown".to_string()
}
}
/// Fetch from all remotes and optionally fast-forward if possible
pub fn fetch(path: &Path, update: bool) -> Result<()> {
// First, fetch from all remotes
let output = Command::new("git")
.args(["fetch", "--all", "--prune"])
.current_dir(path)
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(color_eyre::eyre::eyre!("git fetch failed: {}", stderr));
}
// Try to fast-forward merge the current branch with its upstream if requested
if update {
// This only succeeds if it's a clean fast-forward (no divergence)
let merge_output = Command::new("git")
.args(["merge", "--ff-only", "@{upstream}"])
.current_dir(path)
.output()?;
// If merge succeeded, also update submodules
if merge_output.status.success() {
let _ = Command::new("git")
.args(["submodule", "update", "--init", "--recursive"])
.current_dir(path)
.output();
}
}
Ok(())
}
}
/// Check if a directory is a git repository
fn is_git_repo(path: &Path) -> bool {
path.join(".git").exists()
}
/// Scan directory recursively and find all git repositories
pub fn find_git_repos(root: &Path) -> Vec<GitRepo> {
WalkDir::new(root)
.into_iter()
.filter_entry(|e| {
let filename = e.file_name();
// Skip .git directories and other hidden directories
if filename.to_str().is_some_and(|s| s.starts_with('.')) {
return false;
}
// Skip tmp directories
if filename.to_str().is_some_and(|s| s == "tmp") {
return false;
}
// Skip if parent is a git repo (don't descend into nested repos)
if let Some(parent) = e.path().parent()
&& parent != root
&& is_git_repo(parent)
{
return false;
}
true
})
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().is_dir() && is_git_repo(entry.path()))
.map(|entry| {
let path = entry
.path()
.canonicalize()
.unwrap_or_else(|_| entry.path().to_path_buf());
GitRepo::new(path)
})
.collect()
}