Skip to content

Commit 2f8e262

Browse files
committed
chore: add pre-push git hooks and fix linting errors
- Add pre-push hook to run formatting, clippy, and tests automatically - Create install-git-hooks.sh script for easy hook installation - Update README with development section and hook setup instructions - Fix clippy warnings: - Change copy_dir_recursive to static method (only_used_in_recursion) - Replace manual modulo checks with is_multiple_of() - Fix formatting issues with cargo fmt
1 parent fd4b7ba commit 2f8e262

4 files changed

Lines changed: 164 additions & 30 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ cd crates/cleanser-gui/ui && npm install && cd ..
150150
cargo tauri build
151151
```
152152

153+
## Development
154+
155+
**Setup git hooks:**
156+
```bash
157+
# Install pre-push hooks to run linting and tests automatically
158+
./scripts/install-git-hooks.sh
159+
```
160+
161+
The pre-push hook will automatically run:
162+
- `cargo fmt --all -- --check` (formatting)
163+
- `cargo clippy --workspace --exclude cleanser-gui --all-targets -- -D warnings` (linting)
164+
- `cargo test --workspace --exclude cleanser-gui --verbose` (tests)
165+
166+
To skip the hook (not recommended): `git push --no-verify`
167+
153168
## Safety
154169

155170
- Always preview with `--dry-run` first

‎crates/cleanser-core/src/deletion/trash.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl TrashManager {
223223

224224
// Fall back to copy + delete (works across filesystems)
225225
if source.is_dir() {
226-
self.copy_dir_recursive(source, dest)?;
226+
Self::copy_dir_recursive(source, dest)?;
227227
fs::remove_dir_all(source)?;
228228
} else {
229229
fs::copy(source, dest)?;
@@ -234,7 +234,7 @@ impl TrashManager {
234234
}
235235

236236
/// Recursively copy a directory
237-
fn copy_dir_recursive(&self, src: &Path, dst: &Path) -> Result<()> {
237+
fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
238238
fs::create_dir_all(dst)?;
239239

240240
for entry in fs::read_dir(src)? {
@@ -243,7 +243,7 @@ impl TrashManager {
243243
let dst_path = dst.join(entry.file_name());
244244

245245
if src_path.is_dir() {
246-
self.copy_dir_recursive(&src_path, &dst_path)?;
246+
Self::copy_dir_recursive(&src_path, &dst_path)?;
247247
} else {
248248
fs::copy(&src_path, &dst_path)?;
249249
}

‎crates/cleanser-core/src/scanner.rs‎

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ pub fn scan_with_progress(
8787
current: None,
8888
total: None,
8989
});
90-
let duplicate_items = find_duplicates(&config.paths, config.max_depth.unwrap_or(6), progress)?;
90+
let duplicate_items =
91+
find_duplicates(&config.paths, config.max_depth.unwrap_or(6), progress)?;
9192
items.extend(duplicate_items);
9293
}
9394

@@ -151,13 +152,16 @@ fn scan_using_map(
151152
let current = processed_count.fetch_add(1, Ordering::SeqCst) + 1;
152153

153154
// Report progress every 10 directories, with mutex to prevent out-of-order reporting
154-
if current % 10 == 0 || current == 1 || current == total_dirs {
155+
if current.is_multiple_of(10) || current == 1 || current == total_dirs {
155156
if let Ok(mut last) = last_reported.lock() {
156157
if current > *last {
157158
*last = current;
158159
progress.on_scan_progress(ScanProgress {
159160
phase: ScanPhase::Scanning,
160-
message: format!("Scanning mapped directories... {}/{}", current, total_dirs),
161+
message: format!(
162+
"Scanning mapped directories... {}/{}",
163+
current, total_dirs
164+
),
161165
current: Some(current),
162166
total: Some(total_dirs),
163167
});
@@ -406,7 +410,10 @@ fn find_duplicates(
406410
.into_iter()
407411
.filter(|(_, paths)| paths.len() >= 2)
408412
.collect();
409-
let total_files: usize = potential_duplicates.iter().map(|(_, paths)| paths.len()).sum();
413+
let total_files: usize = potential_duplicates
414+
.iter()
415+
.map(|(_, paths)| paths.len())
416+
.sum();
410417

411418
progress.on_scan_progress(ScanProgress {
412419
phase: ScanPhase::FindingDuplicates,
@@ -419,29 +426,37 @@ fn find_duplicates(
419426
let partial_hash_groups_sync = Mutex::new(HashMap::new());
420427
let last_reported = Mutex::new(0u64);
421428

422-
potential_duplicates.par_iter().for_each(|(size, file_paths)| {
423-
for path in file_paths {
424-
if let Ok(partial) = compute_partial_hash(path, *size) {
425-
if let Ok(mut groups) = partial_hash_groups_sync.lock() {
426-
groups.entry(partial).or_insert_with(Vec::new).push(path.clone());
429+
potential_duplicates
430+
.par_iter()
431+
.for_each(|(size, file_paths)| {
432+
for path in file_paths {
433+
if let Ok(partial) = compute_partial_hash(path, *size) {
434+
if let Ok(mut groups) = partial_hash_groups_sync.lock() {
435+
groups
436+
.entry(partial)
437+
.or_insert_with(Vec::new)
438+
.push(path.clone());
439+
}
427440
}
428-
}
429-
let current = processed.fetch_add(1, Ordering::SeqCst) + 1;
430-
if current % 10 == 0 || current == total_files as u64 {
431-
if let Ok(mut last) = last_reported.lock() {
432-
if current > *last {
433-
*last = current;
434-
progress.on_scan_progress(ScanProgress {
435-
phase: ScanPhase::FindingDuplicates,
436-
message: format!("Computing partial hashes... {}/{}", current, total_files),
437-
current: Some(current),
438-
total: Some(total_files as u64),
439-
});
441+
let current = processed.fetch_add(1, Ordering::SeqCst) + 1;
442+
if current.is_multiple_of(10) || current == total_files as u64 {
443+
if let Ok(mut last) = last_reported.lock() {
444+
if current > *last {
445+
*last = current;
446+
progress.on_scan_progress(ScanProgress {
447+
phase: ScanPhase::FindingDuplicates,
448+
message: format!(
449+
"Computing partial hashes... {}/{}",
450+
current, total_files
451+
),
452+
current: Some(current),
453+
total: Some(total_files as u64),
454+
});
455+
}
440456
}
441457
}
442458
}
443-
}
444-
});
459+
});
445460

446461
let partial_hash_groups = partial_hash_groups_sync.into_inner().unwrap();
447462

@@ -454,7 +469,10 @@ fn find_duplicates(
454469

455470
progress.on_scan_progress(ScanProgress {
456471
phase: ScanPhase::FindingDuplicates,
457-
message: format!("Computing full hashes for {} candidates...", total_candidates),
472+
message: format!(
473+
"Computing full hashes for {} candidates...",
474+
total_candidates
475+
),
458476
current: Some(0),
459477
total: Some(total_candidates as u64),
460478
});
@@ -467,17 +485,23 @@ fn find_duplicates(
467485
for path in file_paths {
468486
if let Ok(hash) = hash_file_full(path) {
469487
if let Ok(mut groups) = full_hash_groups_sync.lock() {
470-
groups.entry(hash).or_insert_with(Vec::new).push(path.clone());
488+
groups
489+
.entry(hash)
490+
.or_insert_with(Vec::new)
491+
.push(path.clone());
471492
}
472493
}
473494
let current = processed.fetch_add(1, Ordering::SeqCst) + 1;
474-
if current % 5 == 0 || current == total_candidates as u64 {
495+
if current.is_multiple_of(5) || current == total_candidates as u64 {
475496
if let Ok(mut last) = last_reported.lock() {
476497
if current > *last {
477498
*last = current;
478499
progress.on_scan_progress(ScanProgress {
479500
phase: ScanPhase::FindingDuplicates,
480-
message: format!("Computing full hashes... {}/{}", current, total_candidates),
501+
message: format!(
502+
"Computing full hashes... {}/{}",
503+
current, total_candidates
504+
),
481505
current: Some(current),
482506
total: Some(total_candidates as u64),
483507
});

‎scripts/install-git-hooks.sh‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
3+
# Script to install git hooks for the cleanser project
4+
# Run this after cloning the repository
5+
6+
set -e
7+
8+
# Colors for output
9+
GREEN='\033[0;32m'
10+
YELLOW='\033[1;33m'
11+
NC='\033[0m' # No Color
12+
13+
echo -e "${YELLOW}Installing git hooks...${NC}"
14+
15+
# Get the root directory of the git repository
16+
GIT_ROOT=$(git rev-parse --show-toplevel)
17+
cd "$GIT_ROOT"
18+
19+
# Create hooks directory if it doesn't exist
20+
mkdir -p .git/hooks
21+
22+
# Copy pre-push hook
23+
cat > .git/hooks/pre-push << 'EOF'
24+
#!/bin/bash
25+
26+
# Pre-push hook to run linting and tests before pushing
27+
# This mirrors the CI checks in .github/workflows/ci.yml
28+
29+
set -e
30+
31+
echo "🔍 Running pre-push checks..."
32+
echo ""
33+
34+
# Colors for output
35+
RED='\033[0;31m'
36+
GREEN='\033[0;32m'
37+
YELLOW='\033[1;33m'
38+
NC='\033[0m' # No Color
39+
40+
# Check if we're in a git repository
41+
if ! git rev-parse --git-dir > /dev/null 2>&1; then
42+
echo -e "${RED}✗ Not in a git repository${NC}"
43+
exit 1
44+
fi
45+
46+
# Get the root directory of the git repository
47+
GIT_ROOT=$(git rev-parse --show-toplevel)
48+
cd "$GIT_ROOT"
49+
50+
echo -e "${YELLOW}📋 Step 1/3: Checking formatting...${NC}"
51+
if cargo fmt --all -- --check; then
52+
echo -e "${GREEN}✓ Formatting check passed${NC}"
53+
else
54+
echo -e "${RED}✗ Formatting check failed${NC}"
55+
echo -e "${YELLOW}Run 'cargo fmt --all' to fix formatting issues${NC}"
56+
exit 1
57+
fi
58+
echo ""
59+
60+
echo -e "${YELLOW}🔧 Step 2/3: Running clippy...${NC}"
61+
if cargo clippy --workspace --exclude cleanser-gui --all-targets -- -D warnings; then
62+
echo -e "${GREEN}✓ Clippy passed${NC}"
63+
else
64+
echo -e "${RED}✗ Clippy found issues${NC}"
65+
echo -e "${YELLOW}Fix the clippy warnings above before pushing${NC}"
66+
exit 1
67+
fi
68+
echo ""
69+
70+
echo -e "${YELLOW}🧪 Step 3/3: Running tests...${NC}"
71+
if cargo test --workspace --exclude cleanser-gui --verbose; then
72+
echo -e "${GREEN}✓ Tests passed${NC}"
73+
else
74+
echo -e "${RED}✗ Tests failed${NC}"
75+
echo -e "${YELLOW}Fix the failing tests before pushing${NC}"
76+
exit 1
77+
fi
78+
echo ""
79+
80+
echo -e "${GREEN}✅ All pre-push checks passed!${NC}"
81+
echo -e "${GREEN}🚀 Pushing to remote...${NC}"
82+
echo ""
83+
84+
exit 0
85+
EOF
86+
87+
# Make hook executable
88+
chmod +x .git/hooks/pre-push
89+
90+
echo -e "${GREEN}✓ Git hooks installed successfully!${NC}"
91+
echo ""
92+
echo "The following hooks have been installed:"
93+
echo " - pre-push: Runs formatting, clippy, and tests before pushing"
94+
echo ""
95+
echo "To skip the pre-push hook (not recommended), use: git push --no-verify"

0 commit comments

Comments
 (0)