0

I have tried the below python code to clone the repository from Git but it is not cloning and it is returning the value as 1.

import subprocess
repo = input ("Repository:") 
_repo_ = f '{repo}'
print ("Choose local repository ") 
local = input ("Repository:") 
local_path = f '{local}'
cmd = " git clone (git repository link)"+_repo_+".git"
print(str(cmd)) 
return_value =subprocess.call(cmd, shell=True) 
print (Return value:" +str(return_value)) 
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
Chakkara
  • 25
  • 4
  • Does this answer your question? [Running Git clone command in Python](https://stackoverflow.com/questions/15079442/running-git-clone-command-in-python) – JKC May 18 '21 at 10:45
  • I removed a typo in your code. The line `cmd = " git clone (git repository link)"+_repo_+".git"` was missing a `+` for concatenation. Also, have you checked the outpu of the subprocess command? Use `subprocess.check_call` to get the output as pointed out in this answer: https://stackoverflow.com/a/58332167/15016163 – Maicon Mauricio May 18 '21 at 10:53

2 Answers2

2

Just found this library gitpython

from git import Repo

Try

Repo.clone_from(url,repo)

OR

git.Git(PATH).clone(URL)
Yafaa
  • 307
  • 1
  • 14
  • What's the error ? pip install GitPython worked fine just tested – Yafaa May 18 '21 at 10:53
  • Thanks. I have also tried with from git import Repo.. Error thrown with no module named git. So I tried to install using pip install gitpython. Unable to do it because of error : Retrying failed to establish connection – Chakkara May 18 '21 at 11:03
  • Have you check to see if you have `git` installed? – astrochun May 19 '21 at 03:29
0

you could simply do this

import subprocess

repo = input("Repository:")
cmd = "git clone {}.git".format(repo)


print(str(cmd))
return_value = subprocess.call(cmd, shell=True)
print("Return value:" + str(return_value))