Skip to content

Commit 7bdecaf

Browse files
Reimplementing directories scan
1 parent 6619afb commit 7bdecaf

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

src/cli.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ pub struct Args {
2020
)]
2121
pub path: String,
2222

23-
#[arg(
24-
short = 's',
25-
long = "summary",
26-
help = "Show only repositories without listing untracked files",
27-
required = false
28-
)]
29-
pub summary: bool,
30-
3123
#[arg(
3224
short = 'w',
3325
long = "workers",
@@ -58,9 +50,20 @@ pub struct Args {
5850
pub exclude: Option<Vec<String>>,
5951

6052
#[arg(
61-
long = "scan-all",
62-
help = "Scan all git repos, with or without untracked files",
53+
short = 'u',
54+
long = "check-untracked",
55+
help = "Only show repositories with untracked files",
56+
default_value_t = false,
57+
required = false
58+
)]
59+
pub check_untracked: bool,
60+
61+
#[arg(
62+
short = 'v',
63+
long = "verbose",
64+
help = "Print verbose output (untracked files)",
65+
default_value_t = false,
6366
required = false
6467
)]
65-
pub scan_all: bool,
68+
pub verbose: bool,
6669
}

src/git_ops.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use colored::*;
2-
use git2::{DiffOptions, Repository, StatusOptions};
2+
use git2::{DiffOptions, Repository, Status, StatusOptions};
33
use std::io;
44
use std::io::ErrorKind;
5+
use std::mem::needs_drop;
56
use std::path::{Path, PathBuf};
67
use std::sync::mpsc;
78
use std::sync::mpsc::Sender;
@@ -14,9 +15,9 @@ fn is_excluded_dir(entry: &DirEntry, exclude_dirs: &[String]) -> bool {
1415
}
1516

1617
// Send the path to the channel if it is a Git repository
17-
fn check_and_send_repo(path: PathBuf, tx: Sender<PathBuf>) {
18+
fn check_and_send_repo(path: PathBuf, tx: Sender<PathBuf>, verbose: bool) {
1819
if path.join(".git").exists() {
19-
tx.send(path).unwrap();
20+
tx.send((path)).unwrap();
2021
}
2122
}
2223

@@ -25,6 +26,7 @@ pub fn find_git_repos(
2526
start_path: &Path,
2627
exclude_dirs: &[String],
2728
num_threads: usize,
29+
verbose: bool,
2830
) -> Vec<PathBuf> {
2931
let pool = ThreadPool::new(num_threads); // Create a thread pool with the specified number of threads
3032
let (tx, rx) = mpsc::channel(); // Create a channel to send results from threads to the main thread
@@ -35,7 +37,7 @@ pub fn find_git_repos(
3537
if path.is_dir() && !is_excluded_dir(&entry, exclude_dirs) {
3638
let tx = tx.clone(); // Clone the sender to be used in the thread
3739
pool.execute(move || {
38-
check_and_send_repo(path, tx); // Check if the directory is a Git repository and send the path if it is
40+
check_and_send_repo(path, tx, verbose); // Check if the directory is a Git repository and send the path if it is
3941
});
4042
}
4143
}
@@ -55,7 +57,7 @@ pub fn check_untracked_files(repo_path: &Path) -> Result<Vec<String>, git2::Erro
5557
let mut untracked_files = Vec::new();
5658
for entry in statuses.iter() {
5759
let status = entry.status();
58-
if status.is_index_new() || status.is_wt_new() || status.is_wt_modified() {
60+
if status.contains(Status::WT_NEW) {
5961
let file_path = entry.path().unwrap_or("unknown file").to_string();
6062
untracked_files.push(file_path);
6163
}

src/main.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,41 @@ fn main() {
3939
let start_path = Path::new(&args.path);
4040
// If user not specify exclude dirs, set it to empty
4141
let exclude_dirs = args.exclude.as_deref().unwrap_or(&[]);
42-
let git_repos = find_git_repos(start_path, &exclude_dirs, args.workers as usize);
42+
let git_repos = find_git_repos(
43+
start_path,
44+
&exclude_dirs,
45+
args.workers as usize,
46+
args.check_untracked,
47+
);
48+
49+
// Print results
50+
for repo in git_repos {
51+
let repo_path = repo.clone();
52+
if args.check_untracked {
53+
match check_untracked_files(&repo_path) {
54+
Ok(untracked_files) => {
55+
if !untracked_files.is_empty() && args.verbose {
56+
println_orange!("Untracked files in: {}", repo.display());
57+
for file in untracked_files {
58+
println_light_orange!(" - {}", file);
59+
if args.diff {
60+
match show_diff(&repo_path, &file) {
61+
Ok(diff) => println!("{}", diff),
62+
Err(e) => eprintln!("{}: {}", "Error showing diff".red(), e),
63+
}
64+
}
65+
}
66+
}
67+
}
68+
Err(e) => {
69+
if args.verbose {
70+
eprintln!("{}: {}", "Error checking repository".red(), e)
71+
}
72+
}
73+
}
74+
}
75+
println_orange!("Found git repository: {}", repo.display());
76+
}
4377

4478
//
4579
// // Spawn a thread for each repo to check for untracked files

0 commit comments

Comments
 (0)