help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reviewing versioned backups


From: Jean Louis
Subject: Re: Reviewing versioned backups
Date: Wed, 24 Mar 2021 14:05:11 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

To add on the database backed backing of files or recording files as
specific revisions, for myself personally I find it handy for text
files and programming sources or letters. Database can be
automatically locally backed up, encrypted and remotely backed
up. Specific file revisions can be easily extracted, accessed, or
differentiated.

For now I work with functions below related to files. For database
entries it is automatic.

- C-r r - personal key binding to invoke `rcd-vc-revision' and
  inserting the revision into the database. This is similar as
  registration of a file in versioning system if the file was not
  there, otherwise new version is inserted. Nothing much to think
  about.

  Interesting is that if I delete the whole file, revisions are still
  there, even if I delete whole directory as database is
  separate. With a function in future, it could be possible to convert
  the revision system from a database into RCS or Git or some other
  system. But this way, personally, I need not have file based
  versioning system.

  Those `backup.~1~' files are useful, but not as handy to browse or
  review, I will switch that back to this database based backing up,
  it is revision system.

- C-x k - has the hook `rcd-vc-insert-revision-on-kill-buffer' which
  verifies if the file by its file path belonging to the buffer exists
  in the database or not, if it does exists, the file is automatically
  backed up, new revision by its ID is automatically creted -- I could
  as well add versioning of a buffer without file name

I am sure this can be implemented with SQLite for single user, but
when it is implemented with the database user could even have the
database online, or on remote computers and files would get backed
up. Reminders could be sent to associated people, and collaboration
and review of changes and review of files becomes available to a team
including available from any kind of devices, provided there is
software to fetch such entries.

(defvar rcd-vc-ask-for-revision t
  "Global decision to ask user for revision number or use
automatically assigned database ID numbers.")

(defun rcd-vc-insert-revision (filename title &optional description revision)
  "Insert the revised FILENAME with TITLE into the
database. Optional DESCRIPTION and REVISION may be provided."
  (if (or (file-directory-p filename)
          (not (file-exists-p filename))
          (not (file-readable-p filename)))
      (message "Cannot read or access file: %s" filename)
    (let* ((file (expand-file-name filename))
           (original-file file)
           (file (sql-escape-string file))
           (title (sql-escape-string title))
           (description (if description
                            (sql-escape-string description)
                          "NULL"))
           (revision (if revision
                         (sql-escape-string revision)
                       "NULL"))
           (file-body (file-to-string original-file))
           (file-body (sql-escape-string file-body))
           (sql (format "INSERT INTO vcfiles (vcfiles_filename, vcfiles_title, 
vcfiles_description, vcfiles_filebody, vcfiles_revision) VALUES (%s, %s, %s, 
%s, %s) RETURNING vcfiles_id" file title description file-body revision)))
      (let ((id (rcd-sql-first sql *cf*)))
        (if id
            (message "File revision ID: %s for %s recorded" id original-file)
          (error "Could not record revision for: %s" original-file))))))

(defun rcd-vc-previous-revision (filename)
  "Returns previous available revision number if any by using
FILENAME."
  (let* ((filename (expand-file-name filename))
         (filename (sql-escape-string filename))
         (sql (format "SELECT vcfiles_revision FROM vcfiles WHERE 
vcfiles_filename = %s ORDER BY vcfiles_id DESC LIMIT 1" filename)))
    (rcd-sql-first sql *cf*)))

(defun rcd-vc-previous-revisions-exist-p (filename)
  "Returns T if previous revisions for FILENAME exist in the
`vcfiles' database table."
  (let* ((filename (expand-file-name filename))
         (filename (sql-escape-string filename))
         (sql (format "SELECT vcfiles_id FROM vcfiles WHERE vcfiles_filename = 
%s ORDER BY vcfiles_id DESC LIMIT 1" filename)))
    (rcd-sql-first sql *cf*)))

(defun rcd-vc-revision ()
  "Record RCD Version Control for the current file of the current
buffer into the database."
  (interactive)
  (when (buffer-modified-p)
    (when (y-or-n-p (format "Save %s?" (expand-file-name (buffer-file-name))))
      (save-buffer)))
  (let ((title (read-from-minibuffer "Title: "))
        (description (if (y-or-n-p "Edit description?")
                         (read-from-buffer "" (concat "Description of changes 
for: "
                                                      filename))
                       nil))
        (revision (when rcd-vc-ask-for-revision
                    (when (y-or-n-p "Enter revision designation?")
                      (read-from-minibuffer "Revision designation: " 
(rcd-vc-previous-revision filename)))))
        (filename (expand-file-name (buffer-file-name))))
    (rcd-vc-insert-revision filename title description revision)))
 
;; TODO if user enters revision same as one of previous, need to get a
;; warning that it is same

(defun rcd-vc-insert-revision-on-kill-buffer ()
  (when (buffer-file-name)
    (let* ((filename (expand-file-name (buffer-file-name)))
           (timestamp (rcd-timestamp))
           (title (concat "Automatic revision: " timestamp)))
      (when filename
        (when (rcd-vc-previous-revisions-exist-p filename)
          (rcd-vc-insert-revision filename title))))))

;; This may be added to automatically insert revision on killing of
;; the buffer if the buffer file name already exists in the database.
;; (add-hook 'kill-buffer-hook 'rcd-vc-insert-revision-on-kill-buffer)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]