gpt4 book ai didi

git - 重写历史 git filter-branch create/split into submodules/subprojects

转载 作者:太空狗 更新时间:2023-10-29 14:01:10 26 4
gpt4 key购买 nike

我目前正在将一个 cvs 项目导入到 git 中。
导入后,我想重写历史以将现有目录移动到单独的子模块中。

假设我有这样的结构:


file1<br/>
file2<br/>
file3<br/>
dir1<br/>
dir2<br/>
library<br/>

现在我想重写历史,这样目录library始终是一个 git 子模块。比如说,将指定的目录拆分成它们自己的子模块/子项目

这是我目前的代码:

文件 rewrite-submodule(调用)

cd project
git filter-branch --tree-filter $PWD/../$0-tree-filter --tag-name-filter cat -- --all

文件重写子模块树过滤器

    #!/bin/bash    function gitCommit()    {        unset GIT_DIR        unset GIT_WORK_TREE        git add -A        if [ -n "$(git diff --cached --name-only)" ]        then            # something to commit            git commit -F $_msg        fi    }    _git_dir=$GIT_DIR    _git_work_tree=$GIT_WORK_TREE    unset GIT_DIR    unset GIT_WORK_TREE    _dir=$PWD    if [ -d "library" ]    then        _msg=$(tempfile)        git log ${GIT_COMMIT}^! --format="%B" > $_msg        git rm -r --cached lib        cd library        if [ -d ".git" ]        then            gitCommit        else            git init            gitCommit        fi        cd ..        export GIT_DIR=$_git_dir        export GIT_WORK_TREE=$_git_work_tree        git submodule add -f ./lib    fi    GIT_DIR=$_git_dir    GIT_WORK_TREE=$_git_work_tree    

此代码创建 .gitmodules 文件,但不创建主存储库中的子模块提交条目(行 Subproject commit <sha1-hash> ,由 git diff 输出)和目录 library 中的文件仍然在主存储库中进行版本控制,而不是在子项目存储库中。

提前感谢任何提示

.gitmodules 看起来像这样:

    [submodule "library"]        path = library        url = ./library    

最佳答案

我解决了我自己的问题,这里是解决方案:

git-submodule-split library another_library

脚本 git-submodule-split:

    #!/bin/bash    set -eu    if [ $# -eq 0 ]    then        echo "Usage: $0 submodules-to-split"    fi    export _tmp=$(mktemp -d)    export _libs="$@"    for i in $_libs    do        mkdir -p $_tmp/$i    done    git filter-branch --commit-filter '    function gitCommit()    {        git add -A        if [ -n "$(git diff --cached --name-only)" ]        then            git commit -F $_msg        fi    } >/dev/null    # from git-filter-branch    git checkout-index -f -u -a || die "Could not checkout the index"    # files that $commit removed are now still in the working tree;    # remove them, else they would be added again    git clean -d -q -f -x    _git_dir=$GIT_DIR    _git_work_tree=$GIT_WORK_TREE    _git_index_file=$GIT_INDEX_FILE    unset GIT_DIR    unset GIT_WORK_TREE    unset GIT_INDEX_FILE    _msg=$(tempfile)    cat /dev/stdin > $_msg    for i in $_libs    do        if [ -d "$i" ]        then            unset GIT_DIR            unset GIT_WORK_TREE            unset GIT_INDEX_FILE            cd $i            if [ -d ".git" ]            then                gitCommit            else                git init >/dev/null                gitCommit            fi            cd ..            rsync -a -rtu $i/.git/ $_tmp/$i/.git/            export GIT_DIR=$_git_dir            export GIT_WORK_TREE=$_git_work_tree            export GIT_INDEX_FILE=$_git_index_file            git rm -q -r --cached $i            git submodule add ./$i >/dev/null            git add $i        fi    done    rm $_msg    export GIT_DIR=$_git_dir    export GIT_WORK_TREE=$_git_work_tree    export GIT_INDEX_FILE=$_git_index_file    if [ -f ".gitmodules" ]    then        git add .gitmodules    fi    _new_rev=$(git write-tree)    shift    git commit-tree "$_new_rev" "$@";    ' --tag-name-filter cat -- --all    for i in $_libs    do        if [ -d "$_tmp/$i/.git" ]        then            rsync -a -i -rtu $_tmp/$i/.git/ $i/.git/            cd $i            git reset --hard            cd ..        fi    done    rm -r $_tmp    git for-each-ref refs/original --format="%(refname)" | while read i; do git update-ref -d $i; done    git reflog expire --expire=now --all    git gc --aggressive --prune=now    

关于git - 重写历史 git filter-branch create/split into submodules/subprojects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13425260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com