I am trying to clone a git repository using the repository URL, username and token which I receive from frontend. A folder is created with the name of the repository. After taking essential information from the newly created folder, I am trying to delete the folder using shutil.rmtree
. But I get this error:
PermissionError: [WinError 5] Access is denied: 'team-sortifie.git\.git\objects\pack\pack-71df0c364b51d2df28502deb6e1c1c7e418bd961.idx'
Here's my code:
@classmethod
def fetch_all_branches_with_creation_date(cls, repo_url):
branch_info_list = []
repo_name = ''
repository_users = []
try:
# Clone the repository to a temporary directory
repo_name = os.path.basename(repo_url.rstrip('/'))
repo = Repo.clone_from(repo_url, repo_name)
# Fetch repository users
repository_users = cls.fetch_repository_users_remotely(repo)
# Get all branches of the repository
branches = repo.remote().refs
# Process each branch
for branch in branches:
branch_info = {
'branch_name': branch.name,
'branch_creation_date': branch.commit.authored_datetime.strftime('%Y-%m-%d'),
'latest_commit_subject': branch.commit.message,
'latest_commit_date': branch.commit.committed_datetime.strftime('%Y-%m-%d'),
'author_name': branch.commit.author.name,
'tags': [],
# Check if the branch is locked
'is_locked': cls.is_branch_locked(branch),
# Check if the branch is deleted
'is_deleted': cls.is_branch_deleted(branch)
}
# Get tags associated with the branch
tags = repo.tags
for tag in tags:
if tag.commit.hexsha.startswith(branch.commit.hexsha):
tag_info = {
'tag_name': tag.name,
'tag_creation_date': tag.commit.committed_datetime.strftime('%Y-%m-%d')
}
branch_info['tags'].append(tag_info)
# Append branch information to the list
branch_info_list.append(branch_info)
except GitCommandError as e:
# If there's an error, it could be due to invalid credentials or repository URL
error_message = Repositoryanalytics.extract_error_message(str(e))
current_app.logger.debug(error_message)
finally:
# Remove the repository directory if it exists
if repo_name:
shutil.rmtree(repo_name)
return branch_info_list, repository_users
I have already tried these methods:
- Tried running the server as administrator.
- Tried
rmdir
instead ofshutil
, like this:
finally:
# Remove the repository directory if it exists
if repo_name:
if configs.platform['isWIN'] == 'TRUE':
subprocess.run(
["rmdir", "/Q", "/S", repo_name], shell=True)
else:
shutil.rmtree(repo_name)
- Right clicked the main backend folder -> Properties -> Security -> Add -> Advanced -> Find Now -> In the search results selected IUSR , then under security tab under the permissions, checked on all the boxes under Allow.
None of these are working. Why exactly is this issue occurring and what can I try now to solve this?
It seems git creates .idx files with read-only permission and that
rmtree()
has problems removing this kind of files (as mentioned here).Thus, a solution is to remove permissions on these files (using
os.chmod()
), then dormtree()
.