clone

The following script can download all git repositories from all of the projects that you have access to on a privately hosted Bitbucket server.

The execution work-flow of this script is as follows:

It will ask for your username (the one you use to login on the Bitbucket server)

Then it will ask for your password, the password will not be visible on screen as you type because we disabled the echo functionality for that step.

Later, you will be prompted to provide the URL of the server, for this step be sure to define the protocol if it is http or https and the correct port number as well (e.g. https://bitbucket.bytefreaks.net:7990)

Finally, you will be requested to give the location to where the repositories should be cloned to.

Then the script will connect to the server, get the list of projects that you have access to and for each project retrieve the repositories of the project and clone them in the designated folder.

The above code will bring the JSON object, filter out everything except for the “ssh_url_to_repo” member of each project and then it will use it to clone the project by fixing up the URL to be used by git.

To get the above code working: the GitLab API requires that you use a token that is related to your account instead of using your credentials to make the call to the API.

From the results, we remove the first 3 lines as they contain no useful information to cloning all the repositories. From the rest of the lines, where each line contains the information for a repository we have access to, we keep the third column only as it is the one that holds the repository name as it is stored on the server.

On the last stage of the pipe we have a list of the names of the repositories, using xargs, we assign each repository name to the special variable {} and processing one result at a time we clone the git repository to the current directory under the folder that is named as the repository.

–connect is used to connect to the hypervisor of the virtual machine, here it is system

-o is the original virtual machine name (which is registered with the previous hypervisor)

-n is the name of the new virtual machine, the clone (this name will be used to register the clone to the hypervisor)

-f the location that the clone will use to store it’s virtual hard drives, can be used as many times as needed (in the above example coeus has two hard disks and that is why we need to provide two -f paramaters)

There is more directives that can be provided to the clone manager from which a very important one is the: –mac where you get to define the mac address of the new virtual machine (e.g –mac C0:FF:EE:11:00:11)