2

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?

Zapper
  • 31
  • 5
  • 1
    Note that `subprocess.run` has `shell=False` as a default, which means you cannot put `cd ; ` in and expect it to work. There are two obvious ways to handle this: use the `cwd=` optional argument *to* `subprocess.run` so that you don't need a `cd `, solving this problem entirely in Python; or use `git -C `, solving this problem with an argument to the Git command you run. – torek Oct 21 '22 at 23:38
  • 1
    You could of course add `shell=True`, but [see xkcd](https://xkcd.com/327/). – torek Oct 21 '22 at 23:39

3 Answers3

2

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.

suvayu
  • 4,271
  • 2
  • 29
  • 35
1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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!

Zapper
  • 31
  • 5