StevieD
StevieD

Reputation: 7433

How do I prevent terminal from closing out out after git hook is called from bash script?

I'm curious to learn and know why a terminal is crashing out. I have a bash function, gc_push_wh, that calls a git hook like so:

git_push_wh () {
  GIT_DIR_="$(git rev-parse --git-dir)"
  BRANCH="$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD))"

  git push "$@"

  POST_PUSH="$GIT_DIR_/../hooks/post-push"
  test $? -eq 0 && test -x "$POST_PUSH" &&
  exec "$POST_PUSH" "$BRANCH" "$@"
}

All this works fine. But when the hook terminates, the terminal closes out. Here is what's in the hook:

#!/usr/bin/env bash

ssh -t [email protected] -p 30000 "cd ~/.dotfiles; git pull; lb;"

I have a git alias set up to run with this function like so:

git gcpush

When run with the git alias, the terminal does not close out.

IMPORTANT: I tried throwing a read command at the end of the hook but after I hit enter, the terminal still closes out.

Upvotes: 0

Views: 1018

Answers (1)

euphoria83
euphoria83

Reputation: 15136

The exec command is replacing the currently running bash process's image with the image of a new bash process that will run the script in $POST_PUSH. Hence, the process will finish running when that script exits. It will never never come back to the bash process that you started with, as would have happened had the script been run in a subshell.

If you replace, the line

  exec "$POST_PUSH" "$BRANCH" "$@"

with

  "$POST_PUSH" "$BRANCH" "$@"

you will achieve what you wish to.

Upvotes: 3

Related Questions