Skip to content

Commit eef3ef4

Browse files
Refactor some flags and code
1 parent 7bdecaf commit eef3ef4

File tree

6 files changed

+150
-114
lines changed

6 files changed

+150
-114
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ repos:
1313
rev: v8.18.0
1414
hooks:
1515
- id: gitleaks
16+
- repo: https://github.com/thlorenz/doctoc
17+
rev: v2.2.0
18+
hooks:
19+
- id: doctoc
1620
- repo: https://github.com/pre-commit/pre-commit
1721
rev: v3.5.0
1822
hooks:

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
4+
5+
- [Changelog](#changelog)
6+
- [[unreleased]](#unreleased)
7+
- [[1.1.0] - 2024-08-23](#110---2024-08-23)
8+
- [[1.0.0] - 2024-08-22](#100---2024-08-22)
9+
10+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
11+
112
# Changelog
213

314
All notable changes to this project will be documented in this file.

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ uninstall: ## Uninstall binary
2727

2828
build: ## Build binary
2929
cargo build --release --locked
30+
31+
cargo-fix: ## Run cargo fix
32+
cargo fix --bin gitrack
33+
34+
doctoc: ## Create table of contents with doctoc
35+
doctoc .

README.md

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
# gitrack
22

3-
I have a lot of personal/work repositories in my laptop. Sometimes you jump from change to change and forget to upload the commit. This tool looks for untracked changes in your local folders.
3+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
4+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
6+
7+
- [About](#about)
8+
- [Badges](#badges)
9+
- [Supported Platforms](#supported-platforms)
10+
- [Installation](#installation)
11+
- [Install latest version](#install-latest-version)
12+
- [Install specific release](#install-specific-release)
13+
- [Using cargo](#using-cargo)
14+
- [Uninstall](#uninstall)
15+
- [Usage](#usage)
16+
- [Help](#help)
17+
- [Scan folders containing git repositories](#scan-folders-containing-git-repositories)
18+
- [Scanning for untracked changes (summarized)](#scanning-for-untracked-changes-summarized)
19+
- [Scanning for untracked changes (verbose)](#scanning-for-untracked-changes-verbose)
20+
- [Diff files for untracked changes](#diff-files-for-untracked-changes)
21+
- [Control number of workers/threads:](#control-number-of-workersthreads)
22+
- [Exclude directories:](#exclude-directories)
23+
- [About threads](#about-threads)
24+
- [Dev](#dev)
25+
- [pre-commit](#pre-commit)
26+
- [Local container](#local-container)
27+
- [Examples](#examples)
28+
- [Scan personal folder summarized](#scan-personal-folder-summarized)
29+
- [Scan specific folder with details](#scan-specific-folder-with-details)
30+
- [Diff files](#diff-files)
31+
- [TODO](#todo)
32+
- [License](#license)
33+
34+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
35+
36+
# About
37+
38+
Scan git repositories in your file system. Find untracked changes, diff files, and more.
39+
40+
Easy and simple. This tool was created just for fun and to practice Rust.
41+
42+
Implemented features:
43+
44+
* Scan for untracked changes in git repositories.
45+
* Scan `.git` folders in your file system (with multithreads).
46+
* Diff files for untracked changes.
447

5-
Easy and simple.
648

749
# Badges
850

@@ -65,47 +107,65 @@ make uninstall
65107

66108
# Usage
67109

110+
## Help
111+
68112
```bash
69113
gitrack --help
70-
```
71114

72-
> By default, it will look for untracked changes in your home folder.
115+
Scan git repositories in your file system
116+
117+
Usage: gitrack [OPTIONS]
118+
119+
Options:
120+
-p, --path <PATH> Folder path you want to scan for git untracked files [default: /home/dcr]
121+
-w, --workers <WORKERS> Number of threads to use for scanning repositories [default: 5]
122+
-d, --diff Show differences between changed files
123+
-e, --exclude-dir <EXCLUDE>... Exclude directories to scan
124+
-u, --check-untracked Only show repositories with untracked files
125+
-v, --verbose Print verbose output
126+
-h, --help Print help
127+
-V, --version Print version
128+
```
73129

74-
Scanning for untracked changes, specific folder:
130+
## Scan folders containing git repositories
75131

76132
```bash
77-
gitrack -p /my/folder/path
133+
gitrack -p /home/elliot # home will be always the default values if -p is not provided
78134
```
79135

80-
Summarized output:
136+
## Scanning for untracked changes (summarized)
81137

82138
```bash
83-
gitrack -p /my/folder/path -s
84-
gitrack -s # remember without -p will scan your home folder
139+
gitrack -p /home/elliot -u
85140
```
86141

87-
Number of workers/threads:
142+
## Scanning for untracked changes (verbose)
88143

89144
```bash
90-
gitrack -p /home/elliot -w 3
145+
gitrack -p /home/elliot -u -v
91146
```
92147

93-
Diff file changes:
148+
## Diff files for untracked changes
94149

95150
```bash
96-
gitrack -p /home/elliot/gitrack -d # diff is not compatible with -s (summarized)
151+
gitrack -p /home/elliot -u -d # without -u, -d will not work
97152
```
98153

99-
Exclude directories:
154+
## Control number of workers/threads:
100155

101156
```bash
102-
gitrack -p /home/elliot -s -e "/home/elliot/.cache" -e "/home/elliot/.local"
157+
gitrack -p /home/elliot -u -w 6
103158
```
104159

105-
# Threads
160+
## Exclude directories:
106161

107-
> The use of threads is not really necessary in this type of tools, unless you have a very large file/folder system. Adding threads does not mean always better performance. I have included them in order to practice their use. **Max 5 threads, default 3**
162+
```bash
163+
gitrack -p /home/elliot -e "/home/elliot/.cache" -e "/home/elliot/.local" -u -w 6
164+
```
165+
166+
# About threads
108167

168+
> The use of threads is not really necessary in this type of tools, unless you have a very large file/folder system. Adding threads does not mean always better performance. I have included them in order to practice their use.
109169
110170
# Dev
111171

@@ -123,25 +183,24 @@ cd gitrack/
123183
docker run -it --rm -w /app -h gitrack --name gitrack -v $PWD:/app docker.io/rust:1.80.1-slim-bullseye
124184
```
125185

126-
# Example
186+
# Examples
127187

128-
Scan personal folder summarized:
188+
## Scan personal folder summarized
129189

130190
![example1](img/example1.png)
131191

132-
Scan specific folder with details:
192+
## Scan specific folder with details
133193

134194
![example2](img/example2.png)
135195

196+
## Diff files
197+
136198
![example3](img/example3.png)
137199

138200
# TODO
139201

140202
* Implement git commit scan for sensitive data using regex. Just for fun. Like gitleaks does.
141203

142-
# Links
143-
144-
* [Using cargo in CI](https://doc.rust-lang.org/cargo/guide/continuous-integration.html)
145204

146205
# License
147206

src/git_ops.rs

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

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

@@ -26,7 +25,6 @@ pub fn find_git_repos(
2625
start_path: &Path,
2726
exclude_dirs: &[String],
2827
num_threads: usize,
29-
verbose: bool,
3028
) -> Vec<PathBuf> {
3129
let pool = ThreadPool::new(num_threads); // Create a thread pool with the specified number of threads
3230
let (tx, rx) = mpsc::channel(); // Create a channel to send results from threads to the main thread
@@ -37,7 +35,7 @@ pub fn find_git_repos(
3735
if path.is_dir() && !is_excluded_dir(&entry, exclude_dirs) {
3836
let tx = tx.clone(); // Clone the sender to be used in the thread
3937
pool.execute(move || {
40-
check_and_send_repo(path, tx, verbose); // Check if the directory is a Git repository and send the path if it is
38+
check_and_send_repo(path, tx); // Check if the directory is a Git repository and send the path if it is
4139
});
4240
}
4341
}
@@ -48,6 +46,7 @@ pub fn find_git_repos(
4846
rx.into_iter().collect()
4947
}
5048

49+
// Check for untracked files in a Git repository
5150
pub fn check_untracked_files(repo_path: &Path) -> Result<Vec<String>, git2::Error> {
5251
let repo = Repository::open(repo_path)?;
5352
let mut status_options = StatusOptions::new();
@@ -57,14 +56,15 @@ pub fn check_untracked_files(repo_path: &Path) -> Result<Vec<String>, git2::Erro
5756
let mut untracked_files = Vec::new();
5857
for entry in statuses.iter() {
5958
let status = entry.status();
60-
if status.contains(Status::WT_NEW) {
59+
if status.is_index_new() || status.is_wt_new() || status.is_wt_modified() {
6160
let file_path = entry.path().unwrap_or("unknown file").to_string();
6261
untracked_files.push(file_path);
6362
}
6463
}
6564
Ok(untracked_files)
6665
}
6766

67+
// Show the diff for a specific file in a Git repository
6868
pub fn show_diff(repo_path: &Path, file: &str) -> io::Result<String> {
6969
let repo = Repository::open(repo_path).expect("Error opening repository");
7070
let mut diff_options = DiffOptions::new();

0 commit comments

Comments
 (0)