以编程方式打印git版本并检查未提交的更改

以编程方式打印git版本并检查未提交的更改,git,Git,为了确保我的科学分析是可复制的,我想通过编程检查代码库中是否有任何未签入的修改,如果没有,则打印出正在使用的commit 例如,如果有未提交的更改,它应该输出 Warning: uncommitted changes made. This output may not be reproducible. 否则,生产 Current commit: d27ec73cf2f1df89cbccd41494f579e066bad6fe 理想情况下,它应该使用“管道”,而不是“陶瓷”。这使用陶瓷,但如果

为了确保我的科学分析是可复制的,我想通过编程检查代码库中是否有任何未签入的修改,如果没有,则打印出正在使用的commit

例如,如果有未提交的更改,它应该输出

Warning: uncommitted changes made. This output may not be reproducible.
否则,生产

Current commit: d27ec73cf2f1df89cbccd41494f579e066bad6fe

理想情况下,它应该使用“管道”,而不是“陶瓷”。这使用陶瓷,但如果与现有文件存在差异,则退出代码为1,如果与现有文件没有差异,则退出代码为0。不幸的是,它不会检查未跟踪的文件


如何在Git中检索当前提交的哈希?倡导者
git rev parse——验证HEAD
以打印当前提交。

您需要的git管道位是 , 也许和我一起

下面的shell程序使用这些Git管道命令,具有可调整的未跟踪/忽略处理,并且对所有可能的错误情况都相当谨慎

#!/bin/sh

# warn-unclean: print a warning if the working tree and index are not clean

#  For utmost strictness, set check_untracked=yes and check_ignored=yes.
#  When both are 'yes', verify that working tree and index are identical to HEAD.
#  When only check_untracked is yes, extra ignored files are allowed.
#  When neither is yes, extra untracked files and ignored files are allowed.

check_untracked=yes
check_ignored=yes

warn() {
    echo 'Warning: '"$*" \
        'This output may not be reproducible.'
}

# Compare HEAD to index and/or working tree versions of tracked files
git diff-index --quiet HEAD
case $? in
    0)
        if test "$check_untracked" != yes; then
            clean=yes
        else
            # Still need to check for untracked files

            or_ignored=''
            exclude=--exclude-standard
            if test "$check_ignored" = yes; then
                or_ignored=' or ignored'
                exclude=''
            fi

            (
                # Move to top level of working tree
                if up="$(git rev-parse --show-cdup)"; then
                    test -n "$up" && cd "$up"
                else
                    echo 'error running "git rev-parse --show-cdup"'
                    exit 129
                fi

                # Check for untracked files
                git ls-files --others $exclude --error-unmatch . >/dev/null 2>&1
                case $? in
                    0)  # some untracked/ignored file is present
                        warn 'some untracked'"$or_ignored"' file is present.'
                        exit 1
                    ;;
                    1)  # no untracked files
                        exit 0
                    ;;
                    *)
                        echo 'error running "git diff-index"!'
                        exit 129
                    ;;
                esac

            )
            case $? in
                0) clean=yes ;;
                1) clean=no ;;
                *) exit $? ;;
            esac
        fi

        test "$clean" = yes &&
        if c="$(git rev-parse --verify HEAD)"; then
            echo 'Current commit: '"$c"
        else
            echo 'error running "git rev-parse --verify"!'
        fi
    ;;
    1)
        warn 'some tracked file has an uncommitted change.'
    ;;
    *)
        echo 'error running "git diff-index"!'
        exit 129
    ;;
esac
如果您希望退出代码在所有情况下都有意义,那么可以添加更多的
exit
s

如果您不关心所有的错误处理或未跟踪/忽略的处理,那么一些简短的内容就足够了:

if git diff-index --quiet; then
    printf 'Current commit: %s\n' "$(git rev-parse --verify HEAD)
else
    echo 'Warning: …'
fi
您还可以以简洁的方式检查未跟踪的文件(可以调整为忽略等),而无需进行错误处理:

git ls-files --others --exclude-standard --error-unmatch \
    "./$(git rev-parse --show-cdup)" >/dev/null 2>&1