Git hook not working

I have post-checkout and post-merge githook with this content:

#!/bin/bash
# MIT © Sindre Sorhus - sindresorhus.com
set -eux  

changed_files="$(git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD)"

check_run() {
    echo "$changed_files" | grep --quiet "$1" && eval "$2"
}

echo ''
echo 'running git submodule update --init --recursive if .gitmodules has changed'
check_run .gitmodules "git submodule update --init --recursive"

echo ''
echo 'running npm install if package.json has changed'
check_run package.json "npm prune && npm install"

echo ''
echo 'running npm build:localhost'
npm run build:localhost 

It's strange if there are no changes to .gitmodules, the script ends, and not progress, to check package.json. (it doesn't even echo lines after line 12)

removing check_run checks and simply executing simple commands seems to work fine.

removal check_run .gitmodules "git submodule update --init --recursive"also works. However, the same behavior appears in the following line: check_run package.json "npm prune && npm install"if package.json has not been modified

Is there something I am missing that causes check_run to terminate the script the first time the file is modified?

Visual evidence: enter image description here

+6
source share
1 answer

set -e : -e ", - ", "fail" "exits nozero".

, :

+ check_run .gitmodules 'git submodule update --init --recursive'
+ echo ''
+ grep --quiet .gitmodules

( ). script grep --quiet.

check_run:

check_run() {
    echo "$changed_files" | grep --quiet "$1" && eval "$2"
}

left && right, echo ... | grep ..., - eval "$2".

, , - . , - : -e set, , - , - . 1 , t .

, left && right . , , - , echo ... | grep .... - , 2 .. grep. Grep , ( --quiet, ), 1 , 2 - , 1.

, left && right 1.

, -e , !

, -e ( , - , , ) , left && right .

: left && right if left; then right; fi:

if echo "$changed_files" | grep --quiet "$1"; then
    eval "$2"
fi

, eval "$2" , .

: left && right left && right || true. "" , :

  • ( ),
  • && ( , , ), || true, 0.

:

echo "$changed_files" | grep --quiet "$1" && eval "$2" || true

0, eval -ing "$2" , ( grepped-for).

check_run , eval "$2" , (|| true) . , check_run ( ) -e, eval "$2" , (if ...; then) .


1 4BSD /bin/sh, , , : -e . ( , - , 4BSD sh, , .)

2 bash . , $PIPESTATUS.

+7

Source: https://habr.com/ru/post/1017117/


All Articles