Fix issue with having ‘[’ in the path

Having a ‘[’ messed up the regular expression used because it wasn’t being
properly escaped.
This commit is contained in:
Tom Willemse 2020-08-01 00:46:42 -07:00
parent 14e4def248
commit 5755d36c03
2 changed files with 104 additions and 8 deletions

View file

@ -36,6 +36,8 @@
;;; Code: ;;; Code:
(require 'subr-x)
(defgroup git-auto-commit-mode nil (defgroup git-auto-commit-mode nil
"Customization options for `git-auto-commit-mode'." "Customization options for `git-auto-commit-mode'."
:group 'external) :group 'external)
@ -118,14 +120,9 @@ It can be:
(defun gac-relative-file-name (filename) (defun gac-relative-file-name (filename)
"Find the path to FILENAME relative to the git directory." "Find the path to FILENAME relative to the git directory."
(let* ((git-dir (let* ((git-dir
(replace-regexp-in-string (string-trim-right
"\n+$" "" (shell-command-to-string (shell-command-to-string "git rev-parse --show-toplevel"))))
"git rev-parse --show-toplevel"))) (file-relative-name filename git-dir)))
(relative-file-name
(replace-regexp-in-string
"^/" "" (replace-regexp-in-string
git-dir "" filename))))
relative-file-name))
(defun gac-password (proc string) (defun gac-password (proc string)
"Ask the user for a password when necessary. "Ask the user for a password when necessary.

View file

@ -64,5 +64,104 @@
:not :to-match (rx string-start "test" string-end))) :not :to-match (rx string-start "test" string-end)))
(delete-directory temp-dir t)))))) (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) (provide 'git-auto-commit-mode-tests)
;;; git-auto-commit-mode-tests.el ends here ;;; git-auto-commit-mode-tests.el ends here