From f29a770af346025a959209768467bac495915e23 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Wed, 18 Aug 2021 16:16:32 +0800 Subject: [PATCH] (feat)org-roam-version: output commit hash when available (#1786) Output the commit hash when it's available (e.g. when using straight to install packages). This comes in handy during debugging during error reporting. --- org-roam-utils.el | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/org-roam-utils.el b/org-roam-utils.el index ec1be61..1132d2c 100644 --- a/org-roam-utils.el +++ b/org-roam-utils.el @@ -288,18 +288,45 @@ If VAL is not specified, user is prompted to select a value." "Return `org-roam' version. Interactively, or when MESSAGE is non-nil, show in the echo area." (interactive) - (let* ((version - (with-temp-buffer - (insert-file-contents-literally (locate-library "org-roam.el")) - (goto-char (point-min)) - (save-match-data - (if (re-search-forward "\\(?:;; Version: \\([^z-a]*?$\\)\\)" nil nil) - (substring-no-properties (match-string 1)) - "N/A"))))) + (let* ((toplib (or load-file-name buffer-file-name)) + gitdir topdir version) + (unless (and toplib (equal (file-name-nondirectory toplib) "org-roam-utils.el")) + (setq toplib (locate-library "org-roam-utils.el"))) + (setq toplib (and toplib (org-roam--straight-chase-links toplib))) + (when toplib + (setq topdir (file-name-directory toplib) + gitdir (expand-file-name ".git" topdir))) + (when (file-exists-p gitdir) + (setq version + (let ((default-directory topdir)) + (shell-command-to-string "git describe --tags --dirty --always")))) + (unless version + (setq version (with-temp-buffer + (insert-file-contents-literally (locate-library "org-roam.el")) + (goto-char (point-min)) + (save-match-data + (if (re-search-forward "\\(?:;; Version: \\([^z-a]*?$\\)\\)" nil nil) + (substring-no-properties (match-string 1)) + "N/A"))))) (if (or message (called-interactively-p 'interactive)) (message "%s" version) version))) +(defun org-roam--straight-chase-links (filename) + "Chase links in FILENAME until a name that is not a link. + +This is the same as `file-chase-links', except that it also +handles fake symlinks that are created by the package manager +straight.el on Windows. + +See ." + (when (and (bound-and-true-p straight-symlink-emulation-mode) + (fboundp 'straight-chase-emulated-symlink)) + (when-let ((target (straight-chase-emulated-symlink filename))) + (unless (eq target 'broken) + (setq filename target)))) + (file-chase-links filename)) + ;;;###autoload (defun org-roam-diagnostics () "Collect and print info for `org-roam' issues."