Pack corrupted in Bitbucket Cloud repo

19 Views Asked by At

I'm trying to make a full backup of every existing repository in my Bitbucket Cloud account. At a point, it throws an error that says "fatal: packed object X (stored in .git/objects/pack/pack-X.pack) is corrupt". Here's the script's code:

import os
import requests
import datetime

username = "MyUser"
password = "MyPassword1234"

backup_path = ".\\repo-backups"

url = "https://api.bitbucket.org/2.0/repositories/{}?pagelen=100".format(username)
response = requests.get(url, auth=(username, password))
repositories = response.json()["values"]

for repository in repositories:
    repository_name = repository["name"]
    repository_url = repository["links"]["clone"][0]["href"]

    repository_branches_url = "https://api.bitbucket.org/2.0/repositories/MyUser/{}/refs/branches?pagelen=100".format(repository_name)
    repository_branches_response = requests.get(repository_branches_url, auth=(username, password))
    repository_branches = repository_branches_response.json()["values"]

    atIndex = repository_url.find("@")
    repository_url = repository_url[:atIndex] + ':' + password + repository_url[atIndex:]
    backup_folder = os.path.join(backup_path, repository_name)
    if os.path.exists(backup_folder):
        print('\n** TRYING TO UPDATE {} **'.format(repository_name))
        os.chdir(backup_folder)
        os.system("git pull --all")

        for branch in repository_branches:
            branch_name = branch['name']
            branch_verification_response = os.popen("git rev-parse --verify {}".format(branch_name)).read()
            branch_does_not_exist_in_local = branch_verification_response == ""

            if branch_does_not_exist_in_local:
                print(branch_name + " does not exist in local. Preparing to clone...")
                os.popen("git checkout -b {} origin/{}".format(branch_name, branch_name)).read()
                print("- NEW BRANCH: {}".format(branch_name))
            else:
                os.popen("git checkout {}".format(branch_name)).read()
                os.popen("git pull --rebase").read()

    else:
        print('\n** NEW REPO {}: TRYING TO CLONE **'.format(repository_name))
        os.system("git clone {} {}".format(repository_url, backup_folder))
        os.chdir(backup_folder)

        for branch in repository_branches:
            if branch['name'] != 'master':
                os.popen("git checkout -b {} origin/{}".format(branch['name'], branch['name'])).read()

        os.popen("git checkout master").read()

    os.chdir("../..")

print("\n--- BACKUP PROCESS FINISHED ---\n")

Is there something wrong? Thanks in advance.

I've already tried to remove the repo and remove the pack with the corrupt object

0

There are 0 best solutions below