Git的命令行 - 知道的话,子模块? [英] Git command line - know if in submodule?

查看:100
本文介绍了Git的命令行 - 知道的话,子模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在git的知道,如果你在一个子模块是?你可以这样想 git的子模块的foreach 中的父目录,但我似乎无法拿出一个通用的方式来表明你在一个子模块,如果你'再于一体,或以任何的子模块中的子目录的

Is there any way in git to know if you're in a submodule? You can do thinks like git submodule foreach in the parent directory but I can't seem to come up with a generic way to show that you're in a submodule if you're in one, or in any of the child directories inside the submodule.

我想你可以找到与回购根 git的REV-解析 - 显示,顶级,然后CD-ING升了一级,找到的根再回购,然后比较子模块到当前目录的列表中,但似乎极粘...

I guess you could find the repo root with git rev-parse --show-toplevel, and then cd-ing up a level, and finding the root of that repo again and then comparing the list of submodules to the current directory, but that seems so sticky...

推荐答案

下面是你可以用它来检测这个外壳功能:

Here is a shell function that you can use to detect this:

function is_submodule() 
{       
     (cd "$(git rev-parse --show-toplevel)/.." && 
      git rev-parse --is-inside-work-tree) | grep -q true
}

修改在回答你提出的脚本:

看起来不错。


  • <击>有一个错误

  • There is a bug in

for line in $submodules; do cd "$parent_git/$line"; \
    if [[ `pwd` = $_git_dir ]]; then return 0; fi; \
done


,因为它不会回光盘(因此它只有在第一子模块的工作
一个匹配)我的版本检查不改变目录。这可以通过 CD -ing在子shell,但返回退出code或者做的是越来越复杂这样

because it won't cd back (so it would only work if the first submodule is a match). My version checks without changing directories; That could be done done by cd-ing in a subshell, but returning the exitcode is getting complicated that way


  • 我不知道你来自哪里获得 $ _ GIT_DIR - 我用基本名(1)吃出
    信息(见下文)。

  • I don't know where you get $_git_dir from - I used basename(1) to get that information (see below).

有也包含在名称中的空格子模块的问题。的在我的版本,还有与子模块的名称换行留下了一个问题,但我不在乎不足以解决这个问题。 (注意'地道'的方式,以避免在而读在子shell,而不需要新的bash-主义像 readarray

There was also a problem with submodules containing a space in the name. In my version, there is still a problem with newlines in submodule names left, but I don't care enough to fix that. (Note the 'idiomatic' way to avoid having the while read in a subshell without needing new bash-isms like readarray)

使用这个的时候终于宣布所有的瓦尔本地修复潜在的问题
里面的其它脚本(例如,当外部脚本使用 $ PATH 变量...)

finally declaring all the vars local fixes potential problems when using this inside other scripts (e.g. when the outer script uses the $path variable...)

我改名 _git_dir TOP_LEVEL (这是减少混乱,因为GIT_DIR意味着别的东西)

I renamed _git_dir to top_level (which is less confusing, because GIT_DIR means something else)


  • 我不知道混帐是否支持它(的我不这么认为的),但如果该子模块目录是符号链接(因为$ TOP_LEVEL /这个脚本可能会失败.. 可能会解决该资料库包含外)

  • I don't know whether git supports it (I don't think so) but this script could fail if the submodule directory is a symlink (because "$top_level/.." might resolve outside the containing repository)

用换行子模块的名字将不会被正确识别

submodule names with newlines will not be recognized properly

我也建议你捕获错误(或者使用设置-e','陷阱回归1
ERR或类似) - 的不是在我的脚本/练习阅读

I also suggest you trap errors (either with 'set -e', 'trap "return 1" ERR' or similar) -- not in my script/exercise for reader

#!/bin/bash

function is_submodule() {
    local top_level parent_git module_name path
    # Find the root of this git repo, then check if its parent dir is also a repo
    top_level="$(git rev-parse --show-toplevel)"
    module_name="$(basename "$top_level")"
    parent_git="$(cd "$top_level/.." && git rev-parse --show-toplevel 2> /dev/null)"
    if [[ -n $parent_git ]]; then
        # List all the submodule paths for the parent repo
        while read path
        do
            if [[ "$path" != "$module_name" ]]; then continue; fi
            if [[ -d "$top_level/../$path" ]];    then return 0; fi
        done < <(cd $parent_git && git submodule --quiet foreach 'echo $path' 2> /dev/null)
        #return 1
    fi
    return 1
}

用法

if is_submodule; then
    echo "In a submodule!"
else
    echo "Not in a submodule"
fi

这篇关于Git的命令行 - 知道的话,子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆