首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法通过git status命令调用脚本?

有没有办法通过git status命令调用脚本?
EN

Stack Overflow用户
提问于 2013-01-31 04:48:06
回答 2查看 82关注 0票数 2

我通过git-svn工具将我的svn存储库作为git存储库进行管理,但是没有办法处理我的svn外部。这个问题的解决方法是将每个外部组件视为一个git-svn代码库。这是使用脚本完成的,结果如下所示:

代码语言:javascript
复制
> src/
> -- .git/
> -- Source1.x
> -- Source2.x
> -- .git_external/
> ---- git-svn_external1/
> ------ .git/
> ------ ExternalSource1.x
> ---- git-svn_external2/
> ------ .git/
> ------ AnotherExternalSource1.x
> ------ AnotherExternalSource2.x

由于缺少处理svn外部的工具,我需要通过一个手动执行的bash脚本来验证每个修改,它类似于:

代码语言:javascript
复制
#!/bin/sh
for i in `ls .` do
  if [ -d $i ] then
    cd $i
    if [ -d .git ] then
      git status .
    fi
  cd ..
  fi
done

当我在git-svn主存储库上执行git status命令时,如何自动实现这一点?

我没有找到任何与这种情况相关的钩子,所以我想我需要找到一个解决方法来解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-31 05:19:01

一般来说,git会尝试提供尽可能少的钩子,只在您不能使用脚本的情况下才提供它们。在这种情况下,只需编写一个执行命令并运行git status的脚本即可。运行此脚本,而不是git status

如果您将其命名为git-st并将其放入您的路径中,则可以通过git st调用它。

票数 4
EN

Stack Overflow用户

发布于 2013-01-31 06:18:22

我使用过的一个技巧是围绕git编写一个外壳包装函数。假设您正在使用Bash (与其他~/.bashrc类似),将以下代码添加到您的shell中

代码语言:javascript
复制
git () {
    if [[ $1 == status ]]
    then
        # User has run git status.
        #
        # Run git status for this folder.  The "command" part means we actually
        # call git, not this function again.
        command git status .

        # And now do the same for every subfolder that contains a .git
        # directory.
        #
        # Use find for the loop so we don't need to worry about people doing
        # silly things like putting spaces in directory names (which we would
        # need to worry about with things like `for i in $(ls)`).  This also
        # makes it easier to recurse into all subdirectories, not just the
        # immediate ones.
        #
        # Note also that find doesn't run inside this shell environment, so we
        # don't need to worry about prepending "command".
        find * -type d -name .git -execdir git status . \;
    else
        # Not git status.  Just run the command as provided.
        command git "$@"
    fi
}

现在,当您运行git status时,它实际上会对当前文件夹和包含其自己的.git文件夹的任何子文件夹运行git status

或者,您可以将其添加到新命令中,方法是以Chronial suggests的形式编写脚本,或者将其放入Git别名中。要执行后一种操作,请运行类似以下命令的命令:

代码语言:javascript
复制
git config --global alias.full-status '!cd ${GIT_PREFIX:-.}; git status .; find * -type d -name .git -execdir git status . \;'

然后,您将能够运行git full-status来执行相同的操作。

( cd ${GIT_PREFIX:-.}部件用于返回到运行该命令的目录;默认情况下,Git别名从存储库的根目录运行。其余部分与上面的函数解决方案相同。)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14613465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档