Git Hook : add a new file to repo if a new branch is created

标签 git githooks git-bash

我正在编写一个 git hook,它检查是否创建了新分支,如果是,则将一些预定义文件添加到该新分支的存储库中(一些配置文件)。然而,由于分支实际上正在创建过程中,所以我的逻辑失败了。

目前,我正在 post-receive Hook 中执行此操作,如下所示:

#!/bin/sh
read oldrev newrev refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
echo "branch is $branch"
echo "oldrev is $oldrev and newrev is $newrev" 

# if $oldrev is 0000...0000, it's a new branch
# also check if the branch is of the format "feature_<name>"
zero="0000000000000000000000000000000000000000"
if [ "$oldrev" = "$zero" ] && [[ $branch =~ feature_.+ ]]; then
    #create a temp repo
    temp_repo=`mktemp -d /tmp/repo.XXXXX`
    cd $temp_repo
    git clone $git_url
    #here i create the config file needed, called file_name
    git checkout "$branch"
    git add "$file_name"
    git commit -m "Added config file"
    git push origin $branch
fi

这适用于现有分支,但是对于新创建的分支,它会给出错误致命:不是 git 存储库:'.'

我不确定在哪个 hook我应该使用这个逻辑,因为我对 git 不太了解。知道我该怎么做吗?

谢谢

最佳答案

如果您处于钩子(Hook)中并且想要运行“普通”git 命令,则需要取消设置 GIT_DIR 环境变量(在钩子(Hook)内它设置为 ).

也就是说,在我看来这不是正确的方法。它应该可以工作,但似乎有点令人惊讶:如果我 git push origin abc:feature_def ,我将不得不从 origin 重新获取并 merge 以获取这个新提交的 $file_name 文件。要求我自己包含该文件,以便它已经存在于分支 feature_def 的提交中,这不是更有意义吗?

如果是这样,预接收或更新 Hook 将是进行检查的地方。一个简化的示例(未经测试):

#! /bin/sh
# update hook - check if new branch is named
# feature_*, and if so, require config file

refname=$1
oldrev=$2
newrev=$3

# BEGIN BOILERPLATE
NULL_SHA1=0000000000000000000000000000000000000000

# what kind of ref is it? also, get short name for branch-or-tag
case $refname in
refs/heads/*) reftype=branch; shortname=${refname#refs/heads/};;
refs/tags/*) reftype=tag; shortname=${refname#refs/tags/};;
*) reftype=other;;
esac

# what's happening to the ref?
# note: if update, there are potentially two different objtypes,
# but we only get the new one here
case $oldrev,$newrev in
$NULL_SHA1,*) action=create; objtype=$(git cat-file -t $newrev);;
*,$NULL_SHA1) action=delete; objtype=$(git cat-file -t $oldrev);;
*,*) action=update; objtype=$(git cat-file -t $newrev);;
esac
# END BOILERPLATE

# code to check a feature branch.  Top level file named xyzzy.conf must exist.
check_feature_branch()
{
    if ! git show $refname:xyzzy.conf >/dev/null 2>&1; then
        echo "new branch $branch does not contain xyzzy.conf at top level" >&2
        exit 1
    fi
}

# check whether we're creating a branch named feature_*

case $action,$reftype,$shortname in
create,branch,feature_*) check_feature_branch;;
*) ;;
esac

# if we got here it must be OK
exit 0

关于Git Hook : add a new file to repo if a new branch is created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323175/

相关文章:

bash - "Git Bash here"不保留 session 之间的 bash 历史记录

windows - 如何在 Windows 的 Git Credential Manager 中按存储库在本地存储凭据?

差异 sqlite 表的 Git 钩子(Hook)

git - 如何测试我的预接收 Hook ?

c++ - 加载共享库时出错 : jvm. dll

bash - 获取错误 : bash: parse_git_branch: command not found

git - 将模式附加到默认差异 header 模式 (diff.cpp.xfuncname)

android - 如何使用git跟踪android分支

svn - 如何使用 git-svn 切换 svn 分支?

git - 将参数传递给 git pre-push hook