From 9e551d9eb7c82ef71b72a19b4bb0e868a0ce8fec Mon Sep 17 00:00:00 2001 From: Jonas Bernoulli Date: Sat, 31 May 2025 14:44:33 +0200 Subject: magit-apply-patch: Respect buffers context limit Applying the region used to fail, even if the changes withing would apply but some other part of the hunk, outside the region, conflicts with the target. --- lisp/magit-apply.el | 3 ++- lisp/magit-diff.el | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lisp/magit-apply.el b/lisp/magit-apply.el index 3f6f5bd..fcf119f 100644 --- a/lisp/magit-apply.el +++ b/lisp/magit-apply.el @@ -220,6 +220,7 @@ adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 @@\"." (command (if (and command (string-match "^magit-\\([^-]+\\)" command)) (match-string 1 command) "apply")) + (context (magit-diff-get-context)) (ignore-context (magit-diff-ignore-any-space-p))) (unless (magit-diff-context-p) (user-error "Not enough context to apply patch. Increase the context")) @@ -230,7 +231,7 @@ adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 @@\"." (let ((magit-inhibit-refresh t)) (magit-run-git-with-input "apply" args "-p0" - (and ignore-context "-C0") + (if ignore-context "-C0" (format "-C%s" context)) "--ignore-space-change" "-"))) (unless magit-inhibit-refresh (when magit-wip-after-apply-mode diff --git a/lisp/magit-diff.el b/lisp/magit-diff.el index 58bd8c7..3a9faaf 100644 --- a/lisp/magit-diff.el +++ b/lisp/magit-diff.el @@ -1549,6 +1549,14 @@ instead." (setq magit-buffer-diff-args val)) (magit-refresh)) +(defun magit-diff-get-context () + (string-to-number + (or (seq-some (##and (string-match "\\`-U\\([0-9]+\\)?\\'" %) + (match-string 1 %)) + magit-buffer-diff-args) + (magit-get "diff.context") + "3"))) + (defun magit-diff-context-p () (if-let ((arg (seq-find (##string-match "^-U\\([0-9]+\\)$" %) magit-buffer-diff-args))) -- cgit v1.0