-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit-push-working-directory.sh
executable file
·85 lines (67 loc) · 2.17 KB
/
split-push-working-directory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Set the maximum commit size in bytes (1.5GB = 1.5 * 1024 * 1024 * 1024 bytes)
MAX_COMMIT_SIZE=$((100 * 1024 * 1024))
DATE=$(date +%d%m%Y)
COMMIT_INDEX=1
# Function to check if a file contains sensitive data
is_sensitive_file() {
local file="$1"
# Only check JSON files
if [[ "$file" == *.json ]]; then
if grep -q '"name": *".env"' "$file"; then
echo "Found .env in JSON: $file"
echo "$file" >> data/.gitignore
return 0
fi
fi
return 1
}
commit_and_push() {
local commit_message="updates data directory ${DATE}:V${COMMIT_INDEX}"
# Check if there are staged changes
if git diff --cached --quiet; then
echo "No changes to commit for: $commit_message. Skipping..."
return
fi
echo "Committing changes."
git commit -m "$commit_message"
git push origin HEAD:$(git rev-parse --abbrev-ref HEAD)
COMMIT_INDEX=$((COMMIT_INDEX + 1))
}
stage_and_commit() {
local staged_size=0
while IFS= read -r file; do
# Skip if file doesn't exist
if [[ ! -e "$file" ]]; then
continue
fi
# Skip if file contains sensitive data
if is_sensitive_file "$file"; then
echo "Skipping file with sensitive data: $file"
continue
fi
# Get file size in bytes
file_size=$(du -b "$file" | cut -f1)
# Stage the file
git add -- "$file"
staged_size=$((staged_size + file_size))
echo "Currently staged size: $staged_size bytes for file: $file"
if (( staged_size > MAX_COMMIT_SIZE )); then
echo "Staged changes exceed max size. Committing and resetting."
commit_and_push
git reset -- "$file"
staged_size=0
fi
done < <(git ls-files --modified --others --exclude-standard)
# Commit any remaining staged files
if ! git diff --cached --quiet; then
commit_and_push
fi
}
# Check if there are changes to commit
if git diff --quiet && git diff --cached --quiet; then
echo "No changes to commit. Exiting."
exit 0
fi
stage_and_commit
echo "All changes committed."