git prepush hook:get branches push to

kxe2p93d  于 2023-11-15  发布在  Git
关注(0)|答案(2)|浏览(185)

如何在pre push hook的上下文中获取被推送到的分支列表?我知道如何获取当前分支,但它可能与被推送到的一个/多个分支不同。
我想过解析这个命令,但我担心我可能会忘记一些情况。比如git pushgit push origin master都将推送到master等等。

jmo0nnb3

jmo0nnb31#

  1. while read oldrev newrev refname
  2. do
  3. if [[ "$newrev" != "0000000000000000000000000000000000000000" ]] ; then
  4. branch=${refname#refs/heads/}
  5. fi
  6. done

字符串

qybjjes1

qybjjes12#

这里有一个可能的循环,尽管我不清楚你想对远程分支或标记名和/或本地引用做什么:

  1. #! /bin/sh
  2. NULLSHA=0000000000000000000000000000000000000000 # 40 0's
  3. say() {
  4. echo "$@" 1>&2
  5. }
  6. while read localref localhash foreignref foreignhash; do
  7. case $foreignref in
  8. refs/heads/*)
  9. reftype=branch
  10. shortref=${foreignref#refs/heads/};;
  11. refs/tags/*)
  12. reftype=tag
  13. shortref=${foreignref#refs/tags/};;
  14. *) reftype=unknown-ref-type
  15. shortref=$foreignref;;
  16. esac
  17. say "pushing to $reftype $shortref"
  18. case $localhash,$foreignhash in
  19. $NULLSHA,*) say "(push with intent to delete)";;
  20. *,$NULLSHA) say "(push with intent to create)";;
  21. *) say "(push with intent to update from $foreignhash to $localhash)"
  22. # for branch updates only, let's see if it's a fast-forward
  23. if [ $reftype = branch ]; then
  24. if git merge-base --is-ancestor $foreignhash $localhash; then
  25. say "(this is a fast-forward)"
  26. else
  27. say "(this can only work if forced)"
  28. fi
  29. fi;;
  30. esac
  31. done

字符串
(note:这一点没有得到很好的测试)。

展开查看全部

相关问题