[libcxx-commits] [libcxx] [libc++] Add a merge driver that can apply clang-format (PR #73712)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 1 07:19:03 PST 2023


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/73712

>From b566597b210ce49b8ddb5289f5f4190994646e44 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 23 Nov 2023 14:58:28 -0500
Subject: [PATCH 1/4] [libc++] Add a merge driver that can apply clang-format

In preparation for the moment when we'll clang-format the whole code
base, this patch adds a script that can be used to rebase patches across
clang-format changes mechanically, without requiring manual intervention.

See https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all.
---
 libcxx/utils/clang-format-merge-driver.sh | 38 +++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100755 libcxx/utils/clang-format-merge-driver.sh

diff --git a/libcxx/utils/clang-format-merge-driver.sh b/libcxx/utils/clang-format-merge-driver.sh
new file mode 100755
index 000000000000000..6b1a339720d76a4
--- /dev/null
+++ b/libcxx/utils/clang-format-merge-driver.sh
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+# This script can be installed in .git/config to allow rebasing old patches across
+# libc++'s clang-format of the whole tree. Most contributors should not require that
+# since they don't have many pre-clang-format patches lying around. This script is to
+# make it easier for contributors that do have such patches.
+#
+# The script is installed by running the following from the root of your repository:
+#
+#   $ git config merge.libcxx-reformat.name "Run clang-format when rebasing libc++ patches"
+#   $ git config merge.libcxx-reformat.driver "libcxx/utils/clang-format-merge-driver.sh %O %A %B %P"
+#   $ git config merge.libcxx-reformat.recursive binary
+#
+# This is based on https://github.com/nico/hack/blob/main/notes/auto_git_rebase_across_mechanical_changes.md.
+# Many thanks to Nico Weber for paving the way here.
+
+# Path to the file's contents at the ancestor's version.
+base=$1
+
+# Path to the file's contents at the current version.
+current=$2
+
+# Path to the file's contents at the other branch's version (for nonlinear histories, there might be multiple other branches).
+other=$3
+
+# The path of the file in the repository.
+path=$4
+
+clang-format --style=file --assume-filename=$path < $base > $base.tmp
+mv $base.tmp $base
+
+clang-format --style=file --assume-filename=$path < $current > $current.tmp
+mv $current.tmp $current
+
+clang-format --style=file --assume-filename=$path < $other > $other.tmp
+mv $other.tmp $other
+
+git merge-file $current $base $other

>From 7df1cade82bd825550e3ba5485d3a5375ebd2bef Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 1 Dec 2023 10:09:57 -0500
Subject: [PATCH 2/4] Quote file paths

---
 libcxx/utils/clang-format-merge-driver.sh | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/libcxx/utils/clang-format-merge-driver.sh b/libcxx/utils/clang-format-merge-driver.sh
index 6b1a339720d76a4..f9d7fbfed9aa0c5 100755
--- a/libcxx/utils/clang-format-merge-driver.sh
+++ b/libcxx/utils/clang-format-merge-driver.sh
@@ -15,24 +15,24 @@
 # Many thanks to Nico Weber for paving the way here.
 
 # Path to the file's contents at the ancestor's version.
-base=$1
+base="$1"
 
 # Path to the file's contents at the current version.
-current=$2
+current="$2"
 
 # Path to the file's contents at the other branch's version (for nonlinear histories, there might be multiple other branches).
-other=$3
+other="$3"
 
 # The path of the file in the repository.
-path=$4
+path="$4"
 
-clang-format --style=file --assume-filename=$path < $base > $base.tmp
-mv $base.tmp $base
+clang-format --style=file --assume-filename="$path" < "$base" > "$base.tmp"
+mv "$base.tmp" "$base"
 
-clang-format --style=file --assume-filename=$path < $current > $current.tmp
-mv $current.tmp $current
+clang-format --style=file --assume-filename="$path" < "$current" > "$current.tmp"
+mv "$current.tmp" "$current"
 
-clang-format --style=file --assume-filename=$path < $other > $other.tmp
-mv $other.tmp $other
+clang-format --style=file --assume-filename="$path" < "$other" > "$other.tmp"
+mv "$other.tmp" "$other"
 
-git merge-file $current $base $other
+git merge-file -L "$current" -L "$base" -L "$other" "$current" "$base" "$other"

>From ca931dfceed52d2370246add7dd5c09f1023e06b Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 1 Dec 2023 10:10:28 -0500
Subject: [PATCH 3/4] Remove recursive.binary from merge driver installation

---
 libcxx/utils/clang-format-merge-driver.sh | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/utils/clang-format-merge-driver.sh b/libcxx/utils/clang-format-merge-driver.sh
index f9d7fbfed9aa0c5..43bb6510423d0d7 100755
--- a/libcxx/utils/clang-format-merge-driver.sh
+++ b/libcxx/utils/clang-format-merge-driver.sh
@@ -9,7 +9,6 @@
 #
 #   $ git config merge.libcxx-reformat.name "Run clang-format when rebasing libc++ patches"
 #   $ git config merge.libcxx-reformat.driver "libcxx/utils/clang-format-merge-driver.sh %O %A %B %P"
-#   $ git config merge.libcxx-reformat.recursive binary
 #
 # This is based on https://github.com/nico/hack/blob/main/notes/auto_git_rebase_across_mechanical_changes.md.
 # Many thanks to Nico Weber for paving the way here.

>From a31c883d3de7f619ed08e406f62cd0e0859e01df Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 1 Dec 2023 10:17:15 -0500
Subject: [PATCH 4/4] Fix labels

---
 libcxx/utils/clang-format-merge-driver.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/utils/clang-format-merge-driver.sh b/libcxx/utils/clang-format-merge-driver.sh
index 43bb6510423d0d7..5abb8ee3e4367e9 100755
--- a/libcxx/utils/clang-format-merge-driver.sh
+++ b/libcxx/utils/clang-format-merge-driver.sh
@@ -34,4 +34,4 @@ mv "$current.tmp" "$current"
 clang-format --style=file --assume-filename="$path" < "$other" > "$other.tmp"
 mv "$other.tmp" "$other"
 
-git merge-file -L "$current" -L "$base" -L "$other" "$current" "$base" "$other"
+git merge-file -Lcurrent -Lbase -Lother "$current" "$base" "$other"



More information about the libcxx-commits mailing list