简体   繁体   中英

How can I use a shell function with a return code in my git pre-commit hook using husky?

I have a project with githooks managed by the husky package. Usually I appreciate the fail-fast nature of the hooks (unsure if set -e is being used like in a shell script) but I ran into an issue today with my own custom function. I am trying to do one of two things depending on the return value of a function:

#!/bin/sh
my_func() {
  # some stuff
  return command
}

if my_func;
then
     echo "func returned 0"
else
     echo "func returned non-zero"
fi

The problem is as soon as any function returns a non-zero value husky quits out:

husky - pre-commit hook exited with code 1 (error)

How can I ignore/handle non-zero returns from function calls? I don't think I want some type of global ignore as like I said I usually appreciate this fail-fast behavior for unhandled errors. Also I don't think I can switch away from function returns to a shared variable or something because I am doing a lot of async stuff with these function calls so I need their actual return values.

The syntax is not if [ condition ] . It is if cmd . (In the first case, the command is simply [ with several arguments, the last of which is ] ). All you need to do is:

if my_func; then
    echo myfunc returned 0
else
    echo myfunc returned non-zero
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM