繁体   English   中英

Python 克隆 git 存储库的方法

[英]Python way to clone a git repository

是否有不使用子进程克隆 git 存储库的 Python 方式? 我愿意使用您推荐的任何类型的模块。

使用GitPython会给你一个很好的 Git 的 Python 接口。

例如,在安装它( pip install gitpython )之后,要克隆一个新的存储库,您可以使用clone_from函数:

from git import Repo

Repo.clone_from(git_url, repo_dir)

有关使用 Repo 对象的示例,请参阅GitPython 教程

注意: GitPython 需要在系统上安装 git,并且可以通过系统的 PATH 访问。

GitPython 之前和内部都没有听说过它,它依赖于在某个地方拥有 git 可执行文件; 此外,它们可能有很多错误。 但这可能值得一试。

如何克隆

import git
git.Git("/your/directory/to/clone").clone("git://gitorious.org/git-python/mainline.git")

(这不好,我不知道这是否是受支持的方式,但它有效。)

我的解决方案非常简单直接。 它甚至不需要手动输入密码/密码。

这是我的完整代码:

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

Github 的libgit2绑定, pygit2提供了单线克隆远程目录:

clone_repository(url, path, 
    bare=False, repository=None, remote=None, checkout_branch=None, callbacks=None)

对于蟒蛇 3

首先安装模块:

pip3 install gitpython

后来,编码它:)

from git.repo.base import Repo
Repo.clone_from("https://github.com/*****", "folderToSave")

我希望这可以帮助你

这是一种在使用GitPython克隆存储库时打印进度的方法

import time
import git
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())

你可以使用dload

import dload
dload.git_clone("https://github.com/some_repo.git")

pip install dload

使用德威小费,您应该能够:

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'})

这是使用 gitpython 模块的 gitpull 和 gitpush 的示例代码。

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('')

在 Windows 上克隆 repo 的最简单方法是:

  1. 点安装克隆
  2. 克隆 [REPO] [用户名]

示例:克隆 Wifi-Brute Cyber​​-Dioxide

您可以通过shell命令执行它

import os os.system("pip install clone") os.system("clone SSH-Brute Cyber​​-Dioxide")

我们可以使用没有任何库的简单解决方案。

#!/usr/bin/python
import os

destination_path  = "destination/path/where/project/to/be/cloned"
clone_command = "git clone https://your.git.servername/git-folder/repo-name.git" 

clone_with_path = clone_command  +" "+ destination_path
os.system(clone_with_path)

Perk:如果它不存在,它将创建一个目标文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM