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