[llvm] [DRAFT][clang-format] helper script for resolving downstream reformat… (PR #80462)

Paul T Robinson via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 2 08:55:57 PST 2024


https://github.com/pogo59 created https://github.com/llvm/llvm-project/pull/80462

… conflicts

>From d0295f0af601bf8eb4596a5ed74ef63a8fee42d9 Mon Sep 17 00:00:00 2001
From: Paul Robinson <paul.robinson at sony.com>
Date: Fri, 2 Feb 2024 08:55:27 -0800
Subject: [PATCH] [DRAFT][clang-format] helper script for resolving downstream
 reformat conflicts

---
 llvm/utils/git/clang-format-merge-resolver.sh | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 llvm/utils/git/clang-format-merge-resolver.sh

diff --git a/llvm/utils/git/clang-format-merge-resolver.sh b/llvm/utils/git/clang-format-merge-resolver.sh
new file mode 100644
index 0000000000000..128e0379a30e4
--- /dev/null
+++ b/llvm/utils/git/clang-format-merge-resolver.sh
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+
+# One-time setup:
+#     git config mergetool.llvm-reformat.cmd 'llvm/utils/git/clang-format-merge-resolver.sh "$BASE" "$LOCAL" "$REMOTE" "$MERGED" '
+#
+# Usage:
+#     If clang-format is not on PATH:
+#         export CLANG_FORMAT_PATH=/path/to/clang-format
+#     After you have merged a reformatting commit and it has conflicts, run:
+#         git mergetool --tool=llvm-reformat
+
+# This script is based on libcxx/utils/clang-format-merge-driver.sh
+# which sadly cannot simply be used.
+
+# Find clang-format the same way code-format-helper.py does.
+if [ -x "$CLANG_FORMAT_PATH" ]; then
+    CLANG_FORMAT=$CLANG_FORMAT_PATH
+else
+    CLANG_FORMAT=`which clang-format`
+    if [ ! $? ]; then
+        exit -1
+    fi
+fi
+
+# 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 -Lcurrent -Lbase -Lother "$current" "$base" "$other"
+STATUS=$?
+if [ $STATUS ]; then
+    mv "$current" "$path"
+fi
+exit $STATUS



More information about the llvm-commits mailing list