GNU bug report logs - #71900
[PATCH] git: Remove untracked files from cached checkouts.

Previous Next

Package: guix-patches;

Reported by: Ludovic Courtès <ludo <at> gnu.org>

Date: Tue, 2 Jul 2024 13:04:01 UTC

Severity: normal

Tags: patch

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 71900 in the body.
You can then email your comments to 71900 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to guix <at> cbaines.net, dev <at> jpoiret.xyz, ludo <at> gnu.org, othacehe <at> gnu.org, zimon.toutoune <at> gmail.com, me <at> tobias.gr, guix-patches <at> gnu.org:
bug#71900; Package guix-patches. (Tue, 02 Jul 2024 13:04:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ludovic Courtès <ludo <at> gnu.org>:
New bug report received and forwarded. Copy sent to guix <at> cbaines.net, dev <at> jpoiret.xyz, ludo <at> gnu.org, othacehe <at> gnu.org, zimon.toutoune <at> gmail.com, me <at> tobias.gr, guix-patches <at> gnu.org. (Tue, 02 Jul 2024 13:04:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: guix-patches <at> gnu.org
Cc: Ricardo Wurmus <rekado <at> elephly.net>,
 Ludovic Courtès <ludo <at> gnu.org>
Subject: [PATCH] git: Remove untracked files from cached checkouts.
Date: Tue,  2 Jul 2024 15:03:01 +0200
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 <rekado <at> elephly.net>
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 d75a301f98b..48a962089de 100644
--- a/guix/git.scm
+++ b/guix/git.scm
@@ -298,6 +298,25 @@ (define (resolve-reference repository ref)
       (('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 @@ (define (switch-to-ref repository 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 ad43435b674..9ccd04f0cdf 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 <ludo <at> gnu.org>
+;;; Copyright © 2019-2020, 2022, 2024 Ludovic Courtès <ludo <at> gnu.org>
 ;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz
 ;;;
 ;;; This file is part of GNU Guix.
@@ -259,4 +259,24 @@ (define-module (test-git)
          ;; COMMIT should be the ID of the commit object, not that of the tag.
          (string=? commit head))))))
 
+(test-assert "update-cached-checkout, untracked files removed"
+  (call-with-temporary-directory
+   (lambda (cache)
+     (with-temporary-git-repository directory
+         '((add "a.txt" "A")
+           (add ".gitignore" ".~\n")
+           (commit "First commit"))
+       (let ((directory commit relation
+                        (update-cached-checkout directory
+                                                #:ref '()
+                                                #:cache-directory cache)))
+         (close-port
+          (open-output-file (in-vicinity cache "stale-untracked-file")))
+         (let ((directory2 commit2 relation2
+                           (update-cached-checkout directory
+                                                   #:ref '()
+                                                   #:cache-directory cache)))
+           (not (file-exists?
+                 (in-vicinity cache "stale-untracked-file")))))))))
+
 (test-end "git")

base-commit: bd908af0c619cb1b74afeeb07839d7af08de9d91
-- 
2.45.2





Reply sent to Ludovic Courtès <ludo <at> gnu.org>:
You have taken responsibility. (Thu, 18 Jul 2024 15:53:02 GMT) Full text and rfc822 format available.

Notification sent to Ludovic Courtès <ludo <at> gnu.org>:
bug acknowledged by developer. (Thu, 18 Jul 2024 15:53:02 GMT) Full text and rfc822 format available.

Message #10 received at 71900-done <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: 71900-done <at> debbugs.gnu.org
Cc: Josselin Poiret <dev <at> jpoiret.xyz>,
 Simon Tournier <zimon.toutoune <at> gmail.com>, Mathieu Othacehe <othacehe <at> gnu.org>,
 Tobias Geerinckx-Rice <me <at> tobias.gr>, Ricardo Wurmus <rekado <at> elephly.net>,
 Christopher Baines <guix <at> cbaines.net>
Subject: Re: [bug#71900] [PATCH] git: Remove untracked files from cached
 checkouts.
Date: Thu, 18 Jul 2024 17:52:35 +0200
Ludovic Courtès <ludo <at> gnu.org> skribis:

> 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 <rekado <at> elephly.net>
> Change-Id: Iccbe644ade396ad27a037db7e0ef1c2a68ef91ce

Pushed as 58e268c2e30567f415fa4e02086e49299c31406a.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 16 Aug 2024 11:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 80 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.