Shell post-commit 钩子的脚本给出 - 不是 git 存储库错误
Shell script for post-commit hook gives - Not a git repository error
我已经编写了一个 post-commit 挂钩,它需要在提交发生时为 python 脚本创建虚拟环境 运行。
以下是我的脚本:
#!/bin/bash
# Teamcity-build trigger
echo "Executing post-commit hook"
BASEDIR=$(dirname $(readlink -f [=13=]))
VENV=venv
ACTIVATE=$VENV/bin/activate
STATUS=false
CIDIR=teamcity
# Username
machine=$(uname -n)
echo "Notifying Teamcity Server to execute a Build on " $machine
# Logic to check if commit is done or merged to a particular branch / master branch
cd $BASEDIR
# Change directory to parent directory, since current directory is `.git/hooks/`
cd ../..
# Go in teamcity directory
cd $CIDIR
# Check if venv folder exists, if it does not then create it
if [ ! -d "$VENV" ]; then
# Control will enter here if $VENV doesn't exist.
virtualenv -p python $VENV
STATUS=true
fi
# Source virtual environment
source $ACTIVATE
# Install required dependencies if not yet installed
if [ "$STATUS" = true ]; then
# if status is true, means need to install all the required libraries
pip install --upgrade pip
pip install -r requirements.txt
fi
# check current git branch
# $(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),,')
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [ "$BRANCH" = "rahul" ]; then
# if branch is master, then only proceed.
# Start processing teamcity jobs
python last_dir_processed.py
else
echo "Current Branch $BRANCH, Nothing to execute."
fi
# Deactivate virtual environment
deactivate
第 43 行,
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
脚本试图知道当前 b运行ch 的名称。
但它给出错误为:
fatal: Not a git repository: '.git'
我在该行之前用 pwd 命令检查了文件夹路径,它显示 .git
文件夹存在。
此外,当我 运行 手动执行命令时,它起作用了:
git rev-parse --symbolic-full-name --abbrev-ref HEAD
这给了我当前的 b运行ch 名称。
编辑: 以下是我的目录结构:
--root_dir
--teamcity
-last_dir_processed.py
--.git
--hooks
-post-commit
所以,理想情况下脚本应该正确执行,但它仍然给出错误。不确定可能是什么原因。
只是为了确定,设置 GIT_DIR
and GIT_WORK_TREE
environment variables 以确保任何 git
命令在正确的上下文中运行:
cd ../..
GIT_DIR=$(pwd)/.git
GIT_WORK_TREE=$(pwd)
我已经编写了一个 post-commit 挂钩,它需要在提交发生时为 python 脚本创建虚拟环境 运行。
以下是我的脚本:
#!/bin/bash
# Teamcity-build trigger
echo "Executing post-commit hook"
BASEDIR=$(dirname $(readlink -f [=13=]))
VENV=venv
ACTIVATE=$VENV/bin/activate
STATUS=false
CIDIR=teamcity
# Username
machine=$(uname -n)
echo "Notifying Teamcity Server to execute a Build on " $machine
# Logic to check if commit is done or merged to a particular branch / master branch
cd $BASEDIR
# Change directory to parent directory, since current directory is `.git/hooks/`
cd ../..
# Go in teamcity directory
cd $CIDIR
# Check if venv folder exists, if it does not then create it
if [ ! -d "$VENV" ]; then
# Control will enter here if $VENV doesn't exist.
virtualenv -p python $VENV
STATUS=true
fi
# Source virtual environment
source $ACTIVATE
# Install required dependencies if not yet installed
if [ "$STATUS" = true ]; then
# if status is true, means need to install all the required libraries
pip install --upgrade pip
pip install -r requirements.txt
fi
# check current git branch
# $(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),,')
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [ "$BRANCH" = "rahul" ]; then
# if branch is master, then only proceed.
# Start processing teamcity jobs
python last_dir_processed.py
else
echo "Current Branch $BRANCH, Nothing to execute."
fi
# Deactivate virtual environment
deactivate
第 43 行,
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
脚本试图知道当前 b运行ch 的名称。
但它给出错误为:
fatal: Not a git repository: '.git'
我在该行之前用 pwd 命令检查了文件夹路径,它显示 .git
文件夹存在。
此外,当我 运行 手动执行命令时,它起作用了:
git rev-parse --symbolic-full-name --abbrev-ref HEAD
这给了我当前的 b运行ch 名称。
编辑: 以下是我的目录结构:
--root_dir
--teamcity
-last_dir_processed.py
--.git
--hooks
-post-commit
所以,理想情况下脚本应该正确执行,但它仍然给出错误。不确定可能是什么原因。
只是为了确定,设置 GIT_DIR
and GIT_WORK_TREE
environment variables 以确保任何 git
命令在正确的上下文中运行:
cd ../..
GIT_DIR=$(pwd)/.git
GIT_WORK_TREE=$(pwd)