Skip to content

Commit 9730554

Browse files
emmaaiEmma Ai
andauthored
Add pre-commit hook to avoid duplicates (#1314)
* add pre-commit hook to avoid duplidate ipynb * add test1 * check against local files * skip duplicate check in ci --------- Co-authored-by: Emma Ai <[email protected]>
1 parent 253e23d commit 9730554

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

.githooks/check_duplicate_ipynb.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
echo "Checking for duplicate .ipynb filenames..."
4+
5+
# Get all existing and staged .ipynb files
6+
all_files=$(git ls-files | grep '\.ipynb$' || true)
7+
staged_files=$(git diff --cached --name-only --diff-filter=A | grep '\.ipynb$' || true)
8+
9+
# Combine them to ensure we check for duplicates globally
10+
combined_files=$(echo -e "$all_files\n$staged_files" | sort -u)
11+
12+
if [[ -z "$combined_files" ]]; then
13+
exit 0 # No .ipynb files found, allow commit
14+
fi
15+
16+
# Extract only filenames (ignore paths)
17+
filenames=$(basename -a $combined_files)
18+
19+
# Check for duplicates
20+
duplicates=$(echo "$filenames" | sort | uniq -d)
21+
22+
if [[ -n "$duplicates" ]]; then
23+
echo "Duplicate .ipynb filenames detected in repository and staged changes:"
24+
echo "$duplicates"
25+
echo "Commit rejected! Ensure unique .ipynb filenames."
26+
exit 1 # Prevent commit
27+
fi
28+
29+
exit 0 # Allow commit

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ci:
2+
skip: [check-duplicate-ipynb]
3+
4+
repos:
5+
- repo: local
6+
hooks:
7+
- id: check-duplicate-ipynb
8+
name: Check for duplicate .ipynb filenames
9+
entry: bash .githooks/check_duplicate_ipynb.sh
10+
language: system
11+
pass_filenames: false
12+
stages: [pre-commit]

0 commit comments

Comments
 (0)