https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/187379

>From 4a7bb884776889fddc4053bb856f4459a867610c Mon Sep 17 00:00:00 2001
From: Marco Barbone <[email protected]>
Date: Wed, 18 Mar 2026 16:58:11 -0400
Subject: [PATCH 1/2] [clang-format] Fix stale .lock files in git-clang-format
 temp index

git-clang-format uses a fixed path (.git/clang-format-index) for its
temporary index file. When invoked from pre-commit hooks or other
concurrent git operations, the .lock file that git creates alongside
it can persist after abnormal exits, blocking all subsequent commits
with "Unable to create .git/clang-format-index.lock: File exists".

Replace the fixed path with a unique temporary file via
tempfile.mkstemp(), so concurrent invocations don't collide and stale
locks from crashed runs don't block future operations.

Assisted-by: Claude <[email protected]>
---
 clang/tools/clang-format/git-clang-format | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/tools/clang-format/git-clang-format 
b/clang/tools/clang-format/git-clang-format
index dcc0a5347f5d2..ef578467bbc5d 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -31,6 +31,7 @@ import os
 import re
 import subprocess
 import sys
+import tempfile
 
 usage = "git clang-format [OPTIONS] [<commit>] [<commit>|--staged] [--] 
[<file>...]"
 
@@ -707,7 +708,12 @@ def create_temporary_index(tree=None):
     If `tree` is not None, use that as the tree to read in.  Otherwise, an
     empty index is created."""
     gitdir = run("git", "rev-parse", "--git-dir")
-    path = os.path.join(gitdir, temp_index_basename)
+    # Use a unique filename to avoid stale .lock files when concurrent git
+    # operations (e.g. pre-commit hooks) race with this process.
+    fd, path = tempfile.mkstemp(
+        prefix=temp_index_basename + "-", dir=gitdir
+    )
+    os.close(fd)
     if tree is None:
         tree = "--empty"
     run("git", "read-tree", "--index-output=" + path, tree)

>From dfdd2ebf7b6c42e2c403ca1372baf3f9c0f27f64 Mon Sep 17 00:00:00 2001
From: owenca <[email protected]>
Date: Wed, 18 Mar 2026 23:56:19 -0700
Subject: [PATCH 2/2] Format with `black`

---
 clang/tools/clang-format/git-clang-format | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/tools/clang-format/git-clang-format 
b/clang/tools/clang-format/git-clang-format
index ef578467bbc5d..6ae32ea3d9472 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -710,9 +710,7 @@ def create_temporary_index(tree=None):
     gitdir = run("git", "rev-parse", "--git-dir")
     # Use a unique filename to avoid stale .lock files when concurrent git
     # operations (e.g. pre-commit hooks) race with this process.
-    fd, path = tempfile.mkstemp(
-        prefix=temp_index_basename + "-", dir=gitdir
-    )
+    fd, path = tempfile.mkstemp(prefix=temp_index_basename + "-", dir=gitdir)
     os.close(fd)
     if tree is None:
         tree = "--empty"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to