痛點:
項目使用的git項目過多,我的目前60多個。
文件夾當時創建的時候比較雜亂。 后期找項目比較困難。
執行方案:
遷移項目,根據git地址內的文件夾進行對應的文件夾創建,
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, subprocess
def get_folder_list(path=os.getcwd(), _set=set()):
# 遍歷所有文件夾。
for item in filter(lambda x: os.path.isdir(os.path.join(path, x)), os.listdir(path)):
item_path = os.path.join(path, item)
# 執行git remote -v 獲取項目的git地址。
result = subprocess.getoutput("cd {} && git remote -v".format(item_path)).split(" ")
if len(result) > 2 and (result[2].startswith("ssh") or result[2].startswith("http")):
if result[2].startswith("http"):
print(item_path, result[2])
_set.add(result[2].replace(" (push)", ""))
else:
# 已經獲取到git地址的文件夾無需繼續遍歷,未獲取到的繼續遞歸遍歷。
get_folder_list(item_path, _set)
return _set
def gitclone(new_path, gitPath):
# 去掉前面的http://xxx.com/ ssh://xxx.com/ 和后面的xxx.git
path = new_path + "/".join(gitPath.split("/")[3:-1])
if not os.path.exists(path):
os.mkdir(path)
cmd = "cd {} && git clone {}".format(path, gitPath)
print(cmd)
# 執行git clone 命令, 記得提前配置好全局的用戶名和密碼。
subprocess.getoutput(cmd)
if __name__ == "__main__":
# 設置要檢索的項目路徑。
old_path = "D://proProject"
new_path = "D://proProjects"
_set = get_folder_list(old_path, set())
print("來源項目git集合信息:", len(_set), _set)
for one in _set:
gitclone(new_path, one)
pass
_new_set = get_folder_list(new_path, set())
print("目標項目git集合信息:", len(_new_set), _new_set)
print("缺失下載失敗git地址集合:", _set - _new_set)
浙公網安備 33010602011771號