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

Bruno De Fraine via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 6 15:52:52 PST 2024


================
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+# Usage:
+#     If clang-format is not on PATH:
+#         export CLANG_FORMAT_PATH=/path/to/clang-format
+#     Run this script to resolve the formatting conflicts and leave only
+#     the non-formatting conflicts for you to resolve manually.
+
+# Find the .git directory.
+GIT_DIR=$(git rev-parse --git-dir)
+
+# Are we in the midst of a merge? If not, this is the wrong tool.
+if [ ! -e $GIT_DIR/MERGE_HEAD ]; then
+    echo Not doing a merge?
+    exit -1
+fi
+
+# Find the "current" "other" and "base" commits.
+# The commit we are merging into.
+read CURRENT < $GIT_DIR/ORIG_HEAD
+# The commit being merged into CURRENT.
+read OTHER < $GIT_DIR/MERGE_HEAD
+# Where it all started.
+BASE=$(git merge-base $CURRENT $OTHER)
+
+# Set up a place to keep temp files.
+MYTEMP=$(mktemp -d)
+trap 'rm -rf $MYTEMP' EXIT
+
+# 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
+        echo clang-format not found on PATH
+        exit -1
+    fi
+fi
+
+# resolve_one_file will perform formatting-conflict resolution on one file.
+# If any conflicts remain, informs the user, otherwise will git-add the
+# resolved file.
+resolve_one_file() {
+    file=$1
+    echo Resolving "$file"...
+
+    # Get formatted copies of the base, current, and other files.
+    git show "$BASE:$file"    | $CLANG_FORMAT --style=file --assume-filename="$file" > "$MYTEMP/base"
+    git show "$OTHER:$file"   | $CLANG_FORMAT --style=file --assume-filename="$file" > "$MYTEMP/other"
+    git show "$CURRENT:$file" | $CLANG_FORMAT --style=file --assume-filename="$file" > "$MYTEMP/current"
----------------
brunodf-snps wrote:

The technique of figuring out `$BASE`/`$OTHER`/`$CURRENT` commits and using them here is fragile (because likely incomplete) and also unneeded because git already has these 3 versions of the file in its index for an unresolved conflict.

To use the versions from the git index, I can suggest either this (syntax explained [here](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltngtltpathgtemegem0READMEememREADMEem)):
```
git show ":1:$file" | $CLANG_FORMAT ... >"$MYTEMP/base"
git show ":2:$file" | $CLANG_FORMAT ... >"$MYTEMP/current"
git show ":3:$file" | $CLANG_FORMAT ... >"$MYTEMP/other"
```
Or this (explained [here](https://git-scm.com/docs/git-checkout-index#_using_temp_or_stageall)):
```
read -r basetmp currenttmp othertmp rem < <(git checkout-index --stage=all "$file")
$CLANG_FORMAT ... -i "$basetmp"
$CLANG_FORMAT ... -i "$currenttmp"
$CLANG_FORMAT ... -i "$othertmp"
```
In both versions, all the logic for figuring out `$BASE`/`$OTHER`/`$CURRENT` can be dropped. In the second version, git creates suitable temp files, so you don't need `$MYTEMP` (but you would still be responsible to cleanup the temp files).

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


More information about the llvm-commits mailing list