批量创建Android源码仓库
修改manifest.xml文件
manifest.xml文件在源代码的.repo目录中
<manifest>
<remote fetch="ssh://localhost/mnt/GitRepository/ANDROID7.1" name="ssh" review="remote"/>
<remote fetch="/mnt/GitRepository/ANDROID7.1" name="local" review="local"/>
<default remote="local" revision="master" sync-j="8" />
</manifest>
此处添加了两个仓库员,默认使用第二个仓库源,可以参考这里
准备仓库
将源代码复制到/mnt/GitRepository/ANDROID7.1中,并运行python3 app.py --xml_path 你修改后xml文件地址 --repo_path /mnt/GitRepository/ANDROID7.1
此时你的源代码仓库已经建立好,运行命令
repo init -u /mnt/GitRepository/ANDROID7.1/platform/manifest.git/
repo sync
就可以下拉你的代码
app.py源代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import os
import argparse
import xml.etree.ElementTree as ET
parser = argparse.ArgumentParser()
parser.add_argument("--xml_path", help="the path of the xml file", default="manifest.xml")
parser.add_argument("--repo_path", help="the path of the repo folder", default="project")
parser.add_argument("-j","--thread", help="the count of git thread, the default is 8", default=8, type=int)
args = parser.parse_args()
xml_path = args.xml_path
repo_path = args.repo_path
thread_count = args.thread
def _init_repo(sub_repo_path):
os.chdir(sub_repo_path) # 切换工作目录
print("==>git init " + os.getcwd())
if not os.path.exists(".gitignore"): # 需要存在文件才能提交
os.system("touch .gitignore")
if os.path.exists(".git"):
os.system("rm -rf .git")
if 0 != os.system("git init"):
print("Error(init):%s" % sub_repo_path)
return False
if 0 != os.system("git add ."):
print("Error(add):%s" % sub_repo_path)
return False
if 0 != os.system("git commit -m \"init repo\""):
print("Error(commit):%s" % sub_repo_path)
return False
return True
if __name__ == '__main__':
tree = ET.parse(xml_path)
root = tree.getroot()
for child in root:
if child.tag == "default":
child.attrib["sync-j"] = str(thread_count)
elif child.tag == "project":
if "remote" in child.attrib.keys():
child.attrib.pop("remote")
if "revision" in child.attrib.keys():
child.attrib.pop("revision")
if "upstream" in child.attrib.keys():
child.attrib.pop("upstream")
if not _init_repo(repo_path + "/" + child.attrib["path"]):
break
if not os.path.exists(repo_path + "/platform/manifest.git"):
os.makedirs(repo_path + "/platform/manifest.git")
if os.path.exists(repo_path + "/platform/manifest.git/default.xml"):
os.remove(repo_path + "/platform/manifest.git/default.xml")
tree.write(repo_path + "/platform/manifest.git/default.xml", encoding="utf-8", xml_declaration=True)
_init_repo(repo_path + "/platform/manifest.git")
# repo init -u git@localhost:$repo_path/platform/manifest.git/