Skip to content

Commit

Permalink
[bugfix] Reformat git command output newlines
Browse files Browse the repository at this point in the history
The git stdout newline separator is customizable. Sadly for us, it
seems like it uses a carriage return (CR) separator by default. Emacs
won't treat CR as a newline, it needs a CR;LF.

We can configure git to use CR;LF, that being said, we don't want to
modify the user's git configuration here.

Adding a process filter to rewrite the newline separators on the go.
  • Loading branch information
picnoir committed Nov 27, 2022
1 parent 4866b1d commit 98e76c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions my-repo-pins-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ it'll get deleted before the end of the test."
(should (not (file-exists-p (concat tmpdir "/destination"))))
(delete-directory tmpdir t)
(funcall done)))))
;; Git filter Tests
;;;;;;;;;;;;;;

(ert-deftest my-repo-pins--test-call-git-in-dir-process-filter ()
"Test the ‘my-repo-pins--test-call-git-in-dir-process-filter’ filter."
(should (equal (my-repo-pins--call-git-in-dir-process-filter (format "hello%cworld" 13)) "hello\nworld"))
(should (equal (my-repo-pins--call-git-in-dir-process-filter "hello\nworld") "hello\nworld"))
(should (equal (my-repo-pins--call-git-in-dir-process-filter "hello\rworld\ranother\rline") "hello\nworld\nanother\nline"))
(should (equal (my-repo-pins--call-git-in-dir-process-filter "hello\nworld\nanother\nline") "hello\nworld\nanother\nline"))
)

;; Test Fetchers
;;;;;;;;;;;;;;;;;

Expand Down
17 changes: 17 additions & 0 deletions my-repo-pins.el
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ Errors out if we can't find it."
git-from-bin-path
(user-error "Can't find git. Is my-repo-pins-git-bin correctly set?")))))

(defun my-repo-pins--call-git-in-dir-process-filter (str)
"Filtering the git output for ‘my-repo-pins--call-git-in-dir’ call.
By default, git seems to be terminating its stdout/stderr lines using
the CR; sequence instead of the traditional CR;LF; unix sequence.
This filter tries to detect these isolated CR sequences and convert
them in a CR;LF sequence.
STR being the string we have to process."
(replace-regexp-in-string "\r" "\n" str))

(defun my-repo-pins--call-git-in-dir (dir &optional callback &rest args)
"Call the git binary as pointed by ‘my-repo-pins-git-bin’ in DIR with ARGS.
Expand All @@ -120,6 +132,10 @@ Returns the git PROCESS object."
(let* ((git-buffer (get-buffer-create "*my repo pins git log*"))
(git-window nil)
(current-buffer (current-buffer))
(git-filter (lambda
(proc str)
(with-current-buffer (process-buffer proc)
(insert (my-repo-pins--call-git-in-dir-process-filter str)))))
(git-sentinel (lambda
(process _event)
(let ((exit-code (process-exit-status process)))
Expand All @@ -136,6 +152,7 @@ Returns the git PROCESS object."
:name "my-repo-pins-git-subprocess"
:buffer git-buffer
:command (seq-concatenate 'list `(,(my-repo-pins--git-path)) args)
:filter git-filter
:sentinel git-sentinel)
(set-buffer current-buffer))))

Expand Down

0 comments on commit 98e76c2

Please sign in to comment.