Skip to content

Commit

Permalink
Submodule now uses a specialized method to remove its trees to allow …
Browse files Browse the repository at this point in the history
…read-only files to be removed on windows as well
  • Loading branch information
Byron committed Jun 13, 2011
1 parent 59587d8 commit 9b6f38d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 4 additions & 5 deletions git/objects/submodule/base.py
Expand Up @@ -13,7 +13,8 @@
Iterable,
join_path_native,
to_native_path_linux,
RemoteProgress
RemoteProgress,
rmtree
)

from git.config import SectionConstraint
Expand All @@ -29,8 +30,6 @@
import sys
import time

import shutil

__all__ = ["Submodule", "UpdateProgress"]


Expand Down Expand Up @@ -622,7 +621,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
if os.path.islink(mp):
method = os.remove
elif os.path.isdir(mp):
method = shutil.rmtree
method = rmtree
elif os.path.exists(mp):
raise AssertionError("Cannot forcibly delete repository as it was neither a link, nor a directory")
#END handle brutal deletion
Expand Down Expand Up @@ -671,7 +670,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
if not dry_run:
wtd = mod.working_tree_dir
del(mod) # release file-handles (windows)
shutil.rmtree(wtd)
rmtree(wtd)
# END delete tree if possible
# END handle force
# END handle module deletion
Expand Down
20 changes: 19 additions & 1 deletion git/util.py
Expand Up @@ -8,6 +8,8 @@
import re
import sys
import time
import stat
import shutil
import tempfile
import platform

Expand All @@ -23,10 +25,26 @@
__all__ = ( "stream_copy", "join_path", "to_native_path_windows", "to_native_path_linux",
"join_path_native", "Stats", "IndexFileSHA1Writer", "Iterable", "IterableList",
"BlockingLockFile", "LockFile", 'Actor', 'get_user_id', 'assure_directory_exists',
'RemoteProgress')
'RemoteProgress', 'rmtree')

#{ Utility Methods

def rmtree(path):
"""Remove the given recursively.
:note: we use shutil rmtree but adjust its behaviour to see whether files that
couldn't be deleted are read-only. Windows will not remove them in that case"""
def onerror(func, path, exc_info):
if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
# END end onerror
return shutil.rmtree(path, False, onerror)



def stream_copy(source, destination, chunk_size=512*1024):
"""Copy all data from the source stream into the destination stream in chunks
of size chunk_size
Expand Down

0 comments on commit 9b6f38d

Please sign in to comment.