Subject: bug#69606: [PATCH] Ensure default-directory exists when generating diff

From: Philip Kaludercic <[email-masked]>
Date: Sun, 16 Feb 2025 21:18:45 +0000

Michael Albinus <[email-masked]> writes:

> Philip Kaludercic <[email-masked]> writes:
>
>> Or would it be better to keep the directory around and re-use it?
>
> Pls don't.
>
> I don't understand why you can't use temporary-file-directory. Did I
> miss this point in the discussion?
>
> Best regards, Michael.

No, you are absolutely right, that is the better idea!

>From 9793f34698a7245c7a83d6d49b6afc54aa3c0e01 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Thu, 7 Mar 2024 10:06:48 +0100
Subject: [PATCH] Ensure default-directory exists when generating diff

* lisp/vc/diff.el (diff-no-select): Fall back to a fresh temporary
directory if 'default-directory' points to an invalid path.
---
 lisp/vc/diff.el | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
index 70497a97d56..cd4dc472c01 100644
--- a/lisp/vc/diff.el
+++ b/lisp/vc/diff.el
@@ -188,7 +188,9 @@ diff-no-select
                                   (list (or old-alt old)
                                         (or new-alt new)))))
 		     " "))
-	 (thisdir default-directory))
+         (thisdir (if (file-exists-p default-directory)
+                      default-directory
+                    temporary-file-directory)))
     (with-current-buffer buf
       (setq buffer-read-only t)
       (buffer-disable-undo (current-buffer))
@@ -203,21 +205,22 @@ diff-no-select
       (setq diff-default-directory default-directory)
       (let ((inhibit-read-only t))
 	(insert command "\n"))
-      (if (and (not no-async) (fboundp 'make-process))
-	  (let ((proc (start-process "Diff" buf shell-file-name
-                                     shell-command-switch command)))
-	    (set-process-filter proc #'diff-process-filter)
-            (set-process-sentinel
-             proc (lambda (proc _msg)
-                    (with-current-buffer (process-buffer proc)
-                      (diff-sentinel (process-exit-status proc)
-                                     old-alt new-alt)))))
-	;; Async processes aren't available.
-	(let ((inhibit-read-only t))
-	  (diff-sentinel
-	   (call-process shell-file-name nil buf nil
-			 shell-command-switch command)
-           old-alt new-alt))))
+      (with-file-modes #o600
+        (if (and (not no-async) (fboundp 'make-process))
+	    (let ((proc (start-process "Diff" buf shell-file-name
+                                       shell-command-switch command)))
+	      (set-process-filter proc #'diff-process-filter)
+              (set-process-sentinel
+               proc (lambda (proc _msg)
+                      (with-current-buffer (process-buffer proc)
+                        (diff-sentinel (process-exit-status proc)
+                                       old-alt new-alt)))))
+	  ;; Async processes aren't available.
+	  (let ((inhibit-read-only t))
+	    (diff-sentinel
+	     (call-process shell-file-name nil buf nil
+			   shell-command-switch command)
+             old-alt new-alt)))))
     buf))
 
 (defun diff-process-filter (proc string)
-- 
2.47.2