繁体   English   中英

如何使用 pre-push git hook 检查本地和远程分支之间的某些文件或目录是否不同?

[英]How can I use a pre-push git hook to check whether certain files or directories different between the local and the remote branch?

...更具体地说,为什么我不能以编程方式获取文件?

我是编写 git hooks 的新手。 如果自上次推送以来配置文件发生更改,我希望能够执行操作 X。 我想到了两种方法:

1)从github获取相关文件并运行diff。

2) 扫描自上次推送以来的提交历史记录是否存在文件,使用您在推送前 git 钩子中免费获得的信息(当我记录时看起来像这样):

Name of the remote to which the push is being done: origin
URL to which the push is being done: git@github.com:<me>/<myrepo>.git
local ref: refs/heads/<mybranch>
local sha1: c83288a9e0b2a5c9f7b1940360e052b2d4468222
remote ref: refs/heads/<mybranch>
remote sha1: 164c55a9af0abee06bc03e91e162301ebf2bb097

现在我正在尝试 (1) 因为它对我来说感觉更明显。 我不确定如何使用该信息来检查所有提交是否对相关文件进行了更改。 但我被卡住了,因为curl https://raw.githubusercontent.com/<repo-owner>/<repo>/docs-and-config/config/config.yml以及在 Postman 中尝试 GET 命令都返回 404s ——我会理解没有授权,但我找不到。 如果我在浏览器中访问相同的 url,我可以访问它(并且 GitHub 将某种令牌附加到它)。

所以我有两个问题:

(1) 为什么会发生这种情况,你能告诉我如何获取远程文件吗? (2) 你对我如何处理这个任务有更好的想法吗?

pre-push钩子运行时,你的数据还没有被推送到远程系统。 因此,试图从没有它的 GitHub 中获取您尚未推送的数据是行不通的。 GitHub 给你一个 404,因为它还没有。

检查提交历史的方法 2 将是这里的最佳方法。 您可以尝试以下操作:

#!/bin/sh

while read lref loid rref roid
do
    if echo $roid | grep -qsE '^0+$'
    then
        # Do whatever you want with a new branch.
    elif echo $loid | grep -qsE '^0+$'
    then
        # Do whatever you want with a deleted branch.
    elif git diff --name-only $loid $roid | grep -qsF "MY-FILENAME-HERE"
    then
        # Do whatever you want to do if MY-FILENAME-HERE is present in the diff.
    else
        # Do whatever you want if the file is absent.
    fi
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM