Zapper
Zapper

Reputation: 31

cloning git repo from python

src = user/my.git dest = /home/git_name ver = 1.1

def run
   p = subprocess.run(cmd, stdout=PIPE, stderr=PIPE)

I am calling this run with the following cmds

1.  self.run(['mkdir', '-p', dest])
2.  self.run(['git', 'clone', '--no-checkout',src, dest])
3.  self.run(['cd', dest, ';', 'git', 'checkout', '--detach', ver]])

output: 1st run is a success
2nd run to clone gets the error stderr=b"Cloning into ' /home/git_name'...\n
3rd run is a success.

This directory /home/git_name.OLD.1723430 gets created and I see a .git inside this directory. I also have a file /home/git_name which points to the src, basically has a link to the src directory.

Both of these should happen in the same directory and I don't know why there are two and partial results in both. I am not sure what's wrong

Also, src = user/my.git/repos/tags/1.1 is the actual location of the tags when I try to use the entire path git clone says path is not right

Why does this happen?

Upvotes: 2

Views: 1083

Answers (3)

suvayu
suvayu

Reputation: 4664

I would use sh. Excellent for this kind of stuff.

>>> import sh
>>> sh.git.clone(git_repo_url, "destination")

If you want to do fancy stuff like changing into a repo and run commands, you also have cd available as a context manager:

with sh.cd(git_repo):
    # do git commands

Also read this note about running git commands.

Upvotes: 2

Zapper
Zapper

Reputation: 31

I ended up doing this to make it work

a = subprocess.Popen(['git', 'clone', '--no-checkout', self.src, self.destination],  stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
b =subprocess.Popen(['git', 'checkout', '--detach', self.ver], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd= self.destination).communicate()[0]

Used shell = False(which is the default) & and added cwd to the argument list to make this switch in directory for git to work

Thanks to Everyone who helped!

Upvotes: 1

VonC
VonC

Reputation: 1326784

2nd run to clone gets the error stderr=b"Cloning into ' /home/git_name'...\n

It is not an error, just the fact human-readable output is often redirected to stderr.

Note: /home is for user accounts. You would usually clone a repository inside /home/me, not directly in /home.

Upvotes: 1

Related Questions