Why does my command fail even if I use `|| echo ""` - bash

I run my bash script with -e
set -e
git push --delete sometag || echo "git delete tag failed"
echo "some other commands"
However git push returns a fatal error, and the rest of the script is not executed (it does execute echo "git delete tag failed")
I thought that was the purpose of using ||. Is a "fatal" error any different?
Note: I have seen other questions but they say that || is the solution

Related

Get commit message in Git hooks

I would like to create commit-msg hook to check the commit message with some regex. So I wrote this bash script in the hooks folder:
#!/bin/sh
valid_commit_msg_regex="^[0-9]+:[a-zA-Z0-9-_ ]{20,150}$"
message="This commit violates the commit message rules. Please rename your commit."
if [[ ! $(cat $1) =~ $valid_commit_msg_regex ]]
then
echo "$message"
exit 1
fi
exit 0
But when I try to write git commit -m "9: texttexttexttexttexttexttexttext" it doesn't work properly and I get the error message from script.
Does anyone has an idea what's gone wrong?
I see two issues:
Double brackets [[ comes from bash. Change shebang to #!/bin/bash
in [a-zA-Z0-9-_ ] you have to escape - with \-

use bash case statement in gitlab ci/cd

so i have a pipeline that checks 2 repos every 24 hours and pushes changes to a 3rd repo. the pipeline looks like this:
- git fetch $CHEATSHEETS
- git fetch $BASH_ONELINER
- git add -A
- git commit -m "Update has been successful" || echo "error"
- git push $TOKEN_AND_REPO HEAD:master
my problem with that is i'm never going to see any error in the pipeline if there's an error like a merge conflict, for example. so i was thinking to use a case for comitting, something like this:
TEST=2; case "${TEST}" in "1") echo "one";; "2") echo "The commit has failed";; *) echo "Error";; esac
my question is would something like this work in gitlab ci/cd pipeline and if so - how should it be implemented?
thanks in advance.
You seem to want to do:
- git fetch $CHEATSHEETS
- git fetch $BASH_ONELINER
- git add -A
- if git diff-index --cached --quiet HEAD; then
echo "Nothing to commit... exiting" &&
exit 0
fi
- git commit -m "Update has been successful"
- git push $TOKEN_AND_REPO HEAD:master
which is unrelated to using case. You can use case normally in gitlab-ci pipeline, just like in shell. See How do I programmatically determine if there are uncommitted changes?
You can try something like this, using a subshell or command groups:
git commit -m "Update has been successful" || {
case $? in
1) echo "one" ;;
2) echo "commit failed" ;;
*) echo "other error" ;;
esac;
}

Right string expansion in Bash (for shell script)

I'm experimenting a little bit with a shell script, which should run git commands for multiple repositories on the same level. This project structure might be a bad idea, but this is another story.
Everything works fine until I've run into this problem:
DETAIL="test test" && command="commit -m '${DETAIL}'" && echo $(git ${command})
# -> error: pathspec 'test'' did not match any file(s) known to git.
I've also tried other opportunities like
DETAIL="test test" && command="commit -m ${DETAIL}" && echo $(git ${command})
DETAIL="test test" && command="commit -m $DETAIL" && echo $(git ${command})
All give the same result (see above). I've also scanned these docs about string expansion, but I don't have the problem, that the variables/strings might be null or undefined. The last echo is not the problem, you can also store the result of $(git status) in a variable and echo this one (my way in the script).
I know, there are similar questions, but I did not found a similar scenario yet, since I'm just dealing with simple and non-null strings, but with (too?) many quotes.
Interesting variant:
DETAIL="test test" && command="commit -m '${DETAIL}'" && echo $("git ${command}")
# -> git commit -m 'test test': command not found # WHAT?
Also interesting, just:
command="commit -m 'test'" && echo $(git ${command})
works fine.
Use bash arrays with proper quoting...
DETAIL="test test" && command=(commit -m "$DETAIL") && git "${command[#]}"
To your code:
echo "$(command)" is the same as command (ok, trailing empty newlines are removed)
"command blabla" does not execute file command with the first argument blabla. It will execute a filename named exactly with space command blabla.
Inside $("git ${command}") you want to execute a filename named git commit -m 'test test' (exactly, this is the whole filename name, with spaces, after ${command} is expanded). As on you system there is no file named git commit -m 'test test' bash returns command not found.

How to make a git pre-commit hook that checks the commit message?

I have a git commit hook script, that checks the commit message, and if the message does not contain the word "updated", the script should reject the commit.
#!/bin/bash
read -p "Enter a commit message: " message
if [[ ${message} != *"updated"* ]];then
echo "Your commit message must contain the word 'updated'"
else
git commit -m "$message"
fi
How to make this hook automatically execute if I try to push some files in my local repo using the command
git commit -m "updated:something"
My idea is to make it not like "run this script to do commit", but rather when you open the console and try to make a commit and entering the commit message, the script will check your commit message automatically and pass it or reject it.
Taking commit-msg for example.
#!/bin/bash
MSG="$1"
if ! grep -qE "updated" "$MSG";then
cat "$MSG"
echo "Your commit message must contain the word 'updated'"
exit 1
fi
chmod 755 commit-msg and copy it as .git/hooks/commit-msg.

unexpected operator [: git: in bourne shell script 'if' conditional statement

I've watched an excellent shell scripting course through a multitude of videos. Now that I think I am fairly familiar with the Bourne shell, I decided to write my first shell script.
Script goal: check if git working directory is clean. If so, overwrite working directory to a branch named deployment. Finally, push the deployment branch to origin.
I ended up with this code:
#!/bin/sh
######################################################
# Deploys working directory to git deployment branch.
# Requires that the working directory is clean.
######################################################
#check if the working directory is clean
if [ git diff-index --quiet HEAD ]
then
if [ git branch -f deployment ]
then
if [ git push origin deployment ]
then
echo
echo "OK. Successfully deployed to git deployment branch."
echo
exit 0 #success
else
echo
echo "Error: failed to push deployment branch to origin."
echo
exit 1 #failure
fi
else
echo
echo "Error: failed to create or overwrite deployment branch."
echo
exit 1 #failure
fi
else
echo
git status #show the status of the working directory
echo
echo "Error: working directory is not clean. Commit your changes first..."
echo
exit 1 #failure
fi
Unfortunately, this seems to give me an error: ./tools/deploygit: 9: [: git: unexpected operator
Why is this so? What operator am I using in if [ git diff-index --quiet HEAD ] that is unexpected?
As a bonus, do you have any suggestions or tips on how to improve the efficiency, logic or readability of this script?
In this statement:
if [ git diff-index --quiet HEAD ]
The [ is an alias for the test command, so what you're actually running is...
if test git diff-index --quiet HEAD ]
...which isn't what you mean. You don't need to use the test command in order to evaluate the result of a command; you should just do this:
if git diff-index --quiet HEAD
Take a look at the documentation for the if command:
$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
The conditional argument to the if statement is command. Normally, the test command is used to make it look like other languages, but you can put any command there. Things that exit with a return code of 0 evaluate to true and anything else evaluates to false.

Resources