From 171a8db32f5b51d3ddb4a31356e1d9c581b4bbd2 Mon Sep 17 00:00:00 2001 From: Kisaragi Hiu Date: Tue, 14 Jun 2022 08:01:56 +0900 Subject: [PATCH] (db)fix: file modification detection failing in some edge cases (#2221) One example of an edge case: - If a file has LF line endings, - and is situated in a repository where .editorconfig asks for CRLF, (Such repositories exist, for example .) - and editorconfig-mode is enabled, org-roam-db--file-hash would then return different results depending on whether the file path is passed to it or not. While this is a contrived edge case, there may be other cases like this where the data from insert-file-contents-literally and the data from find-file-noselect makes secure-hash return different values. This commit removes the branch in org-roam-db--file-hash to compute the hash based on buffer content, instead making it always require a file path. (This commit also allows org-roam-db-insert-file to take an optional HASH argument, allowing it to avoid recalculating the hash.) Since now we always compute the hash with the file on disk, the special case for encrypted files is no longer needed. This should have no impact on whether db is synchronized. The only case when db--file-hash uses the "calculate in current buffer" path is in db-insert-file; the only use of db-insert-file is in db-update-file, which already calls db--file-hash with the file path, and does not rely on the value from the current buffer. --- org-roam-db.el | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/org-roam-db.el b/org-roam-db.el index 930a29a..8f179d1 100644 --- a/org-roam-db.el +++ b/org-roam-db.el @@ -349,15 +349,16 @@ If there is no title, return the file name relative to (buffer-file-name (buffer-base-buffer)) org-roam-directory))))) -(defun org-roam-db-insert-file () +(defun org-roam-db-insert-file (&optional hash) "Update the files table for the current buffer. -If UPDATE-P is non-nil, first remove the file in the database." +If UPDATE-P is non-nil, first remove the file in the database. +If HASH is non-nil, use that as the file's hash without recalculating it." (let* ((file (buffer-file-name)) (file-title (org-roam-db--file-title)) (attr (file-attributes file)) (atime (file-attribute-access-time attr)) (mtime (file-attribute-modification-time attr)) - (hash (org-roam-db--file-hash))) + (hash (or hash (org-roam-db--file-hash file)))) (org-roam-db-query [:insert :into files :values $v1] @@ -606,19 +607,12 @@ INFO is the org-element parsed buffer." (puthash (car row) (cadr row) ht)) ht)) -(defun org-roam-db--file-hash (&optional file-path) - "Compute the hash of FILE-PATH, a file or current buffer." - ;; If it is a GPG encrypted file, we always want to compute the hash - ;; for the GPG encrypted file (undecrypted) - (when (and (not file-path) (equal "gpg" (file-name-extension (buffer-file-name)))) - (setq file-path (buffer-file-name))) - (if file-path - (with-temp-buffer - (set-buffer-multibyte nil) - (insert-file-contents-literally file-path) - (secure-hash 'sha1 (current-buffer))) - (org-with-wide-buffer - (secure-hash 'sha1 (current-buffer))))) +(defun org-roam-db--file-hash (file-path) + "Compute the hash of FILE-PATH." + (with-temp-buffer + (set-buffer-multibyte nil) + (insert-file-contents-literally file-path) + (secure-hash 'sha1 (current-buffer)))) ;;;; Synchronization (defun org-roam-db-update-file (&optional file-path no-require) @@ -645,7 +639,7 @@ in `org-roam-db-sync'." (org-set-regexps-and-options 'tags-only) (org-refresh-category-properties) (org-roam-db-clear-file) - (org-roam-db-insert-file) + (org-roam-db-insert-file content-hash) (org-roam-db-insert-file-node) (setq org-outline-path-cache nil) (org-roam-db-map-nodes