【问题标题】:Verify that a file was only modified at the end between 2 commits验证文件仅在 2 次提交之间最后被修改
【发布时间】:2021-01-24 22:14:15
【问题描述】:

我的存储库中有一个文件,我只想在最后允许插入。

我可以使用什么 linux 命令来验证在两次 git 提交之间文件仅在最后被修改?

【问题讨论】:

  • 服务器端:您可以在 pre-receive 钩子中检查,在本地 git repo 中:在 pre-commit 钩子中(在每次提交时)或在 pre-push 钩子中(在每次推送时)
  • 感谢您的回答。我正在寻找一个命令,如果文件仅在末尾被修改,则返回 0,否则返回 1,以便在 gitlab ci 管道中使用它。很抱歉造成混乱(我会更新问题)

标签: linux git gitlab


【解决方案1】:

没有执行此操作的 Git 命令,但您可以在 Bash(或您选择的 shell)中编写脚本:

您可以创建一个执行此操作的脚本:

#!/bin/bash

# Create a file which is a version of your file of interest before last commit

git --no-pager show HEAD~:<file> > original

# Get the number of lines for that file and call it n:

n=$(cat original | wc -l)

# Test whether the last commit modified the file other than
# by adding content at the end

[[ "$(diff <(cat original) <(head -$n <file>))" == "" ]]

# Finally, clean up by deleting the temp file:

rm original

如果文件仅在最后(或根本没有)被修改,这将返回 0,如果您想要阻止修改(即不在最后),则返回 1。

当然,您需要将&lt;file&gt; 替换为您感兴趣的文件的路径。

如果您感兴趣的 2 次提交不是最后 2 次提交,您可以将脚本修改为更通用。例如,在这里,我将使用 &lt;hash1&gt; 和 &lt;hash2&gt; 进行 2 次提交,但您可以使用任何便于识别任何 2 次提交的参考:

#!/bin/bash

# Create a file which is a version of your file of interest at the first commit
# you care about (using its hash):

git --no-pager show <hash1>:<file> > original

# Create a file which is a version of your file of interest at the second commit
# you care about (using its hash):

git --no-pager show <hash2>:<file> > next

# Get the number of lines for the first of these files and call it n:

n=$(cat original | wc -l)

# Test whether the commit with hash2 modified the file other
# than by adding content at the end

[[ "$(diff <(cat original) <(head -$n next))" == "" ]]

# Finally, clean up by deleting the 2 temp files:

rm original next

或者,为了方便起见,因为您可能需要经常在管道中对此进行测试,您可以创建一个函数并将其添加到您的 .bashrc 或等效项中:

function check {
    local n
    git --no-pager show "$2":"$1" > original
    git --no-pager show "$3":"$1" > next
    n=$(cat original | wc -l)
    [[ "$(diff <(cat original) <(head -$n next))" == "" ]]
    rm original next
}

然后您可以将其用于:

check <file-path> <commit-hash1> <commit-hash2>

【讨论】:

    【解决方案2】:

    可以使用git log hash1..hash2查看是否有一个文件被--stat修改过,修改内容用-c查看。

    注意,这里的 hash2 应该是较旧的提交,而 hash2 是最近的提交。否则你将一无所获

    $ git log --stat bdf402d1a387ba9a8..620f05b4c9 library/HQ/groupsql.sql
    commit 620f05b4c9ce53549b4ad8003de433e44054dea6
    Author: lu <lu.com>
    Date:   Fri Oct 9 17:57:24 2020 +0800
    
        specify material,price when create class course
    
     library/HQ/groupsql.sql | 6 ++++--
     1 file changed, 4 insertions(+), 2 deletions(-)
    
    commit ed0c5461c79acd43072ca1d2655cec76076d7de3
    Author: lu <lu.com>
    Date:   Mon Sep 28 20:56:23 2020 +0800
    
        remove material history
    
     library/HQ/groupsql.sql | 14 --------------
     1 file changed, 14 deletions(-)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-08
      • 2021-04-27
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多