有些脚本在检查更改时不能正确工作。

我是这样试的:

VN=$(git describe --abbrev=7 HEAD 2>/dev/null)

git update-index -q --refresh
CHANGED=$(git diff-index --name-only HEAD --)
if [ ! -z $CHANGED ];
    then VN="$VN-mod"
fi

是否存在某种布尔检查,自上次提交以来是否有更改,或者我如何真正测试本地存储库是否有新的更改?

我所做的所有这些都是为了一个版本创建脚本(我在这里的某个地方找到的)。


当前回答

我的观点是:

git status --porcelain | head -1

只返回一行,而不是一个长列表

其他回答

nano checker_git.sh

粘贴这

#!/bin/bash

echo "First arg: $1"

cd $1

bob="Already up-to-date."
echo $bob

echo $(git pull) > s.txt
cat s.txt
if [ "$(cat s.txt)" == "$bob" ]
then
echo "up"
else
echo "not up"

fi
rm -rf st.txt

执行sh checker_git.sh gitpath命令

您所做的几乎可以工作:如果它是空的,您应该引用$CHANGED,而-z测试为空,这意味着没有更改。你的意思是:

if [ -n "$CHANGED" ]; then
    VN="$VN-mod"
fi

引用Git的Git - version - gen:

git update-index -q --refresh
test -z "$(git diff-index --name-only HEAD --)" ||
VN="$VN-dirty"

看起来你是在抄袭,但你忘记了引用的细节。

当然,你也可以这样做:

if git diff-index --quiet HEAD --; then
    # No changes
else
    # Changes
fi

或者如果你只关心“某事发生了变化”的情况:

if ! git diff-index --quiet HEAD --; then
    VN="$VN-mod"
fi

使用——quiet的好处是Git可以在遇到单个差异时立即停止处理,因此它可能不需要检查整个工作树。

虽然Jefromi的回答很好,但我发布这篇文章只是为了参考。

在Git源代码中有一个sh脚本,其中包括以下内容。

require_clean_work_tree () {
    git rev-parse --verify HEAD >/dev/null || exit 1
    git update-index -q --ignore-submodules --refresh
    err=0

    if ! git diff-files --quiet --ignore-submodules
    then
        echo >&2 "Cannot $1: You have unstaged changes."
        err=1
    fi

    if ! git diff-index --cached --quiet --ignore-submodules HEAD --
    then
        if [ $err = 0 ]
        then
            echo >&2 "Cannot $1: Your index contains uncommitted changes."
        else
            echo >&2 "Additionally, your index contains uncommitted changes."
        fi
        err=1
    fi

    if [ $err = 1 ]
    then
        test -n "$2" && echo >&2 "$2"
        exit 1
    fi
}

Ansible的解决方案:

- name: Bootstrap checks
  block:
  - shell: "git status --porcelain"
    register: git_status
  - fail:
      msg: "Please clean git status: {{ git_status.stdout }}"
    when: git_status.stdout != ""

OP的问题已经超过9年了。我不知道那个时候的男人是怎么说的,但现在是这样说的:

--porcelain[=<version>]  
Give the output in an easy-to-parse format for scripts. This is similar to the 
short output, but will remain stable across Git versions and regardless of user 
configuration. See below for details.  

The version parameter is used to specify the format version. This is optional and 
defaults to the original version v1 format.  

这表明,“瓷器”论点非常适合用于测试回购的变化状态。

Wrt OP的问题,“是否有某种布尔检查自上次提交以来是否有更改,或者我如何真正测试本地存储库是否有新的更改?”

我不认为bash本身有布尔数据类型,但这可能足够接近:

[ -z "`git status --porcelain`" ] && echo "NULL-NO DIFFS" || echo "DIFFS EXIST"

这可以作为脚本的if-then-else形式重新转换,或者在git repo文件夹中按原样在CLI中执行。否则,使用-C选项和目标repo的路径规范:

git -C ~/path/to/MyGitRepo status --porcelain 

附录:

有些人建议使用-u,——untracked-file选项来避免报告想要忽略的文件的状态。注意,这带来了一个不幸的副作用:新添加的文件也没有处于状态。该选项在某些情况下是有用的,但在使用前要仔细考虑。