Python way to clone a git repository
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
Using GitPython will give you a good python interface to Git.
For example, after installing it (pip install gitpython
), for cloning a new repository you can use clone_from function:
from git import Repo
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.
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)
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
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.)