From 5755d36c03ef217064df62fd6da4b14ee070f513 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Sat, 1 Aug 2020 00:46:42 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20issue=20with=20having=20=E2=80=98[?= =?UTF-8?q?=E2=80=99=20in=20the=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Having a ‘[’ messed up the regular expression used because it wasn’t being properly escaped. --- git-auto-commit-mode.el | 13 ++-- tests/git-auto-commit-mode-tests.el | 99 +++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/git-auto-commit-mode.el b/git-auto-commit-mode.el index d5f8afc..00ef4fe 100644 --- a/git-auto-commit-mode.el +++ b/git-auto-commit-mode.el @@ -36,6 +36,8 @@ ;;; Code: +(require 'subr-x) + (defgroup git-auto-commit-mode nil "Customization options for `git-auto-commit-mode'." :group 'external) @@ -118,14 +120,9 @@ It can be: (defun gac-relative-file-name (filename) "Find the path to FILENAME relative to the git directory." (let* ((git-dir - (replace-regexp-in-string - "\n+$" "" (shell-command-to-string - "git rev-parse --show-toplevel"))) - (relative-file-name - (replace-regexp-in-string - "^/" "" (replace-regexp-in-string - git-dir "" filename)))) - relative-file-name)) + (string-trim-right + (shell-command-to-string "git rev-parse --show-toplevel")))) + (file-relative-name filename git-dir))) (defun gac-password (proc string) "Ask the user for a password when necessary. diff --git a/tests/git-auto-commit-mode-tests.el b/tests/git-auto-commit-mode-tests.el index fd40dce..2b0b959 100644 --- a/tests/git-auto-commit-mode-tests.el +++ b/tests/git-auto-commit-mode-tests.el @@ -64,5 +64,104 @@ :not :to-match (rx string-start "test" string-end))) (delete-directory temp-dir t)))))) +(describe "Getting a relative path" + (it "should return the file name for files in the project root" + (let* ((temp-dir (make-temp-file "gac-" t)) + (temp-file (expand-file-name "test" temp-dir)) + (default-directory temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "test")) + (delete-directory temp-dir t)))) + + (it "should return the file name and directory name for nested files" + (let* ((temp-dir (make-temp-file "gac-" t)) + (temp-file (expand-file-name "test/test.txt" temp-dir)) + (default-directory temp-dir)) + (mkdir (expand-file-name "test" temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "test/test.txt")) + (delete-directory temp-dir t)))) + + (it "should handle ‘[]’ in the filename" + (let* ((temp-dir (make-temp-file "gac-" t)) + (temp-file (expand-file-name "[test]-test.txt" temp-dir)) + (default-directory temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "[test]-test.txt")) + (delete-directory temp-dir t)))) + + (it "should handle ‘[]’ in the directory name" + (let* ((temp-dir (make-temp-file "gac-" t)) + (temp-file (expand-file-name "[test]-test/testing.txt" temp-dir)) + (default-directory temp-dir)) + (mkdir (expand-file-name "[test]-test" temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "[test]-test/testing.txt")) + (delete-directory temp-dir t)))) + + (it "should handle ‘[’ in the directory name" + (let* ((temp-dir (make-temp-file "gac-[test-" t)) + (temp-file (expand-file-name "testing.txt" temp-dir)) + (default-directory temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "testing.txt")) + (delete-directory temp-dir t)))) + + (it "should handle ‘[’ in the file name" + (let* ((temp-dir (make-temp-file "gac-" t)) + (temp-file (expand-file-name "[test-test.txt" temp-dir)) + (default-directory temp-dir)) + (unwind-protect + (progn + (shell-command "git init") + (let ((buffer (find-file-noselect temp-file))) + (with-current-buffer buffer + (git-auto-commit-mode) + (insert "test") + (save-buffer))) + (expect (gac-relative-file-name temp-file) + :to-equal "[test-test.txt")) + (delete-directory temp-dir t))))) + (provide 'git-auto-commit-mode-tests) ;;; git-auto-commit-mode-tests.el ends here