git: How to split off library from project? filter-branch, subtree?

前端 未结 5 1568
無奈伤痛
無奈伤痛 2020-12-24 02:34

So, I\'ve a bigger (closed source) project, and in the context of this project created a library which could also be useful elsewhere, I think.

I now want to split o

5条回答
  •  [愿得一人]
    2020-12-24 03:01

    I've done something similar, but splitting several dirs of stuff into an entirely separate repo on an encrypted partition (/secure/tmp/newrepo), so they were not available to a laptop thief. I wrote the shell script and then did: git filter-branch --tree-filter '~/bin/tryit /secure/tmp/newrepo personal private' -- 95768021ff00216855868d12556137115b2789610..HEAD (the SHA avoids commits before either directory came into existance)


    #!/bin/sh
    # to be used with  e.g:
    # git filter-branch --tree-filter '~/bin/tryit /secure/tmp/newrepo personal private' 
    # Don't do it on any repository you can't repeatedly do: 
    #   rm -rf foo ; git clone /wherever/is/foo 
    # when it breaks
    SRC=`pwd`
    DEST=$1
    shift
    MSG=/dev/shm/msg.txt
    TAR=/dev/shm/tmp.tar
    LIST=/dev/shm/list.txt
    LOG=/dev/shm/log
    DONE=''
    
    echo $GIT_AUTHOR_DATE >> $LOG
    git show --raw $GIT_COMMIT > $MSG 
    
    for A in $* 
    do 
    
    if [ -d $A ] 
    then 
    DONE=${DONE}x
    tar -cf $TAR $A 
    tar -tf $TAR > ${LIST}
    cat ${LIST} >> ${LOG}
    rm -rf ${A}
    cd ${DEST}
    tar -xf $TAR
    else
    echo $A non-existant >> ${LOG}
    fi
    cd $SRC
    done
    
    if [ -z "${DONE}" ]
    then
    echo Empty >>$LOG
    else
    cd ${DEST}
    unset GIT_INDEX_FILE
    unset GIT_DIR
    unset GIT_COMMIT
    unset GIT_WORK_TREE
    touch foo
    git add .
    git commit -a -F ${MSG}  >> ${LOG}
    fi
    exit 0
    

    For your purposes you'd probably want to have a different spec for the tar (e.g. --exclude= ) and then use cat ${LIST} | xargs rm to only remove stuff in the tar, but getting that right is not too tricky, I hope.

    The unset stuff and exit 0 are important, since filter-branch sets those to your source repo (not what you want!) and will die if sh passes on a non-zero exit code from the last command in your script.

提交回复
热议问题