有没有一种不使用子进程的Python方式来克隆git存储库?我可以使用您推荐的任何模块。
有没有一种不使用子进程的Python方式来克隆git存储库?我可以使用您推荐的任何模块。
from git import Repo # pip install gitpython
Repo.clone_from(git_url, repo_dir)
git_url
和repo_dir
的示例? - Sam我的解决方案非常简单和直接。它甚至不需要手动输入密码。
这是我的完整代码:
import sys
import os
path = "/path/to/store/your/cloned/project"
clone = "git clone gitolite@<server_ip>:/your/project/name.git"
os.system("sshpass -p your_password ssh user_name@your_localhost")
os.chdir(path) # Specifying the path where the cloned project needs to be copied
os.system(clone) # Cloning
git clone <repo_url> <target_path>
来避免这种情况。不需要使用chdir
。 - Lahiru Chandima针对 Python 3:
首先安装模块:
pip3 install gitpython
然后,编写代码 :)
from git.repo.base import Repo
Repo.clone_from("https://github.com/*****", "folderToSave")
我希望这能对你有所帮助
import time
import git # pip install gitpython
from git import RemoteProgress
class CloneProgress(RemoteProgress):
def update(self, op_code, cur_count, max_count=None, message=''):
if message:
print(message)
print('Cloning into %s' % git_root)
git.Repo.clone_from('https://github.com/your-repo', '/your/repo/dir',
branch='master', progress=CloneProgress())
from dulwich.repo import Repo
Repo("/path/to/source").clone("/path/to/target")
这还很基础——它会复制对象和引用,但如果您创建一个非裸库,则不会创建工作树的内容。
一个相当简单的方法就是直接在 URL 中传递凭据,但这可能会引起怀疑 - 使用时请谨慎。
import os
def getRepo(repo_url, login_object):
'''
Clones the passed repo to my staging dir
'''
path_append = r"stage\repo" # Can set this as an arg
os.chdir(path_append)
repo_moddedURL = 'https://' + login_object['username'] + ':' + login_object['password'] + '@github.com/UserName/RepoName.git'
os.system('git clone '+ repo_moddedURL)
print('Cloned!')
if __name__ == '__main__':
getRepo('https://github.com/UserName/RepoYouWant.git', {'username': 'userName', 'password': 'passWord'})
import os.path
from git import *
import git, os, shutil
# create local Repo/Folder
UPLOAD_FOLDER = "LocalPath/Folder"
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
print(UPLOAD_FOLDER)
new_path = os.path.join(UPLOADFOLDER)
DIR_NAME = new_path
REMOTE_URL = "GitURL" # if you already connected with server you dont need to give
any credential
# REMOTE_URL looks "git@github.com:path of Repo"
# code for clone
class git_operation_clone():
try:
def __init__(self):
self.DIR_NAME = DIR_NAME
self.REMOTE_URL = REMOTE_URL
def git_clone(self):
if os.path.isdir(DIR_NAME):
shutil.rmtree(DIR_NAME)
os.mkdir(DIR_NAME)
repo = git.Repo.init(DIR_NAME)
origin = repo.create_remote('origin', REMOTE_URL)
origin.fetch()
origin.pull(origin.refs[0].remote_head)
except Exception as e:
print(str(e))
# code for push
class git_operation_push():
def git_push_file(self):
try:
repo = Repo(DIR_NAME)
commit_message = 'work in progress'
# repo.index.add(u=True)
repo.git.add('--all')
repo.index.commit(commit_message)
origin = repo.remote('origin')
origin.push('master')
repo.git.add(update=True)
print("repo push succesfully")
except Exception as e:
print(str(e))
if __name__ == '__main__':
a = git_operation_push()
git_operation_push.git_push_file('')
git_operation_clone()
git_operation_clone.git_clone('')