I have 2 repos on my server, one is a bare repo and the other one is a "real/non-bare" repo with worktree.
Inside my bare repo I have a post-receive hook that should change directory to the real/non-bare repo and call a git command:
git pull origin master
So that my real repo is updated according to the pushes made on the bare repository.
But I get an error:
remote: subprocess.CalledProcessError: Command '['git', 'pull', 'origin', 'master']' returned non-zero exit status 128.
It seems to me that although I changed the os.chdir() to the real repo inside hook script, git ignored that and it does not recognize my git repo.
When I run the hook directly by going to the bare repo hook directory and run python3 post-receive it does run the command without throwing an error.
Git dir is the bare repo
Git_real is the non-bare repo
home/my_machine
├── Git
│ └── search_movie.git
│ ├── branches
│ ├── config
│ ├── description
│ ├── HEAD
│ ├── hooks
│ │ └── post-receive
│ ├── info
│ │ └── exclude
│ ├── objects
│ │ ├── 93
│ │ │ └── 68286a3e6b37235950d0b369d6aa955bc6699b
│ │ ├── info
│ │ └── pack
│ │ ├── pack-8710cd10d53b27cda2228c33ff59e337426563b9.idx
│ │ └── pack-8710cd10d53b27cda2228c33ff59e337426563b9.pack
│ └── refs
│ ├── heads
│ │ └── master
│ └── tags
└── Git_real
└── search_movie
├── find_movie.py
├── output.txt
└── search_vid.py
post-receive hook
#!/usr/bin/env python3
import sys, os, subprocess
path_2_non_bare_repo = "/home/my_machine/Git_real/search_movie"
#cwd is the path to my_repo/.git/hooks
cwd = os.path.dirname(os.path.realpath(__file__))
#change dir to real repository
os.chdir(path_2_non_bare_repo)
try:
pll = subprocess.check_output(["git", "pull", "origin", "master"], shell=False)
print("fetch =", pll)
except subprocess.CalledProcessError as err:
print(err)
The Q is why is git not recognizing my current working directory change made with os.chdir?
CodePudding user response:
Check your error message -- meaning : choose a way to inspect your process' stderr when it errors.
git
exiting with a 128
exit code does not specifically indicate "not run from within a repository".
For example : git fetch origin nonexistingbranch
exits with code 128
CodePudding user response:
Git hooks run with numerous Git-specific environment variables set so that references to some Git repository will go to this Git repository, where "this" means "the one running the hook".
The result is that if you:
cd /path/to/other/repo
and then run some Git command, the Git command you've run operates in the original repository, not the /path/to/other/repo
repository.
To fix this, you must un-set the appropriate Git-specific environment variables. These include, but are not necessarily limited to, GIT_DIR
and GIT_WORK_TREE
. The one that is definitely set and definitely must be unset is GIT_DIR
.