0

I am cloning from local network using the code below:

_g = cmd.Git(clone_path)
path = os.path.normcase(os.path.normpath(path))
path = path.replace('\\', '/')
_g.clone("-o" + host,
    "http://" + host + ':8002' + '/' + path + '/' + '.git',
    os.path.join(clone_path, repo))

Now I need to wait for the clone operation to copmplete (succesfully) to proceed. How am I to proceed ?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

1 Answers1

1

In git-python, all direct git calls like repo.git.clone() are synchronous. There is the option to run it asynchronously using the as_process flag. Thus, calling repo.git.clone(..., as_process=True) returns a subprocess.Popen instance which must be handled accordingly. In the most simple case, you would call communicate() on it to receive its entire output.

Doing so is usually never needed nor recommended, as git-python provides higher level methods to get the common operation done more conveniently.

If you desire to simplify your code, you could use a higher-level operation, such as this one:

import git
r = git.Repo.clone_from(your_url, checkout_path, origin=host)

r is a Repo instance, which is your handle to all git-related operations. The documentation for it is on readthedocs.

Byron
  • 3,908
  • 3
  • 26
  • 35
  • Is `git.Repo.clone_from()` synchronous ? – Mr_and_Mrs_D Feb 20 '15 at 16:44
  • Yes, everything in gitpython is. – Byron Feb 21 '15 at 15:27
  • Can you link me to the docs stating this ? What I used definitely wasn't synchronous - synchronous means _blocking till the method returns_ in this context - maybe you confuse this with threads – Mr_and_Mrs_D Feb 21 '15 at 16:19
  • I don't think I am confusing this - all I can say it that it is supposed to be synchronous, and [the code](https://github.com/gitpython-developers/GitPython/blob/master/git/repo/base.py#L826) seems to support this statement as well. 'clone_from' returns a Repo instance, which is ready to go, not a future or promise of some sort. – Byron Feb 21 '15 at 18:02
  • Aha - so you mean that the lower level operation is not synchronous while the higher level is - since you are a gitPython dev I accept your answer and will get back to you when I get round to testing. Btw - I think this is a wrong design decision :( To be much more intuitive to git (as opposed to svn and whatnot) users the `git` object should behave as the command line - so `cmd.Git().clone()` should be synchronous. – Mr_and_Mrs_D Feb 21 '15 at 21:27
  • I have updated my answer to tell a little more about the `Git` command. – Byron Feb 23 '15 at 07:25
  • Thanks for the input - appreciated ;). So `cmd.Git(clone_path).clone()` was indeed asynchronous right (09/2014) ? As for the API - it should be: `g = Git('/path/to/repo')` then `g.clone(), g.commit()` etc - this repo idea is quite ungit, just complicates things and makes learning curve steeper – Mr_and_Mrs_D Feb 23 '15 at 15:40
  • Interesting - maybe you want to write a shell script then. The intended use of `Git` is in the 5% of the use-cases which have no 'pythonic' implementation yet. As `Git` is stable for a long time now, I'd recommend just using its code in your project, and kill one dependency. – Byron Feb 23 '15 at 15:51
  • Thanks - so finally was `cmd.Git(clone_path).clone()` indeed asynchronous - right ? Please edit your answer to make this clear – Mr_and_Mrs_D Feb 23 '15 at 16:22
  • No, it's synchronous. which version of GitPython are you using ? – Byron Feb 23 '15 at 16:36
  • The one that was out on september 2014 – Mr_and_Mrs_D Feb 23 '15 at 18:51