Py学习  »  Git

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

Oumayma Hamdi • 4 年前 • 600 次点击  

这是包含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:我将提交放在一个列表中,先进行测试,然后从数据帧中读取

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50796
 
600 次点击  
文章 [ 1 ]  |  最新文章 4 年前
ElpieKay
Reply   •   1 楼
ElpieKay    4 年前

假设数据文件是 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)