Subject: bug#21559: 25.0.50; auto-revert-mode breaks git rebase

From: Alexei Khlebnikov <[email-masked]>
Date: Wed, 21 Feb 2018 23:07:17 +0100

Here is version 4 of the patch.

"revert-buffer-in-progress-p" flag is set in "auto-revert-handler",
according to Michael's idea and then checked in "vc-git-command", according
to Dmitry's idea.

I have tested the patch, it works.
From d6cffe9f7cb0c7827eb47b13b922291418ea8ebf Mon Sep 17 00:00:00 2001
From: Alexei Khlebnikov <[email-masked]>
Date: Wed, 21 Feb 2018 22:34:22 +0100
Subject: [PATCH v4] Fix for: "25.0.50; auto-revert-mode breaks git rebase"
 (Bug#21559)

* lisp/autorevert.el (auto-revert-handler):
  Set "revert-buffer-in-progress-p" flag
  before calling "vc-refresh-state".

* lisp/vc/vc-git.el (vc-git-command):
  If "revert-buffer-in-progress-p" flag is set,
  prepend "GIT_OPTIONAL_LOCKS=0" to "process-environment".
---
 lisp/autorevert.el | 3 ++-
 lisp/vc/vc-git.el  | 9 ++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index cf145e0ee3..0a9d3bef54 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -709,7 +709,8 @@ auto-revert-handler
     ;; `preserve-modes' avoids changing the (minor) modes.  But we do
     ;; want to reset the mode for VC, so we do it manually.
     (when (or revert auto-revert-check-vc-info)
-      (vc-refresh-state))))
+      (let ((revert-buffer-in-progress-p t))
+        (vc-refresh-state)))))
 
 (defun auto-revert-tail-handler (size)
   (let ((modified (buffer-modified-p))
diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 47172dd52f..b1a57ab6f4 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -1566,7 +1566,14 @@ vc-git-command
          (or coding-system-for-read vc-git-log-output-coding-system))
 	(coding-system-for-write
          (or coding-system-for-write vc-git-commits-coding-system))
-        (process-environment (cons "GIT_DIR" process-environment)))
+        (process-environment
+         (append
+          `("GIT_DIR"
+            ;; Avoid repository locking during background operations
+            ;; (bug#21559).
+            ,@(when revert-buffer-in-progress-p
+                '("GIT_OPTIONAL_LOCKS=0")))
+          process-environment)))
     (apply 'vc-do-command (or buffer "*vc*") okstatus vc-git-program
 	   ;; https://debbugs.gnu.org/16897
 	   (unless (and (not (cdr-safe file-or-list))
-- 
2.15.1