Reuse GIT_WORK_TREE in post-receive hook to rm a few files

前端 未结 3 1902
梦毁少年i
梦毁少年i 2020-12-09 19:30

I\'ve used this \'tutorial\' to set up a DSP environment: http://toroid.org/ams/git-website-howto (Yes, I don\'t have a T).

My workflow is very easy:

  1. D
相关标签:
3条回答
  • 2020-12-09 20:05
    GIT_WORK_TREE=....
    export GIT_WORK_TREE
    git ...
    rm $GIT_WORK_TREE/...
    

    should work and is safest (you don't always need to export your environment variables, but usually it doesn't harm :))

    0 讨论(0)
  • 2020-12-09 20:21

    why not use a variable for the path?

    #!/bin/sh
    work_tree=/var/www/www.example.org
    GIT_WORK_TREE=$work_tree git checkout -f
    rm $work_tree/www/all.css
    rm $work_tree/www/all.js
    

    then you don't need to export. This works perfect for me.

    0 讨论(0)
  • 2020-12-09 20:26

    There are a few problems with that you're trying at the moment. Perhaps it's best to explain what's wrong with each example you've tried in turn:

    GIT_WORK_TREE=/var/www/www.example.org git checkout -f
    rm $GIT_WORK_TREE/www/all.css
    rm $GIT_WORK_TREE/www/all.js
    

    The problem here is that if you define a variable directly before the command, the environment variable is only set in the environment of the command you're running - it's not set in the current shell. You can easily test this:

    $ FOO=bar sh -c 'echo $FOO'            
    bar
    $ echo $FOO
    
    $ 
    

    Your next attempt was:

    GIT_WORK_TREE=/var/www/www.example.org
    git checkout -f
    

    In this case, in order for the variable to be set in the environment of other commands you invoke in the script, you need to export it, for example with export GIT_WORK_TREE=/var/www/www.example.org instead of your first line.

    Your last attempt was:

    GIT_WORK_TREE=/var/www/www.example.org
    cd $GIT_WORK_TREE
    git checkout -f
    

    This has the same problem as your previous attempt (i.e. that you need to export GIT_WORK_TREE) but it also has a more subtle problem. As I describe in this blog post, GIT_DIR is set to . (i.e. the current directory) in the post-receive script's environment. So, if you change directory to the top of the working tree, GIT_DIR will point to that directory, and not the .git directory! A couple of good rules of thumb are (a) that you should only set GIT_DIR and GIT_WORK_TREE together, and (b) if you do set them, you should set them both to absolute paths. So, I think the following hook will work in your case:

    #!/bin/sh
    export GIT_WORK_TREE=/var/www/www.example.org
    export GIT_DIR=/var/www/www.example.org/.git
    cd $GIT_WORK_TREE
    git checkout -f
    rm www/all.css
    rm www/all.js
    
    0 讨论(0)
提交回复
热议问题