Can a Git hook automatically add files to the commit?

前端 未结 10 1605
礼貌的吻别
礼貌的吻别 2020-11-28 05:08

I\'d like to add an automatically generated file to the same commit using a pre- or post-commit hook in Git, dependent on the files that were modified in that commit. How w

10条回答
  •  没有蜡笔的小新
    2020-11-28 05:26

    It's possible to do what you want using pre-commit hooks. We do something similar for a heroku deployment (compiling coffeescript to javascript). The reason your script isn't working is because you used the exec command improperly.

    From the man page:

    The exec builtin is used to replace the currently running shells process image with a new command. On successful completion, exec never returns. exec can not be used inside a pipeline.

    Only your first exec command is running. After that your script is basically terminated.

    Give something like this a try (as a pre-commit hook):

    #!/bin/sh
    files=`git diff --cached --name-status`
    re=""
    if [[ $files =~ $re ]]
    then
      echo "Creating files"
      bundle exec create_my_files
      git add my_files
    fi
    

提交回复
热议问题