From 5a2afc0968fb76ceeea4ec710d7a86d4d8e9e10c Mon Sep 17 00:00:00 2001 From: Jonas Bernoulli Date: Tue, 27 Aug 2024 22:49:22 +0200 Subject: [PATCH] Make directory detection more robust --- borg.el | 95 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/borg.el b/borg.el index a93533b..3c91ace 100644 --- a/borg.el +++ b/borg.el @@ -96,40 +96,69 @@ ;;; Variables -(defvar borg-drones-directory - (let* ((libdir (file-name-directory (directory-file-name - (file-name-directory - (file-truename - (or load-file-name - buffer-file-name)))))) - (topdir (file-name-directory (directory-file-name libdir)))) - (or (ignore-errors - (let ((default-directory topdir)) +(defvar borg-drones-directory) +(defvar borg-user-emacs-directory) +(defvar borg-top-level-directory) + +(let* ((libdir (file-name-directory + (directory-file-name + (file-name-directory + (file-truename (or load-file-name buffer-file-name)))))) + (topdir (ignore-errors + (let ((default-directory libdir)) + (file-name-as-directory + (car (process-lines + "git" "rev-parse" "--show-toplevel")))))) + (setdir (ignore-errors + (let ((default-directory libdir)) + (file-name-as-directory + (car (process-lines + "git" "config" "borg.drones-directory"))))))) + + (defconst borg-drones-directory + (or (and-let* ((dir (and topdir setdir (expand-file-name setdir topdir)))) + (if (file-directory-p dir) + dir + (message "Ignoring borg.drones-directory=%S; does not exist" dir) + nil)) + (and-let* ((dir (and topdir + (file-equal-p libdir + (bound-and-true-p package-user-dir)) + (expand-file-name (file-name-as-directory "borg") + topdir)))) + (and (file-directory-p dir) dir)) + (and (file-directory-p libdir) libdir) + (error "Could not determine `borg-drones-directory'")) + "Directory beneath which drone submodules are placed. +This is automatically set based on the location of the `borg' library. +The Git variable `borg.drones-directory' can be used to override the +default value.") + + (defconst borg-user-emacs-directory + (or (and (equal topdir (file-name-directory + (directory-file-name borg-drones-directory))) + topdir) + (locate-dominating-file borg-drones-directory "init.el") + (and (file-in-directory-p libdir user-emacs-directory) + user-emacs-directory) + (error "Could not determine `borg-user-emacs-directory'")) + "Directory beneath which additional per-user Emacs-specific files are placed. +The same as `user-emacs-directory', except if Emacs was started with +something like \"emacs -q -l /path/to/init.el\". When using Emacs 29.1 +or later, instead use \"emacs --init-directory=/path/to\", ensuring the +values of these variables are the same, even though a non-standard init +directory is used.") + + (defconst borg-top-level-directory + (or topdir + (ignore-errors + (let ((default-directory borg-user-emacs-directory)) (file-name-as-directory - (expand-file-name - (car (process-lines "git" "config" "borg.drones-directory")))))) - (if (ignore-errors - (file-equal-p libdir (bound-and-true-p package-user-dir))) - (expand-file-name (file-name-as-directory "borg") topdir) - libdir))) - "Directory beneath which drone submodules are placed. -If you need to change this, then do so before loading `borg'.") - -(defconst borg-user-emacs-directory - (file-name-directory (directory-file-name borg-drones-directory)) - "Directory beneath which additional per-user Emacs-specific files are placed. - -The value of this variable is usually the same as that of -`user-emacs-directory', except when Emacs is started with -`emacs -q -l /path/to/init.el'.") - -(defconst borg-top-level-directory - (or (ignore-errors - (let ((default-directory borg-user-emacs-directory)) - (file-name-as-directory - (car (process-lines "git" "rev-parse" "--show-toplevel"))))) - borg-user-emacs-directory) - "The top-level of repository containing `borg-user-emacs-directory'.") + (car (process-lines + "git" "rev-parse" "--show-toplevel"))))) + (error "Could not determine `borg-top-level-directory'")) + "The top-level of repository containing `borg-user-emacs-directory'.") + ) (defconst borg-gitmodules-file (expand-file-name ".gitmodules" borg-top-level-directory)