sync: make .git init more robust

Hitting Ctrl-C in the middle of this func will leave the .git in a
bad state that requires manual recovery.  The code tries to catch
all exceptions and recover by deleting the incomplete .git dir, but
it omits KeyboardInterrupt which Exception misses.

We could add that to the recovery path, but we can make this more
robust with a different approach: set up everything in .git.tmp/
and only move it to .git/ once we've fully initialized it.

Change-Id: I0f5b97f2e19fc39cffc3e5e23993a2da7220f4e3
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/244733
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2019-11-11 04:34:16 -05:00
parent b466854bed
commit f454512619
2 changed files with 39 additions and 34 deletions

View File

@ -241,14 +241,15 @@ def _makelongpath(path):
return path return path
def rmtree(path): def rmtree(path, ignore_errors=False):
"""shutil.rmtree(path) wrapper with support for long paths on Windows. """shutil.rmtree(path) wrapper with support for long paths on Windows.
Availability: Unix, Windows.""" Availability: Unix, Windows."""
onerror = None
if isWindows(): if isWindows():
shutil.rmtree(_makelongpath(path), onerror=handle_rmtree_error) path = _makelongpath(path)
else: onerror = handle_rmtree_error
shutil.rmtree(path) shutil.rmtree(path, ignore_errors=ignore_errors, onerror=onerror)
def handle_rmtree_error(function, path, excinfo): def handle_rmtree_error(function, path, excinfo):

View File

@ -2706,18 +2706,22 @@ class Project(object):
raise raise
def _InitWorkTree(self, force_sync=False, submodules=False): def _InitWorkTree(self, force_sync=False, submodules=False):
dotgit = os.path.join(self.worktree, '.git') realdotgit = os.path.join(self.worktree, '.git')
init_dotgit = not os.path.exists(dotgit) tmpdotgit = realdotgit + '.tmp'
try: init_dotgit = not os.path.exists(realdotgit)
if init_dotgit: if init_dotgit:
os.makedirs(dotgit) dotgit = tmpdotgit
self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True, platform_utils.rmtree(tmpdotgit, ignore_errors=True)
os.makedirs(tmpdotgit)
self._ReferenceGitDir(self.gitdir, tmpdotgit, share_refs=True,
copy_all=False) copy_all=False)
else:
dotgit = realdotgit
try: try:
self._CheckDirReference(self.gitdir, dotgit, share_refs=True) self._CheckDirReference(self.gitdir, dotgit, share_refs=True)
except GitError as e: except GitError as e:
if force_sync: if force_sync and not init_dotgit:
try: try:
platform_utils.rmtree(dotgit) platform_utils.rmtree(dotgit)
return self._InitWorkTree(force_sync=False, submodules=submodules) return self._InitWorkTree(force_sync=False, submodules=submodules)
@ -2726,21 +2730,21 @@ class Project(object):
raise e raise e
if init_dotgit: if init_dotgit:
_lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId()) _lwrite(os.path.join(tmpdotgit, HEAD), '%s\n' % self.GetRevisionId())
# Now that the .git dir is fully set up, move it to its final home.
platform_utils.rename(tmpdotgit, realdotgit)
# Finish checking out the worktree.
cmd = ['read-tree', '--reset', '-u'] cmd = ['read-tree', '--reset', '-u']
cmd.append('-v') cmd.append('-v')
cmd.append(HEAD) cmd.append(HEAD)
if GitCommand(self, cmd).Wait() != 0: if GitCommand(self, cmd).Wait() != 0:
raise GitError("cannot initialize work tree for " + self.name) raise GitError('Cannot initialize work tree for ' + self.name)
if submodules: if submodules:
self._SyncSubmodules(quiet=True) self._SyncSubmodules(quiet=True)
self._CopyAndLinkFiles() self._CopyAndLinkFiles()
except Exception:
if init_dotgit:
platform_utils.rmtree(dotgit)
raise
def _get_symlink_error_message(self): def _get_symlink_error_message(self):
if platform_utils.isWindows(): if platform_utils.isWindows():