跳过处理之前已处理的post-receive hook中的Git修订 [英] Skip processing of Git revisions in post-receive hook that have already been previously processed

查看:86
本文介绍了跳过处理之前已处理的post-receive hook中的Git修订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git post-receive钩子,用于提取在git push期间添加的所有修订版本,并对每个版本进行一些处理(例如发送通知邮件)。除了合并时,这很有效;例如:

I have a git post-receive hook that extracts all the revisions that were added during a "git push" and does some processing on each one (such as sending notification emails). This works great except when merging; e.g.:


  1. 我在branch1上做了一些提交,然后push分支1。 post-receive钩子正确地处理提交。

  2. 我将branch1合并到branch2中,然后推送branch2。 post-receive钩子第二次处理所有合并的提交。

我该如何避免这种情况?下面是我的post-receive hook的开头,我提取了应该处理的提交(最后$ COMMITS保存了要处理的提交列表)。

How can I avoid this? Below is the beginning of my post-receive hook where I extract the commits that should be processed (at the end $COMMITS holds the list of commits to process).

#!/bin/sh

REPO_PATH=`pwd`
COMMITS=''

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# for each ref that was updated during the push
while read OLD_REV NEW_REV REF_NAME; do
  OLD_REV="`git rev-parse $OLD_REV`"
  NEW_REV="`git rev-parse $NEW_REV`"
  if expr "$OLD_REV" : '0*$' >/dev/null; then
    # if the branch was created, add all revisions in the new branch; skip tags
    if ! expr "$REF_NAME" : 'refs/tags/' >/dev/null; then
      REF_REV="`git rev-parse $REF_NAME`"
      REF_NAME="`git name-rev --name-only $REF_REV`"
      COMMITS="$COMMITS `git rev-list $REF_NAME | git name-rev --stdin | grep -G \($REF_NAME.*\) | awk '{ print $1 }' | tr '\n' ' '`"
    fi

  elif expr "$NEW_REV" : '0*$' >/dev/null; then
    # don't think branch deletes ever hit a post-receive hook, so we should never get here
    printf ''
  else
    # add any commits in this push
    COMMITS="$COMMITS `git rev-parse --not --all | grep -v $(git rev-parse $REF_NAME) | git rev-list --reverse --stdin $(git merge-base $OLD_REV $NEW_REV)..$NEW_REV | tr '\n' ' '`"
  fi
done


推荐答案

$ c>,这正是我想要的。基本上它使用 git for-each-ref 查找所有分支的名称,然后排除可从除正在更新的分支以外的某个分支到达的每个提交:

Look at $(prefix)/share/git-core/contrib/hooks/post-receive-email, which does just what (I think) you want. Basically it uses git for-each-ref to find the names of all branches, and then exclude every commit that's reachable from some branch other than the one being updated:

if [ "$change_type" = create ]
then
    # Show all revisions exclusive to this (new) branch.
    revspec=$newrev
else
    # Branch update; show revisions not part of $oldrev.
    revspec=$oldrev..$newrev
fi

other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
     grep -F -v $refname)
git rev-parse --not $other_branches | git rev-list --pretty --stdin $revspec

(我在这里简化了它,希望在我的剪贴工作中不会损坏任何东西,这里的输入是: $ change_type create if if $ oldrev 是全零,否则它是 update ; $ oldrev 是来自stdin的最近一行旧的rev SHA1; $ newrev 是新的SHA SHA; $ refname 是全名,例如 refs / heads / topic 。)

(I've simplified it here, and hopefully not damaged anything in my cut-and-paste job. The inputs here are: $change_type is create if $oldrev is all-zeros, otherwise it's update; $oldrev is the old rev SHA1 from the line recently-read from stdin; $newrev is the new rev SHA1; and $refname is the full name, e.g., refs/heads/topic.)

这篇关于跳过处理之前已处理的post-receive hook中的Git修订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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