I have this function:
git_commit () {
if [[ $# -eq 0 || ( $# -eq 1 && $1 == '.' ) ]]
then
git commit .
else
commit_string=''
for var in "$@"
do
! [[ $var =~ ^[0-9]+$ ]] && { echo "Supply integer values from the menu only. Nothing added." && return; }
file="$(git status -s|awk 'FNR == '$var'{$1="";print $0}')";
file=$(sed -e 's/^[[:space:]]*//' <<<"$file")
new_file="${file} "
commit_string+=${new_file}
done
echo $commit_string;
read -ep "Commit description: " desc
commit_string=${commit_string##*( )}
commit_string="${commit_string//\"}"
git commit -m "$desc" ${commit_string}
fi
git_short_status
}
The function generates a string comprised of filenames that I select with a menu. Example:
get_commit 1 3 5
In this case, it would run:
git commit -m 'description from prompt' file_1 file file_3 file_5
I'm having a tough time figuring out how to get the function to handle files with spaces in them. The problem is in this line:
git commit -m "$desc" ${commit_string}
This line works fine for multiple files with no spaces. However, it chokes on a single file with spaces in it. So I changed it to:
git commit -m "$desc" "${commit_string}"
Like this, it works for files with spaces in it but chokes when I have multiple files (I get an error that it doesn't recognize the file path.