0

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) ###
2
  • What is the precise error message? Commented Mar 8, 2015 at 1:07
  • ".git/hooks/post-commit: 9: .git/hooks/post-commit: Syntax error: redirection unexpected" Commented Mar 8, 2015 at 1:49

1 Answer 1

2

The "here-string" syntax (<<<) is a bash extension; it is not present in basic shells. (It's not strictly bash; many other shells use it, too.) The same is true of arrays, and the readarray command.

So you need to make sure that you are using bash to run the script. Your shebang line (#!/bin/sh) tells the system to use a basic shell, and if the basic shell on your system is not bash, it may well not have <<<.

Try changing the shebang line to

#!/bin/bash

(Make sure that is the correct path to bash. You can verify with which bash.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.