Subject: bug#24089: 25.1; Dired: diff error on = <RET> if the file has a backup inside .~/

From: Eli Zaretskii <[email-masked]>
Date: Thu, 28 Jul 2016 18:05:12 +0300

> From: Jorge Peixoto de Morais Neto <[email-masked]>
> Date: Wed, 27 Jul 2016 15:42:26 -0300
> 
> Please extract the attached gzipped tarball into ~.
> 
> Inside ~, start Emacs with an init file containing the following:
> --8<---------------cut here---------------start------------->8---
> (custom-set-variables
>  ;; custom-set-variables was added by Custom.
>  ;; If you edit it by hand, you could mess it up, so be careful.
>  ;; Your init file should contain only one such instance.
>  ;; If there is more than one, they won't work right.
>  '(backup-directory-alist (quote (("." . ".~"))))
> )
> 
> (custom-set-faces
>  ;; custom-set-faces was added by Custom.
>  ;; If you edit it by hand, you could mess it up, so be careful.
>  ;; Your init file should contain only one such instance.
>  ;; If there is more than one, they won't work right.
>  )
> --8<---------------cut here---------------end--------------->8---
> 
> Perform the following steps:
> 1. C-x d ~/dired-diff-bug-1 <RET>
> 2. j dummy <RET>
> 3. = <RET>
> I expected (according to the documentation of dired-diff) to see the
> diff of dummy with its backup.  Instead a diff error occurs.  I have
> saved the diff error buffer to diff-error-dummy.txt and attached it.
> 
> Now perform the following steps:
> 1. C-x d ~/dired-diff-bug-1/.~ <RET>
> 2. j dummy~ <RET>
> 3. = <RET>.
> A similar diff error occurs. It is also unexpected.  I have saved the
> diff error buffer in diff-error-dummy~.txt and attached it.

Thanks.

It seems dired-diff was not prepared to deal with the situation where
the backup file is in another directory.  Does the patch below produce
good results?

--- lisp/dired-aux.el~0	2016-05-08 07:05:52.000000000 +0300
+++ lisp/dired-aux.el	2016-07-28 18:01:02.437091400 +0300
@@ -78,8 +78,14 @@
 	  (file-at-mark (if (and transient-mark-mode mark-active)
 			    (save-excursion (goto-char (mark t))
 					    (dired-get-filename t t))))
+          (separate-dir (and oldf
+                             (not (equal (file-name-directory oldf)
+                                         (dired-current-directory)))))
 	  (default-file (or file-at-mark
-			    (and oldf (file-name-nondirectory oldf))))
+			    (and oldf
+                                 (if separate-dir
+                                     oldf
+                                   (file-name-nondirectory oldf)))))
 	  ;; Use it as default if it's not the same as the current file,
 	  ;; and the target dir is current or there is a default file.
 	  (default (if (and (not (equal default-file current))
@@ -88,7 +94,9 @@
 				default-file))
 		       default-file))
 	  (target-dir (if default
-			  (dired-current-directory)
+                          (if separate-dir
+                              (file-name-directory default)
+                            (dired-current-directory))
 			(dired-dwim-target-directory)))
 	  (defaults (dired-dwim-target-defaults (list current) target-dir)))
      (list