Skip to content

Commit e5dc3a8

Browse files
Full refactor
1 parent 7a213df commit e5dc3a8

14 files changed

+272
-39
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Build 🛠️
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
build:
12+
name: Build the binary
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Install latest nightly
18+
uses: actions-rs/toolchain@v1
19+
with:
20+
toolchain: stable
21+
override: true
22+
components: rustfmt, clippy
23+
24+
- name: Run cargo check
25+
uses: actions-rs/cargo@v1
26+
with:
27+
command: check
28+
29+
- name: Run cargo test
30+
uses: actions-rs/cargo@v1
31+
with:
32+
command: build --release

.github/workflows/release.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Build 📦
2+
3+
permissions:
4+
contents: write
5+
pull-requests: write
6+
7+
env:
8+
BIN_NAME: "gitrack"
9+
10+
on:
11+
push:
12+
# tags:
13+
# - 'v*.*.*'
14+
15+
jobs:
16+
release:
17+
name: Release - ${{ matrix.platform.release_for }}
18+
strategy:
19+
matrix:
20+
platform:
21+
- release_for: linux-arm64
22+
os: ubuntu-latest
23+
target: aarch64-unknown-linux-gnu
24+
bin: ${{ env.BIN_NAME }}
25+
command: build
26+
27+
- release_for: linux-amd64
28+
os: ubuntu-latest
29+
target: x86_64-unknown-linux-musl
30+
bin: ${{ env.BIN_NAME }}
31+
command: build
32+
33+
- release_for: darwin-amd64
34+
os: macOS-latest
35+
target: x86_64-apple-darwin
36+
bin: ${{ env.BIN_NAME }}
37+
command: build
38+
39+
- release_for: darwin-arm64
40+
os: macOS-latest
41+
target: aarch64-apple-darwin
42+
bin: ${{ env.BIN_NAME }}
43+
command: build
44+
45+
runs-on: ${{ matrix.platform.os }}
46+
steps:
47+
- name: Checkout
48+
uses: actions/checkout@v4
49+
50+
- name: Setup MUSL
51+
if: matrix.platform.os == 'ubuntu-latest'
52+
run: |
53+
sudo apt-get -qq install musl-tools
54+
55+
- name: Build binary
56+
uses: houseabsolute/actions-rust-cross@v0
57+
with:
58+
command: ${{ matrix.platform.command }}
59+
target: ${{ matrix.platform.target }}
60+
args: "--release"
61+
strip: true
62+
63+
- name: Set container tag
64+
id: set_tag
65+
run: |
66+
# Extract the tag name from the full reference
67+
IMAGE_TAG=$(echo "${{ github.ref }}" | sed 's|.*/||')
68+
echo "IMAGE_TAG=$IMAGE_TAG" >> "$GITHUB_ENV"
69+
70+
- name: Package zip
71+
run: |
72+
make package ARCH=${{ matrix.platform.release_for }} TARGET=${{ matrix.platform.target }} TAG=${{ env.IMAGE_TAG }}
73+
74+
- name: Release
75+
uses: softprops/action-gh-release@v2
76+
if: startsWith(github.ref, 'refs/tags/')
77+
with:
78+
files: |
79+
CHANGELOG.md
80+
LICENSE
81+
env:
82+
token: ${{ secrets.RELEASE_TOKEN }}
83+
generate_release_notes: true
84+
append_body: true
85+
draft: true #for testing

.github/workflows/test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Test 🛠️
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
test:
12+
name: Test
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Install latest nightly
18+
uses: actions-rs/toolchain@v1
19+
with:
20+
toolchain: stable
21+
override: true
22+
components: rustfmt, clippy
23+
24+
- name: Run cargo check
25+
uses: actions-rs/cargo@v1
26+
with:
27+
command: check
28+
29+
- name: Run cargo test
30+
uses: actions-rs/cargo@v1
31+
with:
32+
command: test
33+
34+
- name: Check fmt
35+
uses: actions-rs/cargo@v1
36+
with:
37+
command: fmt --check

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ Cargo.lock
1515

1616
# Jetbrains
1717
*.iml
18-
.idea/
18+
.idea/

.pre-commit-config.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ repos:
66
- id: check-case-conflict
77
- id: check-merge-conflict
88
- id: check-symlinks
9-
- id: check-yaml
109
- id: end-of-file-fixer
1110
- id: mixed-line-ending
1211
- id: trailing-whitespace
@@ -21,6 +20,6 @@ repos:
2120
- repo: https://github.com/doublify/pre-commit-rust
2221
rev: v1.0
2322
hooks:
24-
# - id: fmt
25-
# args: ['--verbose', '--']
26-
- id: cargo-check
23+
- id: fmt
24+
# args: ['--verbose', '--']
25+
- id: cargo-check

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gitrack"
3-
version = "0.1.0"
3+
version = "1.0.0"
44
edition = "2021"
55
authors = ["containerscrew [email protected]"]
66
repository = "https://github.com/containerscrew/gitrack"
@@ -10,4 +10,4 @@ clap = { version = "4.5.11", features = ["derive"] }
1010
colored = "2.1.0"
1111
git2 = "0.19.0"
1212
walkdir = "2.5.0"
13-
home = "0.5.9"
13+
home = "0.5.9"

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SHELL:=/bin/sh
2+
.PHONY: all
3+
4+
BINARY_NAME = gitrack
5+
6+
7+
help: ## this help
8+
@awk 'BEGIN {FS = ":.*?## "; printf "Usage:\n make \033[36m<target> \033[0m\n\nTargets:\n"} /^[a-zA-Z0-9_-]+:.*?## / {gsub("\\\\n",sprintf("\n%22c",""), $$2);printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
9+
10+
pre-commit: ## Run pre-commit
11+
pre-commit run -a
12+
13+
package: ## Package binary with zip
14+
zip -j ${BINARY_NAME}-$(ARCH)-$(TAG).zip target/$(TARGET)/release/${BINARY_NAME}
15+
16+
generate-changelog: ## Generate changelog using git cliff
17+
git cliff --output CHANGELOG.md
18+
19+
build: ## Build binary
20+
cargo build --release
21+
22+
install: ## Install binary
23+
cargo install --path .

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Open a terminal and run:
1313
```shell
1414
git clone https://github.com/containerscrew/gitrack
1515
cd gitrack
16-
cargo install --path .
16+
make install
1717
```
1818

1919
# Usage
@@ -51,7 +51,8 @@ Scan specific folder with details:
5151

5252
* Implement git commit scan for sensitive data using regex. Just for fun. Like gitleaks does.
5353
* Compile the binary for linux/darwin arm64/amd64.
54+
* Support diff files.
5455

5556
# License
5657

57-
[License](./LICENSE)
58+
[License](./LICENSE)

cliff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ conventional_commits = false
2525
filter_unconventional = false
2626
split_commits = false
2727
tag_pattern = "^v[0-9]+\\.[0-9]+\\.[0-9]+$"
28-
sort_commits = "oldest"
28+
sort_commits = "oldest"

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use clap::{Parser};
21
use crate::utils::default_home_dir;
2+
use clap::Parser;
33

44
#[derive(Parser, Debug)]
55
#[clap(
@@ -24,7 +24,7 @@ pub struct Args {
2424
short = 's',
2525
long = "summary",
2626
help = "Show only repositories without listing untracked files",
27-
required = false,
27+
required = false
2828
)]
2929
pub summary: bool,
30-
}
30+
}

src/git_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::path::{Path, PathBuf};
21
use git2::{Repository, StatusOptions};
2+
use std::path::{Path, PathBuf};
33
use walkdir::WalkDir;
44

55
pub fn find_git_repos(start_path: &Path) -> Vec<PathBuf> {
@@ -28,4 +28,4 @@ pub fn check_untracked_files(repo_path: &Path) -> Result<Vec<String>, git2::Erro
2828
}
2929
}
3030
Ok(untracked_files)
31-
}
31+
}

src/main.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
use crate::git_ops::{check_untracked_files, find_git_repos};
12
use clap::Parser;
23
use cli::Args;
34
use colored::*;
4-
use std::path::{Path, PathBuf};
5-
use crate::git_ops::{check_untracked_files, find_git_repos};
5+
use std::path::Path;
6+
use std::sync::mpsc;
7+
use std::thread;
68

79
mod cli;
810
mod git_ops;
11+
mod test;
912
mod utils;
1013

1114
// Macro print orange
@@ -23,43 +26,54 @@ macro_rules! println_light_orange {
2326
}
2427

2528
fn main() {
26-
// Init the CLI using clap
29+
// Initialize the CLI arguments
2730
let args = Args::parse();
2831

2932
println!("{}", "----->  Starting gitrack  <-----".green());
3033

31-
// Find .git repos in the given path
34+
// Find .git repos in the specified path
3235
let start_path = Path::new(&args.path);
3336
let git_repos = find_git_repos(start_path);
3437

35-
let mut results = Vec::new();
38+
// Create a channel to send the results back to the main thread
39+
let (tx, rx) = mpsc::channel();
3640

37-
// For every repo, check for untracked files
38-
for repo_path in &git_repos {
39-
match check_untracked_files(&repo_path) {
40-
Ok(untracked_files) => {
41-
if !untracked_files.is_empty() {
42-
results.push((repo_path.clone(), untracked_files));
41+
// Spawn a thread for each repo to check for untracked files
42+
for repo_path in git_repos {
43+
let tx = tx.clone();
44+
thread::spawn(move || {
45+
match check_untracked_files(&repo_path) {
46+
Ok(untracked_files) => {
47+
if !untracked_files.is_empty() {
48+
// Send the results back to the main thread
49+
tx.send((repo_path.clone(), untracked_files)).unwrap();
50+
}
4351
}
52+
Err(e) => eprintln!("{}: {}", "Error checking repository".red(), e),
4453
}
45-
Err(e) => eprintln!("{}: {}", "Error checking repository".red(), e),
46-
}
54+
});
4755
}
4856

49-
// Print the results, if any
50-
match results.is_empty() {
51-
true => println_orange!("-----> No git repos found in {}", start_path.display()),
52-
false => print_results(results, args.summary),
53-
}
54-
}
57+
// Close the sending side of the channel
58+
drop(tx);
5559

56-
fn print_results(results: Vec<(PathBuf, Vec<String>)>, summary: bool) {
57-
for (repo_path, untracked_files) in results {
60+
// Print the results as they arrive
61+
let mut has_results = false;
62+
while let Ok((repo_path, untracked_files)) = rx.recv() {
63+
has_results = true;
5864
println_orange!("Untracked files in: {}", repo_path.display());
59-
if !summary {
65+
if !args.summary {
6066
for file in untracked_files {
6167
println_light_orange!(" - {}", file);
6268
}
6369
}
6470
}
65-
}
71+
72+
// Print a message if no results were found
73+
if !has_results {
74+
println_orange!(
75+
"-----> There are no changes to git in {}",
76+
start_path.display()
77+
);
78+
}
79+
}

0 commit comments

Comments
 (0)