Skip to content

Instantly share code, notes, and snippets.

@bukowa
Last active June 15, 2024 00:34
Show Gist options
  • Save bukowa/f2346d23a7b87f179756471d3bf9472e to your computer and use it in GitHub Desktop.
Save bukowa/f2346d23a7b87f179756471d3bf9472e to your computer and use it in GitHub Desktop.
grab github changed files to json array with jq
#!/bin/bash -l
set -euxo pipefail
# Source first argument if it exists
if [ -n "${1-}" ]; then
set -o allexport
source "$1"
set +o allexport
fi
# Build json array of untracked and tracked files
GIT_UNTRACKED_CHANGES=$(git ls-files --others --exclude-standard | jq -R -s -c 'split("\n")[:-1]')
GIT_TRACKED_CHANGES=$(git diff --name-only | jq -R -s -c 'split("\n")[:-1]')
# Group the untracked and tracked files
GIT_CHANGED_FILES=$(echo "$GIT_UNTRACKED_CHANGES" "$GIT_TRACKED_CHANGES" | jq -s -c 'add | unique')
# Check if the changelog file has changed
GIT_HAS_CHANGELOG_CHANGED=$(echo $GIT_CHANGED_FILES | jq -r --arg target "$GIT_CLIFF_OUTPUT" '.[] | select(. == $target)')
# Exit if no changes are detected
if [ -z "$GIT_HAS_CHANGELOG_CHANGED" ]; then
echo "No changes detected after generating the changelog."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment