I'm parsing the output of "git log -1 --name-status" to grab the file extension for each file changed in the last commit.
I tried to run it in the git post-commit hook, but readarray line (#9) is throwing a syntax error.
.git/hooks/post-commit: 9: .git/hooks/post-commit: Syntax error: redirection unexpected
Can you guys point me in the right direction? What am I doing wrong and why the heck is it not working on the post-commit? Excuse my ignorance... I don't have much experience with shell scripting.
#!/bin/sh
### git-stats hook (begin) ###
# Copy last commit hash to clipboard on commit
commit_hash=$(git rev-parse HEAD)
repo_url=$(git config --get remote.origin.url)
commit_date=$(git log -1 --format=%cd)
commit_changes=$(git log -1 --name-status)
readarray -t log_lines <<< "$commit_changes"
fileRegex='(.*)\.'
file_changed_start=6;
file_changed_line="${log_lines[file_changed_start]}"
declare -A languages;
# Step through each line that passes the regex
while [[ "${file_changed_line}" =~ ${fileRegex} ]]; do
# Store the file extension
parsePathAndFilename="${BASH_REMATCH[0]}"
fileExtention=${file_changed_line#${parsePathAndFilename}};
# Add 1 to the language that is already defined
let languages[${fileExtention}]++;
file_changed_start=$(($file_changed_start + 1))
file_changed_line="${log_lines[file_changed_start]}"
done
# Create the JSON output
fileTypes="{";
fileTypeTotal=${#languages[@])};
assocKeys=(${!languages[@]});
for changed in "${!languages[@]}"
do
if [[ "$changed" == "${assocKeys[fileTypeTotal-1]}" ]]
then
fileTypes+="\"$changed\" : ${languages[$changed]}";
else
fileTypes+="\"$changed\" : ${languages[$changed]},";
fi
done
# close off the json
fileTypes+="}";
echo $fileTypes
commit_data="\"{ \"date\": \"$commit_date\", \"url\": \"$repo_url\", \"hash\": \"$commit_hash\", \"languages": \"$fileTypes\" }\""
git-stats --record "${commit_data}"
### git-stats hook (end) ###