branch: elpa/git-commit commit 889ed3111a752de0b478ce071f63d620fd2f26d5 Author: Jonas Bernoulli <jo...@bernoul.li> Commit: Jonas Bernoulli <jo...@bernoul.li>
Allow un-/staging binary files when whitespace is being ignored Closes #4977. --- lisp/magit-apply.el | 43 +++++++++++++++++++++++++++---------------- lisp/magit-base.el | 3 ++- lisp/magit-diff.el | 1 + 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lisp/magit-apply.el b/lisp/magit-apply.el index d47b91e332..8af447ccae 100644 --- a/lisp/magit-apply.el +++ b/lisp/magit-apply.el @@ -262,14 +262,23 @@ adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 @@\"." sections)) (t sections))) -(defun magit-apply--diff-ignores-whitespace-p () - (and (cl-intersection magit-buffer-diff-args - '("--ignore-space-at-eol" - "--ignore-space-change" - "--ignore-all-space" - "--ignore-blank-lines") - :test #'equal) - t)) +(defun magit-apply--ignore-whitespace-p (selection type scope) + "Return t if it is necessary and possible to ignore whitespace. +It is necessary to do so when the diff ignores whitespace changes +and whole files are being applied. It is possible when no binary +files are involved. If it is both necessary and impossible, then +return nil, possibly causing whitespace changes to be applied." + (and (memq type '(unstaged staged)) + (memq scope '(file files list)) + (cl-find-if (lambda (arg) + (member arg '("--ignore-space-at-eol" + "--ignore-space-change" + "--ignore-all-space" + "--ignore-blank-lines"))) + magit-buffer-diff-args) + (not (cl-find-if (lambda (section) + (oref section binary)) + (ensure-list selection))))) ;;;; Stage @@ -279,10 +288,11 @@ With a prefix argument, INTENT, and an untracked file (or files) at point, stage the file but not its content." (interactive "P") (if-let ((s (and (derived-mode-p 'magit-mode) - (magit-apply--get-selection)))) - (pcase (list (magit-diff-type) - (magit-diff-scope) - (magit-apply--diff-ignores-whitespace-p)) + (magit-apply--get-selection))) + (type (magit-diff-type)) + (scope (magit-diff-scope))) + (pcase (list type scope + (magit-apply--ignore-whitespace-p s type scope)) (`(untracked ,_ ,_) (magit-stage-untracked intent)) (`(unstaged region ,_) (magit-apply-region s "--cached")) (`(unstaged hunk ,_) (magit-apply-hunk s "--cached")) @@ -414,10 +424,11 @@ ignored) files." (defun magit-unstage () "Remove the change at point from the staging area." (interactive) - (when-let ((s (magit-apply--get-selection))) - (pcase (list (magit-diff-type) - (magit-diff-scope) - (magit-apply--diff-ignores-whitespace-p)) + (when-let ((s (magit-apply--get-selection)) + (type (magit-diff-type)) + (scope (magit-diff-scope))) + (pcase (list type scope + (magit-apply--ignore-whitespace-p s type scope)) (`(untracked ,_ ,_) (user-error "Cannot unstage untracked changes")) (`(unstaged file ,_) (magit-unstage-intent (list (oref s value)))) (`(unstaged files ,_) (magit-unstage-intent (magit-region-values nil t))) diff --git a/lisp/magit-base.el b/lisp/magit-base.el index 64c0a99805..9d04853adb 100644 --- a/lisp/magit-base.el +++ b/lisp/magit-base.el @@ -483,7 +483,8 @@ and delay of your graphical environment or operating system." (defclass magit-file-section (magit-diff-section) ((keymap :initform 'magit-file-section-map) (source :initform nil) - (header :initform nil))) + (header :initform nil) + (binary :initform nil))) (defclass magit-module-section (magit-file-section) ((keymap :initform 'magit-module-section-map) diff --git a/lisp/magit-diff.el b/lisp/magit-diff.el index 14952123f4..cfedfecd19 100644 --- a/lisp/magit-diff.el +++ b/lisp/magit-diff.el @@ -2408,6 +2408,7 @@ section or a child thereof." (unless (equal orig file) (oset section source orig)) (oset section header header) + (oset section binary binary) (when modes (magit-insert-section (hunk '(chmod)) (insert modes)