-2

The git bash closes abruptly without cloning the repo. I'm unable to understand what's wrong here.

import os
import subprocess
    
parent_dir = r'C:\Users\user\Documents'
dir_name = 'Git_temp'
dir_path = os.path.join(parent_dir, dir_name)
    
os.mkdir(dir_path)
print("{0} created under {1}".format(dir_name, parent_dir))
os.chdir(dir_path)
print("Current Working Directory : {0}".format(os.getcwd()))
git_file = "C:\Program Files\Git\git-bash.exe"
git_clone_cmd = "git clone https://github.com/patankar-saransh/test_repo.git"
subprocess.Popen([git_file, git_clone_cmd])
omajid
  • 14,165
  • 4
  • 47
  • 64
  • 1
    Can you share more information? What goes wrong? What's the error messages? Any logs you can share? What have you tried to solve the problem? – omajid Jul 08 '20 at 19:32
  • It prints till current working directory and then open up the git bash but after that the git bash closes abruptly without giving out any errors. I'm trying to understand why git bash is closing. – Saransh Patankar Jul 09 '20 at 03:55
  • Are you doing anything to keep it open? Your code runs a process (`git-bash.exe`). When the process finishes, everything about it goes away (including all windows). – omajid Jul 09 '20 at 04:01
  • As I'm new to this, I didn't know that we have to keep it open. I thought passing the command "git clone" will keep it running. – Saransh Patankar Jul 09 '20 at 06:27

1 Answers1

1

If you do not want to use GitPython, then simply make sure git.exe is in your %PATH%.

Then your call would be:

import subprocess
process = subprocess.Popen(["git", "clone", "https://..."], stdout=subprocess.PIPE)
output = process.communicate()[0]

As seen here, with Python 2.7+, use check_output

import subprocess
output = subprocess.check_output(["git",  "clone", "https://..."]])
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250