project: Rename if deletion fails

If a project contains files not owned by the current user, remove will
fail. In order to ensure repo sync continues to work, rename the
affected project instead, and let user know about it.

Bug: 321273512
Change-Id: I0779d61fc67042308a0226adea7d98167252a5d3
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/404372
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
This commit is contained in:
Josip Sokcevic 2024-01-24 13:54:25 -08:00 committed by LUCI
parent 208f344950
commit 4217a82bec

View File

@ -1848,7 +1848,7 @@ class Project:
platform_utils.remove(path) platform_utils.remove(path)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
logger.error("error: %s: Failed to remove: %s", path, e) logger.warning("%s: Failed to remove: %s", path, e)
failed = True failed = True
errors.append(e) errors.append(e)
dirs[:] = [ dirs[:] = [
@ -1867,7 +1867,7 @@ class Project:
platform_utils.remove(d) platform_utils.remove(d)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
logger.error("error: %s: Failed to remove: %s", d, e) logger.warning("%s: Failed to remove: %s", d, e)
failed = True failed = True
errors.append(e) errors.append(e)
elif not platform_utils.listdir(d): elif not platform_utils.listdir(d):
@ -1875,16 +1875,28 @@ class Project:
platform_utils.rmdir(d) platform_utils.rmdir(d)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
logger.error("error: %s: Failed to remove: %s", d, e) logger.warning("%s: Failed to remove: %s", d, e)
failed = True failed = True
errors.append(e) errors.append(e)
if failed: if failed:
logger.error( rename_path = (
"error: %s: Failed to delete obsolete checkout.", f"{self.worktree}_repo_to_be_deleted_{int(time.time())}"
self.RelPath(local=False),
) )
try:
platform_utils.rename(self.worktree, rename_path)
logger.warning(
"warning: renamed %s to %s. You can delete it, but you "
"might need elevated permissions (e.g. root)",
self.worktree,
rename_path,
)
# Rename successful! Clear the errors.
errors = []
except OSError:
logger.error( logger.error(
"%s: Failed to delete obsolete checkout.\n",
" Remove manually, then run `repo sync -l`.", " Remove manually, then run `repo sync -l`.",
self.RelPath(local=False),
) )
raise DeleteWorktreeError(aggregate_errors=errors) raise DeleteWorktreeError(aggregate_errors=errors)