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?

2
  • 1
    Note that subprocess.run has shell=False as a default, which means you cannot put cd <path>; <cmd> 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 <path>, solving this problem entirely in Python; or use git -C <path>, solving this problem with an argument to the Git command you run. Commented Oct 21, 2022 at 23:38
  • 1
    You could of course add shell=True, but see xkcd. Commented Oct 21, 2022 at 23:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.