How can I automatically be warned if a specific file changes?

前端 未结 2 595
面向向阳花
面向向阳花 2021-02-04 03:30

I have a php project, and when I pull from another repository and the composer.lock file gets changed, I\'m supposed to run composer.phar install --dev. How can git

2条回答
  •  执笔经年
    2021-02-04 03:48

    Here's an interesting gist: https://gist.github.com/sindresorhus/7996717 I've adapted it for your question.

    post-merge git hook is executed when you merge branches or when you do git pull.

    post-merge hook (read docs)

    #/usr/bin/env bash
    # MIT © Sindre Sorhus - sindresorhus.com
    # forked by naXa! - naxa.by
    
    # Git hook to run a command after `git merge` / `git pull` if a specified file was changed.
    # Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
    
    changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
    
    check_run() {
      echo "$changed_files" | egrep --quiet "$1" && echo "$2"
    }
    
    # In this example it's used to print a warning if composer.lock has been changed
    check_run composer.lock "Run `composer.phar install --dev`"
    

    post-checkout git hook is executed when you switch between branches or when you do git rebase.

    post-checkout hook (read docs)

    #/usr/bin/env bash
    # MIT © Sindre Sorhus - sindresorhus.com
    # forked by naXa! - naxa.by
    
    # Git hook to run a command after `git checkout` if a specified file was changed.
    # Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
    
    # terminate gracefully on a file checkout (retrieving a file from the index)
    # uncomment the below line if you don't want to run this hook on a file checkout (for example on git->revert in IntelliJ IDEA)
    # [ $3 -eq 0 ] && { exit 0; }
    
    changed_files="$(git diff-tree -r --name-only --no-commit-id $1 $2)"
    
    check_run() {
      echo "$changed_files" | egrep --quiet "$1" && echo "$2"
    }
    
    # In this example it's used to print a warning if composer.lock has been changed
    check_run composer.lock "Run `composer.phar install --dev`"
    
    exit 0;
    

    You may notice that I've changed grep to egrep here. It is done in order to be able to search using a fancy expression. For example "file1.txt|file2.txt" where | is used as an OR operator.

提交回复
热议问题