branch: elpa/git-commit
commit fbb32ee30fbfbf9dc081187aab9931cfcf55f5d5
Author: Kyle Meyer <k...@kyleam.com>
Commit: Kyle Meyer <k...@kyleam.com>

    magit-insert-am-sequence: Account for already applied patches
    
    magit-insert-am-sequence inserts a section for all patches in
    $GIT_DIR/rebase-apply/ and then displays ORIG_HEAD through HEAD via
    magit-sequence-insert-sequence.  If the 'git am' call fails to apply a
    patch, this leads to repeated sections for successfully applied
    patches because Git leaves the patch files around after successfully
    applying a patch.
    
    Avoid the repeated sections by explicitly inserting a section only for
    $GIT_DIR/rebase-apply/ patches that have not been applied, leaving the
    rest to the magit-sequence-insert-sequence call.
    
    Closes #5024.
---
 docs/RelNotes/4.0.0.org |  4 ++++
 lisp/magit-sequence.el  | 27 +++++++++++++++++----------
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/docs/RelNotes/4.0.0.org b/docs/RelNotes/4.0.0.org
index 3ba3e65032..2af6261f64 100644
--- a/docs/RelNotes/4.0.0.org
+++ b/docs/RelNotes/4.0.0.org
@@ -223,6 +223,10 @@ f9ae2a6306 #4620 magit-blame--make-highlight-overlay: Add 
only to intended line
   mode change and point is on the file or the mode change section.
   #4623
 
+- If applying a patch series with ~git am~ failed, the status buffer
+  incorrectly repeated already applied patches in the list of
+  remaining patches.  #5024
+
 10b5407131 magit-diff-highlight-list: Ensure delayed highlighting takes place
 b32521d543 magit-ediff-read-files: Handle renames in one-file logs
 94aca04dc8 magit-module-section: Use correct keymap
diff --git a/lisp/magit-sequence.el b/lisp/magit-sequence.el
index 2b210a5716..5a8b66b4d3 100644
--- a/lisp/magit-sequence.el
+++ b/lisp/magit-sequence.el
@@ -914,24 +914,31 @@ If no such sequence is in progress, do nothing."
   (when (magit-am-in-progress-p)
     (magit-insert-section (rebase-sequence)
       (magit-insert-heading "Applying patches")
-      (let ((patches (nreverse (magit-rebase-patches)))
-            patch commit)
-        (while patches
+      (let* ((patches (nreverse (magit-rebase-patches)))
+             (dir (expand-file-name "rebase-apply" (magit-gitdir)))
+             (i (string-to-number
+                 (magit-file-line (expand-file-name "last" dir))))
+             (cur (string-to-number
+                   (magit-file-line (expand-file-name "next" dir))))
+             patch commit)
+        (while (and patches (>= i cur))
           (setq patch (pop patches))
           (setq commit (magit-commit-p
                         (cadr (split-string (magit-file-line patch)))))
-          (cond ((and commit patches)
+          (cond ((and commit (= i cur))
                  (magit-sequence-insert-commit
-                  "pick" commit 'magit-sequence-pick))
-                (patches
+                  "stop" commit 'magit-sequence-stop))
+                ((= i cur)
                  (magit-sequence-insert-am-patch
-                  "pick" patch 'magit-sequence-pick))
+                  "stop" patch 'magit-sequence-stop))
                 (commit
-                 (magit-sequence-insert-sequence commit "ORIG_HEAD"))
+                 (magit-sequence-insert-commit
+                  "pick" commit 'magit-sequence-pick))
                 (t
                  (magit-sequence-insert-am-patch
-                  "stop" patch 'magit-sequence-stop)
-                 (magit-sequence-insert-sequence nil "ORIG_HEAD")))))
+                  "pick" patch 'magit-sequence-pick)))
+          (cl-decf i)))
+      (magit-sequence-insert-sequence nil "ORIG_HEAD")
       (insert ?\n))))
 
 (defun magit-sequence-insert-am-patch (type patch face)

Reply via email to