简体   繁体   中英

make shell commands available in git hook

The goal is to make some set of commands available to the user after they clone/checkout repo.

the git hook file

#!/bin/sh
echo $PWD/dev-commands.sh
source $PWD/dev-commands.sh

the command I want to make available after the git hook names dev-commands.sh

#!/bin/sh
function greet() {
  echo Hello
}

this does not seem to make the command available. if I run source dev-commands.sh in the directory manually it does make the greet commands availble..

any tips?

Sourced functions are available in the shell that sourced them, not its parent shells. That's why when you write a shell script, it can use its own functions without worrying about interfering with what its invoker's commands mean.

( doit() { echo hi; }; doit ); doit

for instance, will say hi and then very probably complain -bash: doit: command not found or an equivalent message because the parens were there to put doit into a subshell, which has its own environment, just like git hooks.

Altering someone's shell namespace on the fly without their active cooperation can and will be accurately described as malware.

Include a setup command they can source if they want.

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