简体   繁体   中英

Running git commands from post-receive hook

Explaining why I want to do this is complicated, but what I would like to do is to equip a particular git repository with a post-receive hook that does the following:

  1. Move to a test directory
  2. Clone the project that was just updated.
  3. Checkout a specific branch.
  4. Do some tests

The post-receive code looks like this (simplified): let "repo" be the name of the git repository, let "testdir" be the name of a sibling directory, initially empty, and let "dev" be the name of the branch.

cd ../testdir
git clone --local ../repo .
git checkout dev

However, when the code in the script gets to "git checkout", git responds with "fatal: not a git repository: '.'"

I have no idea why git thinks "testdir" isn't a git repository. If I run that commands from a command line (rather than from within post-receive), then they work correctly. I don't know why the behavior would be different from within post-receive. Any ideas?

You need to unset GIT_DIR in your post-receive hook. The problem is that at the time your hook script runs, GIT_DIR=. , which after your cd and clone operation is no longer useful.

I set up a local test environment, and when my post-receive script looks like this:

#!/bin/sh

cd ../testdir
git clone --local ../upstream.git .
git checkout dev

I get this:

remote: fatal: Not a git repository: '.'

But if I unset the GIT_DIR variable:

#!/bin/sh

unset GIT_DIR
cd ../testdir
git clone --local ../upstream.git .
git checkout dev

Everything works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM