-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_lock
More file actions
executable file
·22 lines (22 loc) · 1.42 KB
/
Copy pathjson_lock
File metadata and controls
executable file
·22 lines (22 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
REPO_ROOT=$(git rev-parse --show-toplevel)
LOCK_FILE="$REPO_ROOT/lock.json"
OUTPUT_FILE="$REPO_ROOT/changes_output.json"
# Function to track changes and update JSON files
track_changes() {
local changed_files=("$@")
local updated_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Create lock data
local lock_data=$(jq -n --argjson files "$(printf '%s\n' "${changed_files[@]}" | jq -R . | jq -s .)" \
--arg updatedAt "$updated_at" '{modifiedFiles: $files, updatedAt: $updatedAt}')
echo "$lock_data" > "$LOCK_FILE"
# Create log data
local log_data=$(jq -n --argjson files "$(printf '%s\n' "${changed_files[@]}" | jq -R . | jq -s .)" \
--arg updatedAt "$updated_at" '{changedFiles: $files, updatedAt: $updatedAt}')
echo "$log_data" > "$OUTPUT_FILE"
}
# Create a post-commit hook
echo -e "#!/bin/bash\n\nREPO_ROOT=\$(git rev-parse --show-toplevel)\nLOCK_FILE=\"\$REPO_ROOT/lock.json\"\nOUTPUT_FILE=\"\$REPO_ROOT/changes_output.json\"\n\n$(declare -f track_changes)\n\ntrack_changes_after_commit() {\n local changed_files=(\$(git diff --name-only HEAD^ HEAD))\n if [ \${#changed_files[@]} -gt 0 ]; then\n track_changes \"\${changed_files[@]}\"\n fi\n}\n\ntrack_changes_after_commit" > "$REPO_ROOT/.git/hooks/post-commit"
# Make the post-commit hook executable
chmod +x "$REPO_ROOT/.git/hooks/post-commit"
echo "Post-commit hook created. Changes will be tracked automatically after each commit."