用于“git远程更新”的本地git钩子? [英] local git hook for "git remote update"?

查看:256
本文介绍了用于“git远程更新”的本地git钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地的git仓库,用以下方式创建:

  git clone --mirror git://github.com/ < user> /< project> .git 

偶尔的更改会被推送到github上,机器会将它们拉到:

  git remote update --prune 

这一切都正常,但更新后,我想运行一个钩子,但我不确定要运行哪个钩子。我已经尝试过post-receive和post-merge,但是在更新后似乎都没有执行。在这两种情况下,钩子文件的内容都是:

 #!/ bin / sh 
echo foo> foo.log

当我通过bash.exe从命令行运行它们时(是的,在Windows ),创建了一个foo.log文件,这样的工作。但git远程更新的结果没有任何反应。在每个git远程更新之间,我推送一个无用的更改,以确保有一些东西需要解压。

解决方案

这通过制作一个bash脚本来包装git remote update命令。脚本在更新之前和之后查看所有refspecs,然后使用与git会缺少的功能相同的方式调用更新后的挂钩。感谢 @torek @请注意:此脚本使用bash关联数组,这些数组仅在bash版本> = 4.0中可用。

 #!/ bin / bash 

git_dir = $(git rev-parse --git- dir)||退出
cd$ git_dir

声明-A之前全部

#创建'before'列表
while read commit_hash refspec;在[$ refspec] =$ commit_hash之前做

全部[$ refspec] =''
完成< <(git show-ref - heads)

#进行远程更新
git remote update --prune

#创建'after'列表
读取commit_hash时refspec;在[$ refspec] =$ commit_hash
all [$ refspec] =''
完成后执行
< <(git show-ref - heads)

#查看是否有任何更改,如果有,请运行post-receive hook
changes = 0
for refspec在$ {!all [@]}中;做
[$ {before [$ refspec]}!=$ {after [$ refspec]}]&& {changes = 1;打破; }
完成

if [$ changes==1];那么
none =$(printf%0.s0{1..40})#四十零或git的不关心ref
在$ {!all [@]};做
#如果refspec改变了,把它传递给post-receive hook
[$ {before [$ refspec]}!=$ {after [$ refspec]}]&& amp ; \
echo$ {before {$ refspec]: - $ none} $ {after [$ refspec]: - $ none} $ refspec
done |挂钩/收到后
fi


I have a local git repository, created with:

git clone --mirror git://github.com/<user>/<project>.git

Occasionally changes will get pushed to github, and this machine will pull them with:

git remote update --prune

This all works fine, but after the update, I want to run a hook, and I'm not sure which to run. I have tried "post-receive" and "post-merge", but neither seems to execute after the update. In both cases, the contents of the hook file are:

#!/bin/sh 
echo foo > foo.log

When I run them from the command-line via bash.exe (yes, on Windows), a foo.log file is created, so that works. But nothing happens as a result of the "git remote update". Between each "git remote update" I am pushing a useless change to make sure there's something to pull.

I ended up solving this by making a little bash script to wrap the "git remote update" command. The script looks at all refspecs before and after the update, then calls the post-update hook in the same way that git would if it had the missing feature. Credit to @torek and @larsks for the idea in their comments to the original question.

Note: this script uses bash associative arrays which are only available in bash versions >= 4.0.

#!/bin/bash

git_dir=$(git rev-parse --git-dir) || exit
cd "$git_dir"

declare -A before after all

# create the 'before' list
while read commit_hash refspec; do
  before[$refspec]="$commit_hash"
  all[$refspec]=''
done < <(git show-ref --heads)

# do the remote update
git remote update --prune

# create the 'after' list
while read commit_hash refspec; do
  after[$refspec]="$commit_hash"
  all[$refspec]=''
done < <(git show-ref --heads)

# see if there were any changes, and if so, run the post-receive hook
changes=0
for refspec in "${!all[@]}"; do
  [ "${before[$refspec]}" != "${after[$refspec]}" ] && { changes=1; break; }
done

if [ "$changes" == "1" ]; then
  none="$(printf "%0.s0" {1..40})" # forty zeroes, or git's "don't care" ref
  for refspec in "${!all[@]}"; do
    # if the refspec changed, pass it to the post-receive hook
    [ "${before[$refspec]}" != "${after[$refspec]}" ] && \
      echo "${before[$refspec]:-$none} ${after[$refspec]:-$none} $refspec"
  done | hooks/post-receive
fi

这篇关于用于“git远程更新”的本地git钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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