From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.help Subject: Re: Reviewing versioned backups Date: Wed, 31 Mar 2021 15:29:13 +0200 Message-ID: <87wntnv4d2.fsf@posteo.net> References: <87a6qvat9v.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18668"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Mar 31 15:29:56 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRauy-0004kx-Az for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 31 Mar 2021 15:29:56 +0200 Original-Received: from localhost ([::1]:44570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRaux-0001zR-By for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 31 Mar 2021 09:29:55 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47258) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRauO-0001z6-GB for help-gnu-emacs@gnu.org; Wed, 31 Mar 2021 09:29:20 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:59523) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRauM-0003LE-2Q for help-gnu-emacs@gnu.org; Wed, 31 Mar 2021 09:29:20 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 89BDB160065 for ; Wed, 31 Mar 2021 15:29:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1617197354; bh=WrqwQQaQcSE/oPJB8rua3ZpqLrP/M0Om01bq8ERFTw8=; h=From:To:Subject:Date:From; b=FEiLHidynSd3YgaI2pJ0M+ttgxFPHZgQyUQ+PBUdcUrwwY2CEoo0hbIKkFKKkRSSd fK87+GmiuW9j3/NiGWQKnN2+EBFvj2N8SAEjtuSRAzFguYiNevV5TKbnUTy1tvCoLc np90u8LCfSSYqmqDY9b6/+t6/QxF/yNmD4kep0I8WRoRQYZ1HOSL7jL2bfZXFrgJbA 4o6pm83xvyMkP7SzkRZUDUjzs3SB0vxMhjWw91UIlfVu5m4CeGR0eOK1XiFBj1sQDY pmVSMZXusrG+W+rbzPQNicG/8pNr5sRIhQZoImlJN0xzOxXpM3CnVULfqJJgQrKrZA bmC+kxEup1RHA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4F9Rx56wngz9rxK for ; Wed, 31 Mar 2021 15:29:13 +0200 (CEST) In-Reply-To: <87a6qvat9v.fsf@posteo.net> (Philip Kaludercic's message of "Mon, 22 Mar 2021 20:21:48 +0100") Received-SPF: pass client-ip=185.67.36.65; envelope-from=philipk@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128793 Archived-At: --=-=-= Content-Type: text/plain Philip Kaludercic writes: > Hey, > > I have been using version-control for years now, but have always > wondered if there was a better way to review previous versions besides > just going into your backup directory and manually comparing them. Is > there maybe a vc backend that uses the backup information? Or has > someone written some code to make it easier that they would care to > share? I sat down and wrote a sketch for a VC backend that uses version backups. Find it attached to this message. It's not that mature yet, but it does the basics. Would there be any interest to add this to Emacs or as an ELPA package? -- Philip K. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=vc-backup.el Content-Transfer-Encoding: quoted-printable ;;; vc-backup.el --- VC backend for versioned backups -*- lexical-binding:= t; -*- ;; Copyright (C) 2021 Philip Kaludercic ;; Author: Philip Kaludercic ;; Keywords: vc ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Here find a VC backend that uses backup files for versioning. It ;; is recommended to enable `version-control' and related variables, ;; to make the most use of it. ;; ;; To install this VC backend, evaluate ;; ;; (add-to-list 'vc-handled-backends 'Backup t) ;; ;; or add it to your initialisation file. ;; ;; There is no need or ability to manually "commit" anything, as ;; backups should be generated automatically. To force a backup, read ;; up on the documentation of `save-buffer'. Backups can be viewed ;; using the command `vc-print-log'. ;;; Code: (eval-when-compile (require 'subr-x)) (require 'cl-lib) (require 'diff) (require 'vc) (require 'log-view) (defun vc-backup-list-backups (file-or-list) "Generate a list of all backups for FILE-OR-LIST. FILE-OR-LIST can either be a string or a list of strings. This function returns all backups for these files, in order of their recency." (let ((files (if (listp file-or-list) file-or-list (list file-or-list))) versions) (dolist (file files) (let ((filename (thread-last (expand-file-name file) make-backup-file-name file-name-sans-versions))) (push (directory-files (file-name-directory filename) t (concat (regexp-quote (file-name-nondirectory filename)) file-name-version-regexp "\\'") t) versions))) (sort (apply #'append versions) #'file-newer-than-file-p))) (defun vc-backup-extract-version (file-or-backup) "Return a revision string for FILE-OR-BACKUP. If FILE-OR-BACKUP is the actual file, \"real\" is returned. Otherwise, it returns the version number as a string or \"prev\" for unversioned backups." (cond ((not (backup-file-name-p file-or-backup)) "real") ((string-match "\\.~\\([[:digit:]]+\\)~\\'" file-or-backup) (match-string 1 file-or-backup)) (t "prev"))) (defun vc-backup-list-backup-versions (file) "Return an association list of backup files and versions for FILE. Each element of the list has the form (VERS . BACKUP), where VERS is the version string as generated by `vc-backup-extract-version' and BACKUP is the actual backup file." (let (files) (dolist (backup (vc-backup-list-backups file)) (push (cons (vc-backup-extract-version backup) backup) files)) files)) (defun vc-backup-get-backup-file (file rev) "Return backup file for FILE of the version REV." (cond ((string=3D rev "real") file) ((string=3D rev "prev") (let ((prev (thread-last (expand-file-name file) make-backup-file-name file-name-sans-versions (format "%~")))) (and (file-exists-p prev) prev))) ((cdr (assoc rev (vc-backup-list-backup-versions file)))))) (defun vc-backup-revision-granularity () "Inform VC that this backend only operates on singular files." 'file) ;;;###autoload (defun vc-backup-registered (file) "Inform VC that FILE will work if a backup can be found." (not (null (diff-latest-backup-file file)))) (defun vc-backup-working-revision (_file) "Inform VC that the current file is always the real one." "real") (defun vc-backup-checkout-model (_files) "Inform VC that files are not locked." 'implicit) (defun vc-backup-responsible-p (file) "Inform VC that this backend requires a backup for FILE." (not (null (diff-latest-backup-file file)))) (defun vc-backup-find-revision (file rev buffer) "Open a backup of the version REV for FILE in BUFFER." (with-current-buffer buffer (insert-file-contents (vc-backup-get-backup-file file rev)))) (defun vc-backup-checkout (file &optional rev) "Before copying an old version of FILE, force a backup. If REV is non-nil, checkout that version." (cl-assert (=3D (length file) 1)) (let ((backup-inhibited nil) (make-backup-files t)) (with-current-buffer (find-file-noselect file) (backup-buffer))) (copy-file (vc-backup-get-backup-file file rev) file t)) (defun vc-backup-find-admin-dir (file) "Inform VC that the FILE's backup directory is the administrative directo= ry." (file-name-directory (diff-latest-backup-file file))) (defun vc-backup-print-log (file buffer &optional _shortlog _start-revision= _limit) "Generate a listing of old backup versions for FILE. The results are written into BUFFER." (setq file (if (listp file) (car file) file)) (with-current-buffer buffer (let ((inhibit-read-only t)) (erase-buffer) (insert "Backups for " file "\n\n") (dolist (rev (nreverse (vc-backup-list-backup-versions file))) (let* ((attr (file-attributes (cdr rev))) (mtime (file-attribute-modification-time attr)) (mdate (format-time-string "%c" mtime))) (insert (format "v%s\tFrom %s\n" (car rev) mdate)))))) 'limit-unsupported) (define-derived-mode vc-backup-log-view-mode log-view-mode "Backup Log" "VC-Log Mode for Backup" (setq-local log-view-file-re "\\`Backups for \\(.+\\)$") (setq-local log-view-message-re "^v\\([^\t]+\\)")) (defun vc-backup-diff (files &optional rev1 rev2 buffer async) "Generate a diff for FILES between versions REV1 and REV2. BUFFER and ASYNC as interpreted as specified in vc.el." (cl-assert (=3D (length files) 1)) (when (null rev1) (setq rev1 "real")) (when (null rev2) (setq rev2 (vc-backup-extract-version (car (vc-backup-list-backups files))))) (save-window-excursion (let ((dirty nil)) (dolist (file files) (let ((diff (diff-no-select (vc-backup-get-backup-file file rev1) (vc-backup-get-backup-file file rev2) (vc-switches 'Backup 'diff) (not async) (get-buffer (or buffer "*vc-diff*"))))) (unless async (with-current-buffer diff (unless (search-forward "no differences" nil t) (setq dirty t)))))) (if dirty 1 0)))) (defun vc-backup-make-version-backups-p (_file) "Always allow backup files to be made for this backend." t) (defun vc-backup-previous-revision (file rev) "Determine the revision before REV for FILE." (let* ((backups (vc-backup-list-backups file)) (index (cl-position rev backups :key #'car))) (cond ((string=3D rev "real") (car backups)) ((string=3D rev "prev") nil) ((and (natnump index) (> index 0)) (car (nth (1- index) backups)))))) (defun vc-backup-next-revision (file rev) "Determine the revision after REV for FILE." (let* ((backups (vc-backup-list-backups file)) (index (cl-position rev backups :key #'car))) (cond ((string=3D rev "real") nil) ((string=3D rev "prev") "real") ((and (natnump index) (< index (length backups))) (car (nth (1+ index) backups))) ((error "Illegal state"))))) (defun vc-backup-delete-file (file) "Delete FILE and all its backup." (dolist (backup (vc-backup-list-backups file)) (delete-file backup)) (delete-file file)) (defun vc-backup-rename-file (old-file new-file) "Rename OLD-FILE to NEW-FILE and all its backup accordingly." (rename-file old-file new-file) (let ((new-part (thread-last (expand-file-name new-file) make-backup-file-name file-name-sans-versions)) (old-part (thread-last (expand-file-name old-file) make-backup-file-name file-name-sans-versions))) (dolist (backup (vc-backup-list-backups old-file)) (let ((new-backup (concat new-part (substring backup (length old-part= ))))) (rename-file backup new-backup t))))) (provide 'vc-backup) ;;; vc-backup.el ends here --=-=-=--