Вы определяете $force
вне функции deleteCurrentBranch()
?
Если нет, $force
будет пустым, оператор if
(if [ $force == true ]
) будет читаться так:
if [ == true ]; then
Это не удастся,Вы можете решить эту проблему, добавив значение по умолчанию force=false
, чтобы оно не могло быть пустым, или вы должны добавить "
вокруг переменной $force
.Таким образом, bash всегда видит (пустую) строку: `
function deleteCurrentBranch() {
# Default
force="false"
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
echo -e "Current branch is '$branch'"
if [ "$branch" == "master" ]; then
echo "This script cannot be used to delete the master branch!"
return 1
fi
while getopts ":f" opt; do
case ${opt} in
f)
echo -e "Using the force!\n"
force=true
;;
*)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
echo "Using the force? $force"
if [ "$force" == "true" ]; then
echo -e "git reset --hard\n"
git reset --hard
else
if [ -z "$(git status --porcelain)" ]; then
force=false
else
echo -e "Working directory not clean and not using the force, exiting"
return 1
fi
fi
echo -e "git checkout master\n"
git checkout master
echo -e "\ngit pull"
git pull
deleteCommand="git branch -"
if [ "$force" == "true" ]; then
deleteCommand+="D"
else
deleteCommand+="d"
fi
deleteCommand+=" $branch"
echo -e $deleteCommand
eval "$deleteCommand"
echo -e "\ngit remote prune origin"
git remote prune origin
return 0
}