all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 71900@debbugs.gnu.org
Cc: "Ricardo Wurmus" <rekado@elephly.net>,
	"Ludovic Courtès" <ludo@gnu.org>,
	"Christopher Baines" <guix@cbaines.net>,
	"Josselin Poiret" <dev@jpoiret.xyz>,
	"Ludovic Courtès" <ludo@gnu.org>,
	"Mathieu Othacehe" <othacehe@gnu.org>,
	"Simon Tournier" <zimon.toutoune@gmail.com>,
	"Tobias Geerinckx-Rice" <me@tobias.gr>
Subject: [bug#71900] [PATCH] git: Remove untracked files from cached checkouts.
Date: Tue,  2 Jul 2024 15:03:01 +0200	[thread overview]
Message-ID: <384ad3938ada52672969a2d8563d132c34e9dd01.1719925366.git.ludo@gnu.org> (raw)

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@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@gnu.org>
+;;; Copyright © 2019-2020, 2022, 2024 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2021 Xinglu Chen <public@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	other threads:[~2024-07-02 13:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-02 13:03 Ludovic Courtès [this message]
2024-07-18 15:52 ` bug#71900: [PATCH] git: Remove untracked files from cached checkouts Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=384ad3938ada52672969a2d8563d132c34e9dd01.1719925366.git.ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=71900@debbugs.gnu.org \
    --cc=dev@jpoiret.xyz \
    --cc=guix@cbaines.net \
    --cc=me@tobias.gr \
    --cc=othacehe@gnu.org \
    --cc=rekado@elephly.net \
    --cc=zimon.toutoune@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.