0

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?

1 Answer 1

2
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."
Sign up to request clarification or add additional context in comments.

4 Comments

Interesting ( and thank you for fast answer!). So git add "${TARGET_FILES[@]}" is the right way and also "makes the array into a string" like I had here: TARGET_FILES=$(IFS=$' '; echo "${TARGET_FILES[*]}"). Did I understand right?
@Rikard git add adds one file per argument so you have to pass multiple arguments, not a single string. Are you passing the command to ssh?
Ok, so "${TARGET_FILES[@]}" is parsed as multiple arguments? so I was doing a single argument with a string.
Yes it cleanly expands to multiple arguments. Even spaces on elements are preserved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.