自动将所有子模块添加到仓库中

6

我有一个大的目录~/.vim,里面有许多其他git仓库的子目录。我想把我的~/.vim目录制作成一个git仓库,但不想逐个处理我的其他git子目录。

有没有一种方法可以递归地添加所有子模块?


1
你想将你的 .vim 文件夹变成一个 git 仓库,并将其中所有子目录(它们都是 git 仓库)作为子模块添加进来?是这样吗? - Fatih Arslan
是的,我举个例子,我已经从GitHub上克隆了~/.vim/bundle/matchit - mazlix
7个回答

16

假设 .vim 已经是 一个有效的git仓库,而您想将所有git仓库添加到主git仓库中,则下面的for循环可能是您需要的:

首先,cd 到您的 git 仓库的根目录。

可粘贴的预览命令- 仅回显,不会进行任何更改:

for x in $(find . -type d) ; do if [ -d "${x}/.git" ] ; then cd "${x}" ; origin="$(git config --get remote.origin.url)" ; cd - 1>/dev/null; echo git submodule add "${origin}" "${x}" ; fi ; done

可粘贴的命令以添加子模块:

for x in $(find . -type d) ; do if [ -d "${x}/.git" ] ; then cd "${x}" ; origin="$(git config --get remote.origin.url)" ; cd - 1>/dev/null; git submodule add "${origin}" "${x}" ; fi ; done

这个循环首先找到目录,然后查找一个名为".git"的文件夹,确定原始URL,最后添加子模块。

for x in $(find . -type d) ; do
    if [ -d "${x}/.git" ] ; then
        cd "${x}"
        origin="$(git config --get remote.origin.url)"
        cd - 1>/dev/null
        git submodule add "${origin}" "${x}"
    fi
done

2
以下是我的解决方案:for i in $(ls -d bundle/*); do;if [ -d "$i"/.git ]; then; git submodule add $(cd $i && git remote show origin | grep Fetch | awk '{print $3}') ./$i; fi; done - NeonNinja
将初始的for循环中的find命令更改为for x in $(find . -mindepth 1 -maxdepth 1 -type d)-mindepth是必需的,以跳过当前顶级目录('.'),而maxdepth不允许find在目录结构中深入。如果您有深度嵌套的存储库,则可能要跳过-maxdepth - Dmitry Avtonomov

1
cd /to/super/repo
root_path=$(pwd)
for submodule in $(find -mindepth 2 -type d -name .git); do
    submodule_dir=$root_path/$submodule/..
    remote_name=$(cd $submodule_dir && git rev-parse --abbrev-ref --symbolic-full-name @{u}|cut -d'/' -f1 )
    remote_uri=$(cd $submodule_dir && git remote get-url $remote_name)

    echo "Adding $submodule with remote $remote_name..."

    # If already added to index, but not to .gitmodules...
    git rm --cached $submodule_dir &> /dev/null

    git submodule add $remote_uri $submodule_dir
done

0

我有一些嵌套在更深层次子目录中的git仓库,因此为了从任何深度级别找到并添加它们,我必须使用以下命令:

for i in $(find ~/Sources/* -type d -name .git) do cd $i && cd .. && git submodule add $(pwd) done

我怀疑这个操作是否有用。它会将子模块添加为远程仓库,但使用的是http或ssh URI。 - lumbric

0

遗憾的是,这个功能似乎不存在。Git 应该有一个命令可以添加 .gitmodules 中引用的所有模块,类似于:

### wishful thinking

git submodules init --all
git submodules add --all

:(


0

@Mussa 当我在做的时候它正在显示给我看

The following path is ignored by one of your .gitignore files:

Use -f if you really want to add it.

0

由于在许多项目中这样做可能是一个重复的任务,我基于@Fatih的精彩答案进行了一些修改,并创建了两个帮助函数,并将它们存储在我的机器上,使用自定义bash shell文件。

这些函数可以在任何文件夹中调用,无需记住或键入所有实现细节。

以下是使生活更轻松的步骤:

在您的用户根路径中创建一个文件,例如

.myBashCommands.sh

请将以下内容写入其中,如果您已经拥有自己的专用文件来存储自定义函数,请随意使用它。

#!/bin/bash

# Find and log all git sub-modules if exist without making any changes.
# Credits: https://dev59.com/qWPVa4cB1Zd3GeqP9t_T#10607225
function logAllGitSubModulesIfExist() {
 for x in $(find . -type d) ; do
 if [ -d "${x}/.git" ] ; then
     cd "${x}" ;
     origin="$(git config --get remote.origin.url)" ;
     cd - 1>/dev/null;
     echo "found gitsubmodule at =>" "${origin}" "${x}" "\n";
 fi ;
 done
}

# This loop first finds directories only, looks for a .git directory, identifies the original URL and then adds the submodule.
# Credits: https://dev59.com/qWPVa4cB1Zd3GeqP9t_T#10607225
function addAllGitSubModules() {
for x in $(find . -type d) ; do
    if [ -d "${x}/.git" ] ; then
        cd "${x}"
        origin="$(git config --get remote.origin.url)"
        cd - 1>/dev/null
        git submodule add "${origin}" "${x}"
    fi
done
}

关闭文件,同时仍然在同一工作目录中,刷新终端以识别您新添加的方法。

以下是操作步骤:

source .myBashCommands.sh

完成了!现在您可以在任何项目和任何文件夹中重复使用这两个函数。

要调用这些方法,只需输入名称即可。

例如,要记录所有可用的子模块,只需在终端中键入以下内容:

logAllGitSubModulesIfExist

类似地,要查找并添加所有现有的子模块,只需键入以下内容:

addAllGitSubModules

0
我建议创建一个Python脚本。
import 
glob,remodules = glob.glob('*/**/.git', recursive=True)
content = ''def get_url(submod):
    for line in open(submod+'/config','r'):
       if '\turl' in line:
           return line.split('=')[1].strip()
for submod in modules:
    content += f'''
[submodule "{submod.strip(".git/")}"]
    path = {submod.strip(".git/")}
    url = {get_url(submod)}
    '''
print(content)
with open('.gitmodules','w') as f:
    f.write(content)

使用以下命令将更改推送到您的GitHub

git add . 
git commit -m "Adding submodule"
git push origin branch_name 

有时候会出现一些文件没有被添加的情况,您可以简单地添加生成的.gitmodules文件并手动添加它们。如果您发现某些文件出现错误,请根据位置编辑上述代码或者暂时删除它们并手动添加以获得最佳效果。

.gitmodules的示例

[submodule "src/abc"]
    path = src/abc
    url = https://github.com/abc.git

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接