Python code to clone bitbucket repository using subprocess
Asked by kompal.sithta:

Hi All,

I am trying to clone a python repository using the following code. I am trying to automate the process of cloning for certain automation tasks required for code analysis:

import subprocess

def clone_bitbucket_repo(username, password, repository_url,destination_path):  
command = \['git' ,'clone', repository_url, destination_path\]

<br />

# Set up environment variables for authentication  
env_variables = {  
'BITBUCKET_USERNAME': username,  
'BITBUCKET_PASSWORD': password  
}

# Execute the git clone command  
try:  
subprocess.check_output(command, shell=True, env=env_variables)  
print("Repository cloned successfully!")  
except subprocess.CalledProcessError as e:  
print(f"Error cloning repository: {e}")

# Example usage  
bitbucket_username = 'username'  
bitbucket_password = 'password'  
repository_url = '<https://username@bitbucket.org/{workspace-name}/{repo-name}.git>'  
destination_path = 'path/of/folder/'

clone_bitbucket_repo(bitbucket_username, bitbucket_password, repository_url,destination_path)  
the error being generated by running this script is :fatal: unable to access '<https://bitbucket.org/{workspace-name}/{repo-name}.git/>': getaddrinfo() thread failed to start  
Please let me know if can have a solution to reolve this error.  
Thanks,  
Kompal


Answers:
author: Aron Gombas _Midori_
body: **The problem**

The error message
> unable to access '[https://bitbucket.org/{workspace-name}/{repo-name}.git/](https://bitbucket.org/%7Bworkspace-name%7D/%7Brepo-name%7D.git/)': getaddrinfo()

...makes it clear that those two placeholders in the URL weren't replaced.

**The fix**

Change this:
> repository_url = '[https://username@bitbucket.org/{workspace-name}/{repo-name}.git](https://username@bitbucket.org/%7Bworkspace-name%7D/%7Brepo-name%7D.git)'

to the URL where you enter your actual workspacename and repository name:
> repository_url = '[https://username@bitbucket.org/foo/bar.git](https://username@bitbucket.org/%7Bworkspace-name%7D/%7Brepo-name%7D.git)'

To me it seems that the "username" part in the URL should also be replaced. At least I don't think that this is what you really want to use.

comments: author: jake
body: hi Aron.

i m trying to do the similar thing and getting the same error.

so the question is, where to get the exact url i need to pass as repository_url?  
should we go to our repo -\> clone -\> and copy that link?

or just copy the link from browser?

also, why do we need to pass username in the base url? is it because it different when we clone manually vs when we do from python using subprocess?

ex: i see this when i go to bitbucket and click on clone and copy the link in ssh

ssh://git@bitbucketdc-ssh.mycompnay.net:1111/myproject/myrepo.git and can see in http like this  
<https://bitbucketdc.mycompnay.net/scm/myproject/myrepo.git>  
appreciate for any help.


author: jake
body: ok. i got it fixed. not sure what was the issue, but now its working


author: jake
body: i though it might worth to put my solution here.

so when you try to add your url make sure to use f when you pass username and password as object.

like you have this
> '[https://{username}:{password}bitbucket.org/{workspace-name}/{repo-name}.git](https://username@bitbucket.org/%7Bworkspace-name%7D/%7Brepo-name%7D.git)'
>
> so if you have something like above, make sure to add f, otherwise it cant be readed and will throw error of :fatal: unable to access
>
> so like this:
>
> f"[https://{username}:{password}bitbucket.org/{workspace-name}/{repo-name}.git"](https://username@bitbucket.org/%7Bworkspace-name%7D/%7Brepo-name%7D.git)


Tags: bitbucket-cloud

cloud

python