Skip to content

Commit 171a8db

Browse files
authored
(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 <https://github.com/obsidianmd/obsidian-docs/>.) - 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.
1 parent bbac208 commit 171a8db

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

org-roam-db.el

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,16 @@ If there is no title, return the file name relative to
349349
(buffer-file-name (buffer-base-buffer))
350350
org-roam-directory)))))
351351

352-
(defun org-roam-db-insert-file ()
352+
(defun org-roam-db-insert-file (&optional hash)
353353
"Update the files table for the current buffer.
354-
If UPDATE-P is non-nil, first remove the file in the database."
354+
If UPDATE-P is non-nil, first remove the file in the database.
355+
If HASH is non-nil, use that as the file's hash without recalculating it."
355356
(let* ((file (buffer-file-name))
356357
(file-title (org-roam-db--file-title))
357358
(attr (file-attributes file))
358359
(atime (file-attribute-access-time attr))
359360
(mtime (file-attribute-modification-time attr))
360-
(hash (org-roam-db--file-hash)))
361+
(hash (or hash (org-roam-db--file-hash file))))
361362
(org-roam-db-query
362363
[:insert :into files
363364
:values $v1]
@@ -606,19 +607,12 @@ INFO is the org-element parsed buffer."
606607
(puthash (car row) (cadr row) ht))
607608
ht))
608609

609-
(defun org-roam-db--file-hash (&optional file-path)
610-
"Compute the hash of FILE-PATH, a file or current buffer."
611-
;; If it is a GPG encrypted file, we always want to compute the hash
612-
;; for the GPG encrypted file (undecrypted)
613-
(when (and (not file-path) (equal "gpg" (file-name-extension (buffer-file-name))))
614-
(setq file-path (buffer-file-name)))
615-
(if file-path
616-
(with-temp-buffer
617-
(set-buffer-multibyte nil)
618-
(insert-file-contents-literally file-path)
619-
(secure-hash 'sha1 (current-buffer)))
620-
(org-with-wide-buffer
621-
(secure-hash 'sha1 (current-buffer)))))
610+
(defun org-roam-db--file-hash (file-path)
611+
"Compute the hash of FILE-PATH."
612+
(with-temp-buffer
613+
(set-buffer-multibyte nil)
614+
(insert-file-contents-literally file-path)
615+
(secure-hash 'sha1 (current-buffer))))
622616

623617
;;;; Synchronization
624618
(defun org-roam-db-update-file (&optional file-path no-require)
@@ -645,7 +639,7 @@ in `org-roam-db-sync'."
645639
(org-set-regexps-and-options 'tags-only)
646640
(org-refresh-category-properties)
647641
(org-roam-db-clear-file)
648-
(org-roam-db-insert-file)
642+
(org-roam-db-insert-file content-hash)
649643
(org-roam-db-insert-file-node)
650644
(setq org-outline-path-cache nil)
651645
(org-roam-db-map-nodes

0 commit comments

Comments
 (0)