Skip to content

Instantly share code, notes, and snippets.

@felicianotech
Created June 15, 2016 04:51
Show Gist options
  • Save felicianotech/12a4b38c594fcf3d3999de2c01f7d05e to your computer and use it in GitHub Desktop.
Save felicianotech/12a4b38c594fcf3d3999de2c01f7d05e to your computer and use it in GitHub Desktop.
An example Git hook to automate when to use [skip ci] in commit messages. This particular hook uses patterns from a file called .ciignore
#!/usr/bin/env bash
if [[ ! -a .ciignore ]]; then
exit # If .ciignore doesn't exists, just quit this Git hook
fi
# Load in every file that will be changed via this commit into an array
changes=( `git diff --name-only --cached` )
# Load the patterns we want to skip into an array
mapfile -t blacklist < .ciignore
for i in "${blacklist[@]}"
do
# Remove the current pattern from the list of changes
changes=( ${changes[@]/$i/} )
if [[ ${#changes[@]} -eq 0 ]]; then
# If we've exhausted the list of changes before we've finished going
# through patterns, that's okay, just quit the loop
break
fi
done
if [[ ${#changes[@]} -gt 0 ]]; then
# If there's still changes left, then we have stuff to build, leave the commit alone.
exit
fi
# Prefix the commit message with "[skip ci]"
sed -i '1s/^/[skip ci] /' "$1"
@Tchangang
Copy link

Tchangang commented Nov 9, 2019

Got many problems with sed -i. I replace it and everything seems to be ok.

#!/usr/bin/env bash
if [[ ! -a .ciignore ]]; then
	exit # If .ciignore doesn't exists, just quit this Git hook
fi

# Load in every file that will be changed via this commit into an array
changes=( `git diff --name-only --cached` )

# Load the patterns we want to skip into an array
mapfile -t blacklist < .ciignore

for i in "${blacklist[@]}"
do
	# Remove the current pattern from the list of changes
	changes=( ${changes[@]/$i/} )
	if [[ ${#changes[@]} -eq 0 ]]; then
		# If we've exhausted the list of changes before we've finished going
		# through patterns, that's okay, just quit the loop
		break
	fi
done

if [[ ${#changes[@]} -gt 0 ]]; then
	# If there's still changes left, then we have stuff to build, leave the commit alone.
	exit
fi
# Prefix the commit message with "[skip ci]"
commitContent=$(<$1)
echo "[skip ci] ${commitContent}" > $1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment