InitGitDir: Clean up created directories

If _InitGitDir fails, it leaves any progress it had made on the file
system. This can cause subsequent calls to repo sync to behave
differently. This is especially evident when _CheckDirReference() fails,
since it will not be invoked when sync is retried because both the
source and destination directories already exist.

To address this, have _InitGitDir() clean up any directories it has created
if it catches an exception. Also behave the same way for _InitWorkTree().

Change-Id: Ic16bb3feea649e115b59bd44be294e89e3692aeb
This commit is contained in:
Kevin Degi 2015-07-27 13:33:43 -06:00
parent b3d6e67196
commit b1a07b8276

View File

@ -2169,6 +2169,7 @@ class Project(object):
def _InitGitDir(self, mirror_git=None):
init_git_dir = not os.path.exists(self.gitdir)
init_obj_dir = not os.path.exists(self.objdir)
try:
# Initialize the bare repository, which contains all of the objects.
if init_obj_dir:
os.makedirs(self.objdir)
@ -2217,6 +2218,12 @@ class Project(object):
self.config.SetString('core.bare', 'true')
else:
self.config.SetString('core.bare', None)
except Exception:
if init_obj_dir and os.path.exists(self.objdir):
shutil.rmtree(self.objdir)
if init_git_dir and os.path.exists(self.gitdir):
shutil.rmtree(self.gitdir)
raise
def _UpdateHooks(self):
if os.path.exists(self.gitdir):
@ -2363,6 +2370,7 @@ class Project(object):
def _InitWorkTree(self):
dotgit = os.path.join(self.worktree, '.git')
init_dotgit = not os.path.exists(dotgit)
try:
if init_dotgit:
os.makedirs(dotgit)
self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True,
@ -2380,6 +2388,10 @@ class Project(object):
raise GitError("cannot initialize work tree")
self._CopyAndLinkFiles()
except Exception:
if init_dotgit:
shutil.rmtree(dotgit)
raise
def _gitdir_path(self, path):
return os.path.realpath(os.path.join(self.gitdir, path))