How to remove git repository, in python, on windows
Asked Answered
H

4

8

As the title describes, I need to remove a git repository by using python. I've seen other questions about this very same topic, but none of the solutions seem to work for me.

My work: I need to download a repository, using gitpython, followed by checking some irrelevant things. After the process is finished, I need to delete the repository to save space for whoever is using my script.

The problem: When cloning a git repository, a .git file will be created. This file is hidden in windows, and the modules I've been using do not have permission to delete any of the files within the .git folder.

What I've tried:

import shutil
shutil.rmtree('./cloned_repo')

PermissionError: [WinError 5] Access is denied:

Any help regarding this issue would be deeply appreciated.

Hexagonal answered 15/11, 2019 at 13:30 Comment(5)
Did you try to use os.system (' rm -r $gitfolder' ) ?Goalkeeper
I did now. This command requires user input (it is asking me if I really want to remove write-protected files). It also fails to remove the files while giving me a weird stack trace.Hexagonal
Please try adding -y to the command to see if user input is avoided this way. os.system ( ' rm -r $gitfolder -y' )Goalkeeper
Also please try using os.remove('path to folder')Goalkeeper
Does this answer your question? Deleting directory in PythonDanube
H
6

Git has some readonly files. You need to change the permissions first:

import subprocess
import shutil
import os
import stat
from os import path
for root, dirs, files in os.walk("./cloned_repo"):  
    for dir in dirs:
        os.chmod(path.join(root, dir), stat.S_IRWXU)
    for file in files:
        os.chmod(path.join(root, file), stat.S_IRWXU)
shutil.rmtree('./cloned_repo')
Helladic answered 15/11, 2019 at 13:40 Comment(2)
He is in windows, it is in the titleWorsley
s_irwxu means inode read write execute ??Monkery
L
3

As noted above, this is because many of Git's files are read-only, which Python does not delete gracefully on Windows. The GitPython module has a utility function for just this purpose: git.util.rmtree

Calling the function like so should resolve your problem:

from git import rmtree
rmtree('./cloned_repo')

You can also see their source here--it's similar to the answer above, as of Dec 2020.

Latona answered 15/12, 2020 at 18:18 Comment(1)
That is actually the best and cleanest solution imho. Does not require additional imports when using git in python and it does not require any loopsElva
P
0

I've tried all the suggestions above, but none of them worked.

I'm using the GitPython module to handle my git repo's and I'm not convinced that I'm handling any open connections correctly. Saying that I have found a way to kill all git sessions which "fixed" my issue. (Albeit it a dirty fix)

import os
folder = "path/to/root"
git_repo = git.Repo(folder)
# do something with gitrepo

os.system(f'taskkill /IM "git.exe" /F')

This is not a good solution as you might have other git session open that you don't want to close. One way will be to make sure you close the git session, by closing it.

git_repo = git.Repo(folder)
#do something with gitrepo
git_repo.close()

Although the better way will be to use a context manager like 'with'

import git
with git.Repo(folder) as git_repo:
   # do something with gitrepo

The context manager makes it easier to manage the git repo, and less likely have open repo's in session, thus less likely to have read only access errors.

Pierro answered 18/7, 2022 at 19:34 Comment(0)
F
0

If you use tempfile.TemporaryDirectory in Python 3.8+, this will just work. The specific issue with Git and Windows was considered a bug in Python and addressed in 3.8.

For <=3.7, you'll need to refer to one of the other answers, or to some workarounds linked in the Python issue.

Franchot answered 20/2, 2023 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.