如何从git worktree命令生成多个文件夹?

时间:2019-06-10 20:30:31

标签: python git cmd git-worktree

这是我的数据框文件,其中包含CommitId:

CommitId
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8
4bb968a47ce00279d6051df95bd782650700179e
c3d7ec38417ecff03d1cd3be0163e6ce07578eb3
00568c9886e739d6b5dd61b4a4326d598552fb6f
00568c9886e739d6b5dd61b4a4326d598552fb6f
00568c9886e739d6b5dd61b4a4326d598552fb6f
00568c9886e739d6b5dd61b4a4326d598552fb6f
6e062098453febbfb0169cd0af56f70f2e3fc77f
63f658918c2f4b851b0d0fffbffab4df0cfe13ca

我需要检出每个提交并将代码版本复制到另一个目录中,因此对于这个示例,我每个目录中需要11个版本的代码,所以11个目录中的名称不同

我尝试了以下示例代码:

import os
from distutils.dir_util import copy_tree
path1='C:/Users/AQ42770/Desktop/RefactoringMiner/bin/Android-ContactPicker'
os.chdir(path1)

commande1='git worktree add C:/Users/AQ42770/Documents/commit d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8' 
os.system(commande1)

commande2='git worktree add C:/Users/AQ42770/Documents/commit1 
4bb968a47ce00279d6051df95bd782650700179e'
os.system(commande2) 

但是这不是很实际,因为我有很多提交,例如我有11个提交,而我有100个以上的提交。所以我尝试了这段代码,但没有返回任何内容:

n=1
list=["95fe00030ad97c998cd0b1b7df030dcda0db7baa","47b91018e3cb45ee0f7c3135488855554ad6617d"]
path="C:/Users/AQ42770/Documents/commit"
for n in range(0,2):
   t=path+str(n)
   os.system('git worktree add t list[n]')

PS:我将提交内容放在列表中进行测试,然后再从数据框中读取

1 个答案:

答案 0 :(得分:0)

假设数据文件为C:/Users/AQ42770/Desktop/mydata.txt

import os
import re

wortreebase = 'C:/Users/AQ42770/Documents/'
mydata = 'C:/Users/AQ42770/Desktop/mydata.txt'
gitdir = 'C:/Users/AQ42770/Desktop/RefactoringMiner/bin/Android-ContactPicker/.git'

# find all commits
with open(mydata) as f:
    commits = re.findall(r'[0-9a-f]{40}', f.read())

for i, commit in enumerate(commits):
    # name the worktree
    worktree = wortreebase + '%s_%s' % (i, commit))
    # create the worktree
    cmd = 'git --git-dir=%s worktree add %s %s' % (gitdir, worktree, commit)
    os.system(cmd)