如果本地Git存储库发生了更改,如何检入Bash脚本?

时间:2022-08-01 07:14:27

There are some scripts that do not work correctly if they check for changes.

如果检查更改,有些脚本不能正常工作。

I tried it like this:

我这样尝试:

VN=$(git describe --abbrev=7 HEAD 2>/dev/null)git update-index -q --refreshCHANGED=$(git diff-index --name-only HEAD --)if [ ! -z $CHANGED ];    then VN="$VN-mod"fi

Is there some kind of boolean check if there has been changes since the last commit, or how can I really test if there are new changes to my local repository?

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

I'm doing all this for a version creation script (that I found somewhere here).

我正在为一个版本创建脚本(我在这里找到)做所有这些工作。

8 个解决方案

#1


138  

What you're doing will almost work: you should quote $CHANGED in case it's empty, and -z tests for empty, which means no changes. What you meant was:

您所做的几乎都是有用的:您应该引用$CHANGED以防它是空的,并且-z测试为empty,这意味着没有更改。你的意思是:

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

A quote from Git's GIT-VERSION-GEN:

引用Git的Git的Git版本- gen:

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

It looks like you were copying that, but you just forgot that detail of quoting.

看起来你复制了那个,但是你忘记了引用的细节。

Of course, you could also just do this:

当然,你也可以这样做:

if git diff-index --quiet HEAD --; then    # No changeselse    # Changesfi

Or if you only care about the "something has changed" case:

或者如果你只关心“事情已经改变”的情况:

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

Using --quiet has the benefit that Git can stop processing as soon as it encounters a single diff, so it may not have to check your entire work tree.

使用—quiet的好处是,Git可以在遇到一个diff时立即停止处理,因此它不必检查整个工作树。

#2


171  

Using git status:

使用git状态:

cd /git/directoryif [[ `git status --porcelain` ]]; then  # Changeselse  # No changesfi

#3


14  

Although Jefromi's answer is good, I'm posting this just for reference.

虽然Jefromi的答案很好,但我只是把它贴出来作为参考。

From the Git source code there is a sh script which includes the following.

在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}

#4


6  

I had a similar problem, but I had to check for added files also. So I did the following:

我有一个类似的问题,但是我必须检查添加的文件。所以我做了如下的事:

cd /local/repoRUN=0git diff --no-ext-diff --quiet --exit-code || RUN=1if [ $RUN = 0 ]; then    RUN=`git ls-files --exclude-standard --others| wc -l`fiif [ $RUN = 0 ]; then    exit 0fi

#5


2  

git status is your friend

git状态是您的朋友

Change to the Git directory for git status to work:

将Git目录更改为Git状态工作:

cd c:/path/to/.git

Set a variable to set the work tree so you don't get 'This operation must be run in a work tree' error:

设置一个变量来设置工作树,这样你就不会得到“这个操作必须在工作树中运行”的错误:

WORKTREE=c:/path/to/worktree

Capture the git status output in a Bash variable

捕获Bash变量中的git状态输出

Use --porcelain which guarantees to be in a standard format and parsable:

用途——确保标准格式和可分割的瓷器:

CHANGED=$(git --work-tree=${WORKTREE} status --porcelain)

If -n (not null), we have changes.

如果-n(非空),我们有变化。

if [ -n "${CHANGED}" ]; then  echo 'changed';else  echo 'not changed';fi

#6


1  

Here is a nice set of Bash script functions that check if there is a diff, prints it to the user and prompts the user if they would like to commit changes before deployment. It is built for a Heroku and Python application, but it needs little change for any other application.

这里有一组很棒的Bash脚本函数,它们检查是否存在差异,将其打印给用户,并提示用户是否希望在部署之前提交更改。它是为Heroku和Python应用程序构建的,但是对任何其他应用程序都不需要太大的更改。

commit(){    echo "Please enter a commit message..."    read msg    git add . --all    git commit -am $msg}check_commit(){    echo ========== CHECKING FOR CHANGES ========    changes=$(git diff)    if [ -n "$changes" ]; then        echo ""        echo "*** CHANGES FOUND ***"        echo "$changes"        echo ""        echo "You have uncomitted changes."        echo "Would you like to commit them (y/n)?"        read n        case $n in            "y") commit;;            "n") echo "Changes will not be included...";;            *) echo "invalid option";;        esac    else        echo "... No changes found"    fi}deploy(){    check_commit    echo ========== DEPLOYING TO HEROKU ========    git push heroku master    heroku run python manage.py syncdb}

You can copy from Gists on: https://gist.github.com/sshadmand/f33afe7c9071bb725105

您可以从Gists中复制:https://gist.github.com/sshadmand/f33afe7c9071bb725105

#7


0  

Here's how I do it...

我是这样做的……

CHANGES=`git status | grep "working directory clean"`if [ ! CHANGES -eq "" ] then    # do stuff hereelse    echo "You have uncommitted changes..."fi

#8


0  

This works nicely. It will also list the files affected:

这很好地工作。它还将列出受影响的文件:

if git diff-index --name-status --exit-code HEAD;then    echo Git working copy is clean...;else    echo ;    echo ERROR: Git working copy is dirty!;    echo Commit your changes and try again.;fi;

#1


138  

What you're doing will almost work: you should quote $CHANGED in case it's empty, and -z tests for empty, which means no changes. What you meant was:

您所做的几乎都是有用的:您应该引用$CHANGED以防它是空的,并且-z测试为empty,这意味着没有更改。你的意思是:

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

A quote from Git's GIT-VERSION-GEN:

引用Git的Git的Git版本- gen:

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

It looks like you were copying that, but you just forgot that detail of quoting.

看起来你复制了那个,但是你忘记了引用的细节。

Of course, you could also just do this:

当然,你也可以这样做:

if git diff-index --quiet HEAD --; then    # No changeselse    # Changesfi

Or if you only care about the "something has changed" case:

或者如果你只关心“事情已经改变”的情况:

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

Using --quiet has the benefit that Git can stop processing as soon as it encounters a single diff, so it may not have to check your entire work tree.

使用—quiet的好处是,Git可以在遇到一个diff时立即停止处理,因此它不必检查整个工作树。

#2


171  

Using git status:

使用git状态:

cd /git/directoryif [[ `git status --porcelain` ]]; then  # Changeselse  # No changesfi

#3


14  

Although Jefromi's answer is good, I'm posting this just for reference.

虽然Jefromi的答案很好,但我只是把它贴出来作为参考。

From the Git source code there is a sh script which includes the following.

在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}

#4


6  

I had a similar problem, but I had to check for added files also. So I did the following:

我有一个类似的问题,但是我必须检查添加的文件。所以我做了如下的事:

cd /local/repoRUN=0git diff --no-ext-diff --quiet --exit-code || RUN=1if [ $RUN = 0 ]; then    RUN=`git ls-files --exclude-standard --others| wc -l`fiif [ $RUN = 0 ]; then    exit 0fi

#5


2  

git status is your friend

git状态是您的朋友

Change to the Git directory for git status to work:

将Git目录更改为Git状态工作:

cd c:/path/to/.git

Set a variable to set the work tree so you don't get 'This operation must be run in a work tree' error:

设置一个变量来设置工作树,这样你就不会得到“这个操作必须在工作树中运行”的错误:

WORKTREE=c:/path/to/worktree

Capture the git status output in a Bash variable

捕获Bash变量中的git状态输出

Use --porcelain which guarantees to be in a standard format and parsable:

用途——确保标准格式和可分割的瓷器:

CHANGED=$(git --work-tree=${WORKTREE} status --porcelain)

If -n (not null), we have changes.

如果-n(非空),我们有变化。

if [ -n "${CHANGED}" ]; then  echo 'changed';else  echo 'not changed';fi

#6


1  

Here is a nice set of Bash script functions that check if there is a diff, prints it to the user and prompts the user if they would like to commit changes before deployment. It is built for a Heroku and Python application, but it needs little change for any other application.

这里有一组很棒的Bash脚本函数,它们检查是否存在差异,将其打印给用户,并提示用户是否希望在部署之前提交更改。它是为Heroku和Python应用程序构建的,但是对任何其他应用程序都不需要太大的更改。

commit(){    echo "Please enter a commit message..."    read msg    git add . --all    git commit -am $msg}check_commit(){    echo ========== CHECKING FOR CHANGES ========    changes=$(git diff)    if [ -n "$changes" ]; then        echo ""        echo "*** CHANGES FOUND ***"        echo "$changes"        echo ""        echo "You have uncomitted changes."        echo "Would you like to commit them (y/n)?"        read n        case $n in            "y") commit;;            "n") echo "Changes will not be included...";;            *) echo "invalid option";;        esac    else        echo "... No changes found"    fi}deploy(){    check_commit    echo ========== DEPLOYING TO HEROKU ========    git push heroku master    heroku run python manage.py syncdb}

You can copy from Gists on: https://gist.github.com/sshadmand/f33afe7c9071bb725105

您可以从Gists中复制:https://gist.github.com/sshadmand/f33afe7c9071bb725105

#7


0  

Here's how I do it...

我是这样做的……

CHANGES=`git status | grep "working directory clean"`if [ ! CHANGES -eq "" ] then    # do stuff hereelse    echo "You have uncommitted changes..."fi

#8


0  

This works nicely. It will also list the files affected:

这很好地工作。它还将列出受影响的文件:

if git diff-index --name-status --exit-code HEAD;then    echo Git working copy is clean...;else    echo ;    echo ERROR: Git working copy is dirty!;    echo Commit your changes and try again.;fi;