Git钩子:如果创建了新的分支,则添加一个新的文件到回购站 [英] Git hook: add a new file to repo if a new branch is created

查看:169
本文介绍了Git钩子:如果创建了新的分支,则添加一个新的文件到回购站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个git钩子,检查是否创建了新的分支,如果是,则将一些预定义的文件添加到该新分支(某些配置文件)的repo中。但是,因为分支实际上正在创建过程中,所以我的逻辑失败了。



目前我正在做一个 post-receive code> hook,它看起来像这样:

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

#if $ oldrev是0000 ... 0000,它是一个新分支
#还检查分支是否为格式feature_< name>
zero =0000000000000000000000000000000000000000
if [$ oldrev=$ zero]&& [[$ branch =〜feature _。+]];然后
#创建临时存储库
temp_repo =`mktemp -d / tmp / repo.XXXXX`
cd $ temp_repo
git clone $ git_url
#这里我创建需要的配置文件,名为file_name
git checkout$ branch
git add$ file_name
git commit -m添加配置文件
git push origin $ branch
fi

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



我不确定哪个 hook 我应该使用这个逻辑,因为我对 git 没有太多了解。任何想法如何我可以去做这件事?



谢谢 如果你在一个钩子中并想运行正常的git命令,你需要取消设置 GIT_DIR 环境变量(在它设置为<$ c $的钩子中c>。)。



也就是说,这在我看来并不是正确的方法。它应该可以工作,但是看起来有点令人惊讶:如果我我必须从原点重新获取和合并来获取这个新提交的内容 $ file_name 文件。如果要求我自己包含该文件,那么它已经存在于分支 feature_def


$ b的提交中了吗? $ b

如果是这样,预接收或更新挂钩将是做检查的地方。一个简单的例子(未经测试):

 #! / bin / sh 
#更新钩子 - 检查新分支是否被命名为
#feature_ *,如果是,则需要配置文件

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

#BEGIN BOILERPLATE
NULL_SHA1 = 0000000000000000000000000000000000000000

#它是什么样的ref?也可以在
refs / heads / *)中输入branch-or-tag
case $ refname的短名称reftype = branch;短名称= $ {refname#参/头/} ;;
refs / tags / *)reftype = tag;短名称= $ {refname#裁判/标签/} ;;
*)reftype = other ;;
esac

#参考文献发生了什么?
#注意:如果更新,可能有两种不同的objtypes,
#但我们只在这里得到新的
case $ oldrev,$ newrev在
$ NULL_SHA1,*)行动=创建; 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
#结束BOILERPLATE

#检查特征分支的代码。名为xyzzy.conf的顶级文件必须存在。
check_feature_branch()
{
if! git show $ refname:xyzzy.conf> / dev / null 2>& 1;那么
echonew branch $ branch不包含顶层的xyzzy.conf>& 2
exit 1
fi
}

#检查我们是否创建了一个名为feature_ *的分支

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

#如果我们到了这里,一定是好的
出口0


I am writing a git hook which checks if a new branch is created, and if so then add some predefined files to the repo for that new branch(Some config files). However because the branch is actually in the process of being created, my logic fails.

Currently I am doing this in a post-receive hook, which looks something like this:

#!/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

This works for an existing branch, however for a newly created branch it gives an error fatal: Not a git repository: '.'.

I am not sure in which hook I should use this logic, as I don't really know much about git. Any idea how I can go about this?

Thanks

解决方案

If you're in a hook and want to run "normal" git commands, you will need to unset the GIT_DIR environment variable (inside a hook it's set to .).

That said, this does not seem to me to be the right approach. It should work but it seems a little surprising: if I git push origin abc:feature_def I'll have to re-fetch and merge from origin to pick up this newly-committed $file_name file. Would it not make more sense to require that I include that file myself, so that it's already there in the commit on branch feature_def?

If so, a pre-receive or update hook would be the place to do the checking. A simplified example (untested):

#! /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钩子:如果创建了新的分支,则添加一个新的文件到回购站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆