From db111d392407797e170355e1c0ec98e71d4a8278 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Wed, 15 Jan 2025 23:43:22 +0000 Subject: [PATCH] sync: Recover from errors during read-tree When repo is initializing a git repository, it calls `git read-tree`. During such operation, git is restoring workspace based on the current index. However, some things can go wrong: a user can run out of disk space, or, in case of partial clone, user may no longer reach the remote host. That will leave affected repository in a bad state with partially checked out workspace. The follow up repo sync won't try to fix such state. This change removes .git symlink, which will force the next `repo sync` to redo Git repository setup. Bug: b/363171216 Bug: b/390161127 Change-Id: I57db4b6cae0ef21826dc7cede4d3bf02cfc3d955 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/447801 Reviewed-by: Scott Lee Tested-by: Josip Sokcevic Commit-Queue: Josip Sokcevic --- project.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/project.py b/project.py index 8fec9816..de1ab3c6 100644 --- a/project.py +++ b/project.py @@ -3443,11 +3443,18 @@ class Project: # Finish checking out the worktree. cmd = ["read-tree", "--reset", "-u", "-v", HEAD] - if GitCommand(self, cmd).Wait() != 0: - raise GitError( - "Cannot initialize work tree for " + self.name, - project=self.name, - ) + try: + if GitCommand(self, cmd).Wait() != 0: + raise GitError( + "Cannot initialize work tree for " + self.name, + project=self.name, + ) + except Exception as e: + # Something went wrong with read-tree (perhaps fetching + # missing blobs), so remove .git to avoid half initialized + # workspace from which repo can't recover on its own. + platform_utils.remove(dotgit) + raise e if submodules: self._SyncSubmodules(quiet=True)