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

From: Philip Kaludercic <[email-masked]>
Date: Sun, 16 Feb 2025 16:17:58 +0000

Stefan Kangas <[email-masked]> writes:

> Philip Kaludercic <[email-masked]> writes:
>
>> From 761ee105380c60bcf410e3f3f6e15af8073b1549 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 | 4 +++-
>>  1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
>> index 70497a97d56..1aa4f87d1ea 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
>> +                    (make-temp-file "emacs-diff" t))))
>>      (with-current-buffer buf
>>        (setq buffer-read-only t)
>>        (buffer-disable-undo (current-buffer))
>> --
>> 2.47.2
>
> Can we please also remove the temporary directory when we're done?

Sure, this should do the job:

diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
index 1aa4f87d1ea..bb509d0d4e3 100644
--- a/lisp/vc/diff.el
+++ b/lisp/vc/diff.el
@@ -188,9 +188,10 @@ diff-no-select
                                   (list (or old-alt old)
                                         (or new-alt new)))))
 		     " "))
-	 (thisdir (if (file-exists-p default-directory)
-                      default-directory
-                    (make-temp-file "emacs-diff" t))))
+         (use-temp-dir (not (file-exists-p default-directory)))
+         (thisdir (if use-temp-dir
+                      (make-temp-file "emacs-diff" t)
+                    default-directory)))
     (with-current-buffer buf
       (setq buffer-read-only t)
       (buffer-disable-undo (current-buffer))
@@ -220,6 +221,8 @@ diff-no-select
 	   (call-process shell-file-name nil buf nil
 			 shell-command-switch command)
            old-alt new-alt))))
+    (when use-temp-dir
+      (delete-directory thisdir t))
     buf))
 
 (defun diff-process-filter (proc string)

Or would it be better to keep the directory around and re-use it?