aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorJonas Bernoulli <jonas@bernoul.li>2025-05-31 14:44:33 +0200
committerJonas Bernoulli <jonas@bernoul.li>2025-05-31 14:44:33 +0200
commit9e551d9eb7c82ef71b72a19b4bb0e868a0ce8fec (patch)
tree5c531df19bb8b712ef128fdbbe5625905df877f3 /lisp
parent66f3cdaca41a041ff8c3ebcbd97cc9e33843b80c (diff)
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.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/magit-apply.el3
-rw-r--r--lisp/magit-diff.el8
2 files changed, 10 insertions, 1 deletions
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)))