Dev Aggarwal
Dev Aggarwal

Reputation: 8526

GitPython: How to git clone inside directory without providing the full path?

When i do a git clone from the command line, It automatically creates a new directory with the name of that repository and puts the code inside that directory.

But when i do

 from git import Repo
 Repo.clone_from('https://github.com/jessfraz/dockerfiles.git', '/home/dev')

i get this

GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git clone -v https://github.com/jessfraz/dockerfiles.git /home/dev
  stderr: 'fatal: destination path '/home/dev' already exists and is not an empty directory.

How can i get the repository name and build the path /home/dev/<repo_name> ?

Upvotes: 2

Views: 1475

Answers (1)

Dominique Barton
Dominique Barton

Reputation: 918

I just had a look at the code and I think you need to provide the full path to your working copy (i.e. /home/dev/dockerfiles).

If you don't want that, you might need to create your own Repo class inherited from the origin Repo class. You might then want to overwrite the clone_from() class method. Something like this (not tested, just a quick draft):

from git import Repo


class YourRepo(Repo):

    @classmethod
    def clone_from(cls, url, to_path=None, **kwargs):
        if to_path is None:
            to_path = url.split('/')[-1]
            if to_path.endswith('.git'):
                to_path = to_path[0:-4]
        return super().clone_from(url=url, to_path=to_path, **kwargs)

Upvotes: 3

Related Questions