Mike
Mike

Reputation: 7831

Python way to clone a git repository

Is there a Python way without using a subprocess to clone a git repository? I'm up for using any sort of modules you recommend.

Upvotes: 152

Views: 372614

Answers (12)

crizCraig
crizCraig

Reputation: 8897

Here's a way to print progress while cloning a repo with GitPython

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

Upvotes: 14

Mike Brian Olivera
Mike Brian Olivera

Reputation: 1601

Here it is

import git  # pip install gitpython
git.Repo.clone_from("https://github.com/*****", "folderToSave")

I hope this helps you

Upvotes: 21

Debilski
Debilski

Reputation: 67828

There is GitPython. Haven’t heard of it before and internally, it relies on having the git executables somewhere; additionally, they might have plenty of bugs. But it could be worth a try.

How to clone:

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

(It’s not nice and I don’t know if it is the supported way to do it, but it worked.)

Upvotes: 90

Amir Ali Akbari
Amir Ali Akbari

Reputation: 6396

Using GitPython will give you a good python interface to Git.

For cloning a new repository you can use clone_from function:

from git import Repo  # pip install gitpython

Repo.clone_from(git_url, repo_dir)

See the GitPython Tutorial for examples on using the Repo object.

Note: GitPython requires git being installed on the system, and accessible via system's PATH.

Upvotes: 228

Rumit Patel
Rumit Patel

Reputation: 12459

We can use simple solution without any library.

#!/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: It will create a destination folder if it doesn't exist.

Upvotes: -1

Never Settle
Never Settle

Reputation: 1

The easiest way to clone a repo on windows is:

  1. pip install clone
  2. clone [REPO] [USERNAME]

Example: clone Wifi-Brute Cyber-Dioxide

You can execute it via shell command

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

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98871

You can use dload

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

pip install dload

Upvotes: 5

Manje
Manje

Reputation: 421

My solution is very simple and straight forward. It doesn't even need the manual entry of passphrase/password.

Here is my complete code:

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

Upvotes: 31

haku-kiro
haku-kiro

Reputation: 66

Pretty simple method is to just pass the creds in the url, can be slightly suspect though - use with caution.

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

Upvotes: 4

v.k
v.k

Reputation: 151

This is the sample code for gitpull and gitpush using gitpython module.

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 "[email protected]: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('')

Upvotes: 1

chiffa
chiffa

Reputation: 2088

Github's libgit2 binding, pygit2 provides a one-liner cloning a remote directory:

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

Upvotes: 13

jelmer
jelmer

Reputation: 2450

With Dulwich tip you should be able to do:

from dulwich.repo import Repo
Repo("/path/to/source").clone("/path/to/target")

This is still very basic - it copies across the objects and the refs, but it doesn't yet create the contents of the working tree if you create a non-bare repository.

Upvotes: 4

Related Questions