1 ;;; ztree-diff.el --- Text mode diff for directory trees -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Alexey Veretennikov <alexey.veretennikov@gmail.com>
9 ;; Keywords: files tools
10 ;; URL: https://github.com/fourier/ztree
11 ;; Compatibility: GNU Emacs 24.x
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (require 'ztree-diff-model)
34 (defconst ztree-diff-hidden-files-regexp "^\\."
36 By default all filest starting with dot '.', including . and ..")
38 (defface ztreep-diff-header-face
39 '((((type tty pc) (class color)) :foreground "lightblue" :weight bold)
40 (((background dark)) (:height 1.2 :foreground "lightblue" :weight bold))
41 (t :height 1.2 :foreground "darkblue" :weight bold))
42 "*Face used for the header in Ztree Diff buffer."
43 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
44 (defvar ztreep-diff-header-face 'ztreep-diff-header-face)
46 (defface ztreep-diff-header-small-face
47 '((((type tty pc) (class color)) :foreground "lightblue" :weight bold)
48 (((background dark)) (:foreground "lightblue" :weight bold))
49 (t :weight bold :foreground "darkblue"))
50 "*Face used for the header in Ztree Diff buffer."
51 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
52 (defvar ztreep-diff-header-small-face 'ztreep-diff-header-small-face)
54 (defface ztreep-diff-model-diff-face
55 '((t (:foreground "red")))
56 "*Face used for different files in Ztree-diff."
57 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
58 (defvar ztreep-diff-model-diff-face 'ztreep-diff-model-diff-face)
60 (defface ztreep-diff-model-add-face
61 '((t (:foreground "blue")))
62 "*Face used for added files in Ztree-diff."
63 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
64 (defvar ztreep-diff-model-add-face 'ztreep-diff-model-add-face)
66 (defface ztreep-diff-model-ignored-face
67 '((((type tty pc) (class color) (min-colors 256)) :foreground "#2f2f2f")
68 (((type tty pc) (class color) (min-colors 8)) :foreground "white")
69 (t (:foreground "#7f7f7f" :strike-through t)))
70 "*Face used for non-modified files in Ztree-diff."
71 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
72 (defvar ztreep-diff-model-ignored-face 'ztreep-diff-model-ignored-face)
74 (defface ztreep-diff-model-normal-face
75 '((((type tty pc) (class color) (min-colors 8)) :foreground "white")
76 (t (:foreground "#7f7f7f")))
77 "*Face used for non-modified files in Ztree-diff."
78 :group 'Ztree-diff :group 'font-lock-highlighting-faces)
79 (defvar ztreep-diff-model-normal-face 'ztreep-diff-model-normal-face)
82 (defvar-local ztree-diff-filter-list (list ztree-diff-hidden-files-regexp)
83 "List of regexp file names to filter out.
84 By default paths starting with dot (like .git) are ignored")
86 (defvar-local ztree-diff-dirs-pair nil
87 "Pair of the directories stored. Used to perform the full rescan.")
89 (defvar-local ztree-diff-show-equal-files t
90 "Show or not equal files/directories on both sides.")
92 (defvar-local ztree-diff-show-filtered-files nil
93 "Show or not files from the filtered list.")
95 (defvar-local ztree-diff-wait-message nil
96 "Message showing while constructing the diff tree.")
100 (define-minor-mode ztreediff-mode
101 "A minor mode for displaying the difference of the directory trees in text mode."
106 ;; The minor mode keymap
108 (,(kbd "C") . ztree-diff-copy)
109 (,(kbd "h") . ztree-diff-toggle-show-equal-files)
110 (,(kbd "H") . ztree-diff-toggle-show-filtered-files)
111 (,(kbd "D") . ztree-diff-delete-file)
112 (,(kbd "v") . ztree-diff-view-file)
113 (,(kbd "d") . ztree-diff-simple-diff-files)
114 (,(kbd "r") . ztree-diff-partial-rescan)
115 (,(kbd "R") . ztree-diff-full-rescan)
116 ([f5] . ztree-diff-full-rescan)))
119 (defun ztree-diff-node-face (node)
120 "Return the face for the NODE depending on diff status."
121 (let ((diff (ztree-diff-node-different node)))
122 (cond ((eq diff 'ignore) ztreep-diff-model-ignored-face)
123 ((eq diff 'diff) ztreep-diff-model-diff-face)
124 ((eq diff 'new) ztreep-diff-model-add-face)
125 ((eq diff 'same) ztreep-diff-model-normal-face))))
127 (defun ztree-diff-insert-buffer-header ()
128 "Insert the header to the ztree buffer."
129 (ztree-insert-with-face "Differences tree" ztreep-diff-header-face)
131 (when ztree-diff-dirs-pair
132 (ztree-insert-with-face (concat "Left: " (car ztree-diff-dirs-pair))
133 ztreep-diff-header-small-face)
135 (ztree-insert-with-face (concat "Right: " (cdr ztree-diff-dirs-pair))
136 ztreep-diff-header-small-face)
138 (ztree-insert-with-face "Legend:" ztreep-diff-header-small-face)
140 (ztree-insert-with-face " Normal file " ztreep-diff-model-normal-face)
141 (ztree-insert-with-face "- same on both sides" ztreep-diff-header-small-face)
143 (ztree-insert-with-face " Orphan file " ztreep-diff-model-add-face)
144 (ztree-insert-with-face "- does not exist on other side" ztreep-diff-header-small-face)
146 (ztree-insert-with-face " Mismatch file " ztreep-diff-model-diff-face)
147 (ztree-insert-with-face "- different from other side" ztreep-diff-header-small-face)
149 (ztree-insert-with-face "Ignored file" ztreep-diff-model-ignored-face)
150 (ztree-insert-with-face " - ignored from comparison" ztreep-diff-header-small-face)
153 (ztree-insert-with-face "==============" ztreep-diff-header-face)
156 (defun ztree-diff-full-rescan ()
157 "Force full rescan of the directory trees."
159 (when (and ztree-diff-dirs-pair
160 (yes-or-no-p (format "Force full rescan?")))
161 (ztree-diff (car ztree-diff-dirs-pair) (cdr ztree-diff-dirs-pair))))
165 (defun ztree-diff-existing-common (node)
166 "Return the NODE if both left and right sides exist."
167 (let ((left (ztree-diff-node-left-path node))
168 (right (ztree-diff-node-right-path node)))
171 (file-exists-p right))
175 (defun ztree-diff-existing-common-parent (node)
176 "Return the first node in up in hierarchy of the NODE which has both sides."
177 (let ((common (ztree-diff-existing-common node)))
180 (ztree-diff-existing-common-parent (ztree-diff-node-parent node)))))
182 (defun ztree-diff-do-partial-rescan (node)
183 "Partly rescan the NODE."
184 (let* ((common (ztree-diff-existing-common-parent node))
185 (parent (ztree-diff-node-parent common)))
187 (when ztree-diff-dirs-pair
188 (ztree-diff (car ztree-diff-dirs-pair) (cdr ztree-diff-dirs-pair)))
189 (ztree-diff-update-wait-message
190 (concat "Updating " (ztree-diff-node-short-name common) " ..."))
191 (ztree-diff-model-partial-rescan common)
193 (ztree-refresh-buffer (line-number-at-pos)))))
196 (defun ztree-diff-partial-rescan ()
197 "Perform partial rescan on the current node."
199 (let ((found (ztree-find-node-at-point)))
201 (ztree-diff-do-partial-rescan (car found)))))
204 (defun ztree-diff-simple-diff (node)
205 "Create a simple diff buffer for files from left and right panels.
206 Argument NODE node containing paths to files to call a diff on."
207 (let* ((node-left (ztree-diff-node-left-path node))
208 (node-right (ztree-diff-node-right-path node)))
212 (not (file-directory-p node-left)))
213 ;; show the diff window on the bottom
214 ;; to not to crush tree appearance
215 (let ((split-width-threshold nil))
216 (diff node-left node-right)))))
219 (defun ztree-diff-simple-diff-files ()
220 "Create a simple diff buffer for files from left and right panels."
222 (let ((found (ztree-find-node-at-point)))
224 (let ((node (car found)))
225 (ztree-diff-simple-diff node)))))
227 (defun ztree-diff-node-action (node hard)
228 "Perform action on NODE:
229 1 if both left and right sides present:
230 1.1 if they are differend
232 1.1.2 simple diff otherwiste
233 1.2 if they are the same - view left
234 2 if left or right present - view left or rigth"
235 (let ((left (ztree-diff-node-left-path node))
236 (right (ztree-diff-node-right-path node))
237 ;; FIXME: The GNU convention is to only use "path" for lists of
238 ;; directories as in load-path.
239 (open-f #'(lambda (path) (if hard (find-file path)
240 (let ((split-width-threshold nil))
241 (view-file-other-window path))))))
242 (cond ((and left right)
243 (if (eql (ztree-diff-node-different node) 'same)
244 (funcall open-f left)
247 (ztree-diff-simple-diff node))))
248 (left (funcall open-f left))
249 (right (funcall open-f right))
254 (defun ztree-diff-copy-file (node source-path destination-path copy-to-right)
255 "Update the NODE status and copy the file.
256 File copied from SOURCE-PATH to DESTINATION-PATH.
257 COPY-TO-RIGHT specifies which side of the NODE to update."
258 (let ((target-path (concat
259 (file-name-as-directory destination-path)
260 (file-name-nondirectory
261 (directory-file-name source-path)))))
262 (let ((err (condition-case error-trap
264 ;; don't ask for overwrite
266 (copy-file source-path target-path t t)
268 (error error-trap))))
269 ;; error message if failed
270 (if err (message (concat "Error: " (nth 2 err)))
272 ;; assuming all went ok when left and right nodes are the same
273 ;; set both as not different if they were not ignored
274 (unless (eq (ztree-diff-node-different node) 'ignore)
275 (setf (ztree-diff-node-different node) 'same))
276 ;; update left/right paths
278 (setf (ztree-diff-node-right-path node) target-path)
279 (setf (ztree-diff-node-left-path node) target-path))
280 (ztree-diff-node-update-all-parents-diff node)
281 (ztree-refresh-buffer (line-number-at-pos))))))
284 (defun ztree-diff-copy-dir (node source-path destination-path copy-to-right)
285 "Update the NODE status and copy the directory.
286 Directory copied from SOURCE-PATH to DESTINATION-PATH.
287 COPY-TO-RIGHT specifies which side of the NODE to update."
288 (let* ((src-path (file-name-as-directory source-path))
289 (target-path (file-name-as-directory destination-path))
290 (target-full-path (concat
292 (file-name-nondirectory
293 (directory-file-name source-path)))))
294 (let ((err (condition-case error-trap
298 (copy-directory src-path target-path t t)
300 (error error-trap))))
301 ;; error message if failed
304 (message (concat "Error: " (nth 1 err)))
305 ;; and do rescan of the node
306 (ztree-diff-do-partial-rescan node))
307 ;; if everything is ok, update statuses
308 (message target-full-path)
310 (setf (ztree-diff-node-right-path node) target-full-path)
311 (setf (ztree-diff-node-left-path node) target-full-path))
312 (ztree-diff-update-wait-message
313 (concat "Updating " (ztree-diff-node-short-name node) " ..."))
314 ;; TODO: do not rescan the node. Use some logic like in delete
315 (ztree-diff-model-update-node node)
317 (ztree-diff-node-update-all-parents-diff node)
318 (ztree-refresh-buffer (line-number-at-pos))))))
321 (defun ztree-diff-copy ()
322 "Copy the file under the cursor to other side."
324 (let ((found (ztree-find-node-at-point)))
326 (let* ((node (car found))
328 (node-side (ztree-diff-node-side node))
329 (copy-to-right t) ; copy from left to right
330 (node-left (ztree-diff-node-left-path node))
331 (node-right (ztree-diff-node-right-path node))
333 (destination-path nil)
334 (parent (ztree-diff-node-parent node)))
335 (when parent ; do not copy the root node
336 ;; determine a side to copy from/to
338 ;; 1) if both side are present, use the side
340 (setq copy-to-right (if (eq node-side 'both)
342 ;; 2) if one of sides is absent, copy from
343 ;; the side where the file is present
344 (eq node-side 'left)))
345 ;; 3) in both cases determine if the destination
346 ;; directory is in place
347 (setq source-path (if copy-to-right node-left node-right)
348 destination-path (if copy-to-right
349 (ztree-diff-node-right-path parent)
350 (ztree-diff-node-left-path parent)))
351 (when (and source-path destination-path
352 (yes-or-no-p (format "Copy [%s]%s to [%s]%s/ ?"
353 (if copy-to-right "LEFT" "RIGHT")
354 (ztree-diff-node-short-name node)
355 (if copy-to-right "RIGHT" "LEFT")
357 (if (file-directory-p source-path)
358 (ztree-diff-copy-dir node
362 (ztree-diff-copy-file node
365 copy-to-right))))))))
367 (defun ztree-diff-view-file ()
368 "View file at point, depending on side."
370 (let ((found (ztree-find-node-at-point)))
372 (let* ((node (car found))
374 (node-side (ztree-diff-node-side node))
375 (node-left (ztree-diff-node-left-path node))
376 (node-right (ztree-diff-node-right-path node)))
377 (when (or (eq node-side 'both)
379 (cond ((and (eq side 'left)
381 (view-file node-left))
382 ((and (eq side 'right)
384 (view-file node-right))))))))
387 (defun ztree-diff-delete-file ()
388 "Delete the file under the cursor."
390 (let ((found (ztree-find-node-at-point)))
392 (let* ((node (car found))
394 (node-side (ztree-diff-node-side node))
395 (parent (ztree-diff-node-parent node))
396 ;; algorithm for determining what to delete similar to copy:
397 ;; 1. if the file is present on both sides, delete
398 ;; from the side currently selected
399 ;; 2. if one of sides is absent, delete
400 ;; from the side where the file is present
402 (or (eql node-side 'left)
403 (and (eql node-side 'both)
405 (remove-path (if delete-from-left
406 (ztree-diff-node-left-path node)
407 (ztree-diff-node-right-path node))))
408 (when (and parent ; do not delete the root node
409 (yes-or-no-p (format "Delete the file [%s]%s ?"
410 (if delete-from-left "LEFT" "RIGHT")
412 (let* ((delete-command
413 (if (file-directory-p remove-path)
416 (children (ztree-diff-node-children parent))
418 (condition-case error-trap
420 (funcall delete-command remove-path t)
422 (error error-trap))))
425 (message (concat "Error: " (nth 2 err)))
426 ;; when error happened while deleting the
427 ;; directory, rescan the node
428 ;; and update the parents with a new status
430 (when (file-directory-p remove-path)
431 (ztree-diff-model-partial-rescan node)))
433 ;; if was only on one side
434 ;; remove the node from children
435 (if (or (and (eql node-side 'left)
437 (and (eql node-side 'right)
438 (not delete-from-left)))
439 (setf (ztree-diff-node-children parent)
441 (lambda (x) (not (ztree-diff-node-equal x node)))
443 ;; otherwise update only one side
444 (mapc (if delete-from-left
445 (lambda (x) (setf (ztree-diff-node-left-path x) nil))
446 (lambda (x) (setf (ztree-diff-node-right-path x) nil)))
447 (cons node (ztree-diff-node-children node)))
448 ;; and update diff status
449 ;; if was ignored keep the old status
450 (unless (eql (ztree-diff-node-different node) 'ignore)
451 (setf (ztree-diff-node-different node) 'new))
452 ;; finally update all children statuses
453 (ztree-diff-node-update-diff-from-parent node)))
454 (ztree-diff-node-update-all-parents-diff node)
455 (ztree-refresh-buffer (line-number-at-pos))))))))
459 (defun ztree-diff-node-ignore-p (node)
460 "Determine if the NODE is in filter list.
461 If the node is in the filter list it shall not be visible,
462 unless it is a parent node."
463 (let ((name (ztree-diff-node-short-name node)))
465 ;; not a root and is in filter list
466 (and (ztree-diff-node-parent node)
467 (ztree-find ztree-diff-filter-list #'(lambda (rx) (string-match rx name))))))
470 (defun ztree-node-is-visible (node)
471 "Determine if the NODE should be visible."
472 (let ((diff (ztree-diff-node-different node)))
474 ;; either it is a root. root have no parent
475 (or (not (ztree-diff-node-parent node)) ; parent is always visible
476 ;; or the files are different or orphan
479 ;; or it is ignored but we show ignored for now
480 (and (eql diff 'ignore)
481 ztree-diff-show-filtered-files)
482 ;; or they are same but we show same for now
483 (and (eql diff 'same)
484 ztree-diff-show-equal-files))))
486 (defun ztree-diff-toggle-show-equal-files ()
487 "Toggle visibility of the equal files."
489 (setq ztree-diff-show-equal-files (not ztree-diff-show-equal-files))
490 (message (concat (if ztree-diff-show-equal-files "Show" "Hide") " equal files"))
491 (ztree-refresh-buffer))
493 (defun ztree-diff-toggle-show-filtered-files ()
494 "Toggle visibility of the filtered files."
496 (setq ztree-diff-show-filtered-files (not ztree-diff-show-filtered-files))
497 (message (concat (if ztree-diff-show-filtered-files "Show" "Hide") " filtered files"))
498 (ztree-refresh-buffer))
501 (defun ztree-diff-update-wait-message (&optional msg)
502 "Update the wait mesage with one more '.' progress indication."
504 (setq ztree-diff-wait-message msg)
505 (when ztree-diff-wait-message
506 (setq ztree-diff-wait-message (concat ztree-diff-wait-message "."))))
507 (message ztree-diff-wait-message))
510 (defun ztree-diff (dir1 dir2)
511 "Create an interactive buffer with the directory tree of the path given.
512 Argument DIR1 left directory.
513 Argument DIR2 right directory."
514 (interactive "DLeft directory \nDRight directory ")
515 (unless (and dir1 (file-directory-p dir1))
516 (error "Path %s is not a directory" dir1))
517 (unless (file-exists-p dir1)
518 (error "Path %s does not exist" dir1))
519 (unless (and dir2 (file-directory-p dir2))
520 (error "Path %s is not a directory" dir2))
521 (unless (file-exists-p dir2)
522 (error "Path %s does not exist" dir2))
524 (ztree-diff-node-create nil dir1 dir2 nil))
525 (buf-name (concat "*"
526 (ztree-diff-node-short-name model)
528 (ztree-diff-node-right-short-name model)
530 ;; after this command we are in a new buffer,
531 ;; so all buffer-local vars are valid
534 'ztree-node-is-visible
535 'ztree-diff-insert-buffer-header
536 'ztree-diff-node-short-name-wrapper
537 'ztree-diff-node-is-directory
538 'ztree-diff-node-equal
539 'ztree-diff-node-children
540 'ztree-diff-node-face
541 'ztree-diff-node-action
542 'ztree-diff-node-side)
544 (ztree-diff-model-set-ignore-fun #'ztree-diff-node-ignore-p)
545 (ztree-diff-model-set-progress-fun #'ztree-diff-update-wait-message)
546 (setq ztree-diff-dirs-pair (cons dir1 dir2))
547 (ztree-diff-update-wait-message (concat "Comparing " dir1 " and " dir2 " ..."))
548 (ztree-diff-node-recreate model)
551 (ztree-refresh-buffer)))
558 (provide 'ztree-diff)
559 ;;; ztree-diff.el ends here