From 58e268c2e30567f415fa4e02086e49299c31406a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 2 Jul 2024 14:52:07 +0200 Subject: [PATCH] git: Remove untracked files from cached checkouts. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cached checkouts could end up with stale untracked files, for example because the checkout was interrupted. As a result, when this happens for the Guix checkout, users would not get substitutes for ‘guix pull’. * guix/git.scm (delete-untracked-files): New procedure. (switch-to-ref): Use it. * tests/git.scm ("update-cached-checkout, untracked files removed"): New test. Co-authored-by: Ricardo Wurmus Change-Id: Iccbe644ade396ad27a037db7e0ef1c2a68ef91ce --- guix/git.scm | 24 ++++++++++++++++++++++++ tests/git.scm | 22 +++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/guix/git.scm b/guix/git.scm index d75a301f98..48a962089d 100644 --- a/guix/git.scm +++ b/guix/git.scm @@ -298,6 +298,25 @@ corresponding Git object." (('tag . tag) (tag->commit repository tag))))) +(define (delete-untracked-files repository) + "Delete untracked files from the work directory of REPOSITORY." + (let ((workdir (repository-working-directory repository)) + (status (status-list-new repository + (make-status-options + STATUS-SHOW-WORKDIR-ONLY + (logior + STATUS-FLAG-INCLUDE-UNTRACKED + STATUS-FLAG-INCLUDE-IGNORED))))) + (for-each (lambda (entry) + (let ((status (status-entry-status entry))) + (when (or (memq 'wt-new status) + (memq 'ignored status)) + (let* ((diff (status-entry-index-to-workdir entry)) + (new (diff-delta-new-file diff))) + (delete-file-recursively + (in-vicinity workdir (diff-file-path new))))))) + (status-list->status-entries status)))) + (define (switch-to-ref repository ref) "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the OID (roughly the commit hash) corresponding to REF." @@ -305,6 +324,11 @@ OID (roughly the commit hash) corresponding to REF." (resolve-reference repository ref)) (reset repository obj RESET_HARD) + + ;; There might still be untracked files in REPOSITORY due to an interrupted + ;; checkout for example; delete them. + (delete-untracked-files repository) + (object-id obj)) (define (call-with-repository directory proc) diff --git a/tests/git.scm b/tests/git.scm index ad43435b67..9ccd04f0cd 100644 --- a/tests/git.scm +++ b/tests/git.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2019, 2020, 2022 Ludovic Courtès +;;; Copyright © 2019-2020, 2022, 2024 Ludovic Courtès ;;; Copyright © 2021 Xinglu Chen