Rikard
Rikard

Reputation: 7805

git add multiple files from bash array

I want to add filenames/path to a git commit. I have the file path in a array but when i commit I get:

fatal: pathspec 'dist/core.js dist/style.css bower.json Source/core.js' did not match any files

Earlier in my script I have:

DIR=`dirname "$0"`
for file in "${COREFILES[@]}"; do echo $file="$DIR/$file"; done
for file in "${JSONFILES[@]}"; do echo $file="$DIR/$file"; done
TARGET_FILES=$(IFS=$' '; echo "${TARGET_FILES[*]}")
TARGET_FILES=( "${COREFILES[@]}" "${JSONFILES[@]}" )

and the error line is:

sh -c "cd $DIR && git add '$TARGET_FILES' && git commit -qm 'Current tag $TAG$SUFFIX.'"

Other parts of the script that change the source code in files works good. When I do echo $TARGET_FILES I get a string, space separated and looks all ok...

Any pointer what I could be missing? or how to do this otherwise?

Upvotes: 0

Views: 550

Answers (1)

konsolebox
konsolebox

Reputation: 75488

git add '$TARGET_FILES'

should be

git add "${TARGET_FILES[@]}"

And this line does nothing and is probably not helpful:

TARGET_FILES=$(IFS=$' '; echo "${TARGET_FILES[*]}")

Also I don't think you should use sh -c:

cd "$DIR" && git add "${TARGET_FILES[@]}" && git commit -qm "Current tag $TAG$SUFFIX."

As a whole:

DIR=$(dirname "$0")
for file in "${COREFILES[@]}"; do echo "$file=$DIR/$file"; done
for file in "${JSONFILES[@]}"; do echo "$file=$DIR/$file"; done
TARGET_FILES=( "${COREFILES[@]}" "${JSONFILES[@]}" )
cd "$DIR" && git add "${TARGET_FILES[@]}" && git commit -qm "Current tag $TAG$SUFFIX."

Upvotes: 2

Related Questions