[llvm] [libcxx] Add pre-commit hooks and git config for libc++ & git-clang-format. (PR #73798)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 09:08:06 PST 2023


https://github.com/EricWF updated https://github.com/llvm/llvm-project/pull/73798

>From e02dd1680546fa5f9ad70446f9a9a5b3eb431f5b Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 09:38:13 -0500
Subject: [PATCH 01/11] Add pre-commit hooks and git config for libc++ &
 git-clang-format.

This patch aims to ease the entry into libc++'s codebase by

(A) Setting up the users clangFormat.extensions values in the
.git/config. libc++ needs this to format extensionless files.

(2) Install a pre-commit hook which checks for files which are
   misformatted and offers to format them.

These can be installed by running
./libcxx/utils/formatting/setup-formatting-config.sh

Documentation is incoming
---
 .../libcxx-pre-commit-formatting-hook.sh      | 31 ++++++++
 .../formatting/setup-formatting-config.sh     | 75 +++++++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100755 libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh
 create mode 100755 libcxx/utils/formatting/setup-formatting-config.sh

diff --git a/libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh b/libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh
new file mode 100755
index 000000000000000..741b686e5e5a402
--- /dev/null
+++ b/libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+
+# Define the staged files
+staged_files=$(git diff --cached --name-only --diff-filter=d -- libcxx/ libcxxabi/ libunwind/)
+
+# Check if any of the staged files have not been properly formatted with git-clang-format
+
+echo "Checking $file..."
+formatting_diff=$(git-clang-format --diff -- "$staged_files") # quotes are used here
+if [[ "$formatting_diff" != "no modified files to format" && "$formatting_diff" != "clang-format did not modify any files" ]]; then
+    echo "$file has not been formatted with git-clang-format."
+    git-clang-format --diff -- "$staged_files"
+    read -p "Format the files [Y/n]? " -n 1 -r
+      echo
+    if [[ $REPLY =~ ^[Yy]$ ]]
+      then
+          git-clang-format -- "$staged_files"
+          git add "$staged_files"
+          exit 0
+    else
+          echo "No changes were made to the git config."
+          exit 1
+    fi
+
+fi
+
+
+# Everything checks out
+echo "All staged files have been formatted with git-clang-format."
+exit 0
diff --git a/libcxx/utils/formatting/setup-formatting-config.sh b/libcxx/utils/formatting/setup-formatting-config.sh
new file mode 100755
index 000000000000000..9332ee9e9adebac
--- /dev/null
+++ b/libcxx/utils/formatting/setup-formatting-config.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+
+# Jump up to the root directory of the repository
+
+git_repo_path="$(git rev-parse --show-toplevel)"
+cd "$git_repo_path"
+
+# Set up git-clang-format config so that it formats files without extensions as needed by libc++
+function check_formatting_config() {
+  # Set the desired git config key and value
+  CONFIG_KEY="clangFormat.extensions"
+  CONFIG_VALUE=" c,h,m,mm,cpp,cxx,hpp,,"
+
+  # Get the actual value of the config key
+  ACTUAL_VALUE=$(git config --local --get $CONFIG_KEY)
+
+  # Check if the actual value is the same as the desired value
+  if [[ "$ACTUAL_VALUE" == "$CONFIG_VALUE" ]]; then
+      echo "The git config value for $CONFIG_KEY is correctly set to $CONFIG_VALUE"
+  else
+      echo "Setting up git-clang-format config for libc++..."
+      # Prompt the user to set the git config key to the desired value
+      echo "Git config key $CONFIG_KEY is not set or incorrect."
+      read -p "Would you like to set it to $CONFIG_VALUE [Y/n]? " -n 1 -r
+      echo
+      if [[ $REPLY =~ ^[Yy]$ ]]
+      then
+          git config --local $CONFIG_KEY "$CONFIG_VALUE"
+          echo "Git config key $CONFIG_KEY has been set to $CONFIG_VALUE"
+      else
+          echo "No changes were made to the git config."
+      fi
+  fi
+}
+
+# Check for an installation of git-clang-format
+function check_clang_format() {
+  # Check if git-clang-format is installed
+  GIT_CLANG_FORMAT_COMMAND="git-clang-format"
+  if command -v $GIT_CLANG_FORMAT_COMMAND >/dev/null 2>&1; then
+      echo "git-clang-format is installed in your system."
+  else
+      echo "Warning: git-clang-format is not installed in your system."
+  fi
+}
+# ...
+# Existing script here
+# ...
+
+# Check if libcxx-formatting.sh is installed in pre-commit hook
+function check_pre_commit_hooks() {
+  # Check if libcxx-formatting.sh is present in pre-commit hook
+  PRE_COMMIT_FILE=".git/hooks/pre-commit"
+  EXPECTED_COMMIT_SCRIPT=". ./libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh"
+  if grep -q -F "$EXPECTED_COMMIT_SCRIPT" "$PRE_COMMIT_FILE"; then
+      echo "libcxx-pre-commit-formatting-hook.sh is already installed in pre-commit hook."
+  else
+      # Offer to install it
+      read -p "libcxx-pre-commit-formatting-hook.sh is not installed. Would you like to install it [Y/n]? " -n 1 -r
+      echo
+      if [[ $REPLY =~ ^[Yy]$ ]]
+      then
+          echo "Installing libcxx-pre-commit-formatting-hook.sh..."
+          echo "$EXPECTED_COMMIT_SCRIPT" >> "$PRE_COMMIT_FILE"
+          echo "Installed libcxx-pre-commit-formatting-hook.sh to pre-commit hook."
+      else
+          echo "No changes were made to the pre-commit hook."
+      fi
+  fi
+}
+
+check_formatting_config
+check_clang_format
+check_pre_commit_hooks

>From b50ac103f449eb570460754893122dd0c6296204 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 10:30:34 -0500
Subject: [PATCH 02/11] Attempt to use the new check on the bots.

---
 .../libcxx-check-generated-files.yml          | 21 +++++++++----
 ...-config.sh => install-formatting-hooks.sh} | 31 +++++++++++++------
 ...-hook.sh => pre-commit-formatting-hook.sh} | 30 +++++++++++++-----
 3 files changed, 59 insertions(+), 23 deletions(-)
 rename libcxx/utils/formatting/{setup-formatting-config.sh => install-formatting-hooks.sh} (68%)
 rename libcxx/utils/formatting/{libcxx-pre-commit-formatting-hook.sh => pre-commit-formatting-hook.sh} (55%)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index 570055624b2a8d4..f6e9b8e04c89c00 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -9,16 +9,25 @@ permissions:
 
 jobs:
   check_generated_files:
-    runs-on: ubuntu-latest
+    runs-on: [ libcxx-runners-1 ]
     steps:
       - name: Fetch LLVM sources
         uses: actions/checkout at v4
-
-      - name: Install dependencies
-        uses: aminya/setup-cpp at v1
         with:
-          clangformat: 17.0.1
-          ninja: true
+          fetch-depth: 0
+      - name: Install Precommit Hooks
+        run: bash ./libcxx/utils/formatting/install-formatting-hooks.sh --force
+
+      - name: Setup the paths to check for formatting
+        run: git config --local llvmPrecommit.formatPaths "libcxx"
+
+      # Reset the git branch to have all of the modified files staged but not committed.
+      - name: Reset git state
+        run: git reset --soft ${{ github.base_ref }}
+
+      - name: Check generated files
+        run: bash ./libcxx/utils/formatting/pre-commit-formatting-hook.sh
+
 
       - name: Check generated files
         run: libcxx/utils/ci/run-buildbot check-generated-output
diff --git a/libcxx/utils/formatting/setup-formatting-config.sh b/libcxx/utils/formatting/install-formatting-hooks.sh
similarity index 68%
rename from libcxx/utils/formatting/setup-formatting-config.sh
rename to libcxx/utils/formatting/install-formatting-hooks.sh
index 9332ee9e9adebac..5880e7bff8ab537 100755
--- a/libcxx/utils/formatting/setup-formatting-config.sh
+++ b/libcxx/utils/formatting/install-formatting-hooks.sh
@@ -2,6 +2,14 @@
 
 
 # Jump up to the root directory of the repository
+FORCE=${LLVM_FORCE_FORMATTING:-0}
+
+for arg in "$@"
+do
+  if [ "$arg" = "--force" ]; then
+    FORCE=1
+  fi
+done
 
 git_repo_path="$(git rev-parse --show-toplevel)"
 cd "$git_repo_path"
@@ -9,8 +17,9 @@ cd "$git_repo_path"
 # Set up git-clang-format config so that it formats files without extensions as needed by libc++
 function check_formatting_config() {
   # Set the desired git config key and value
-  CONFIG_KEY="clangFormat.extensions"
-  CONFIG_VALUE=" c,h,m,mm,cpp,cxx,hpp,,"
+  CONFIG_KEY=$1
+  CONFIG_VALUE=$2
+  ALLOW_DIFFERENT=$3
 
   # Get the actual value of the config key
   ACTUAL_VALUE=$(git config --local --get $CONFIG_KEY)
@@ -18,11 +27,13 @@ function check_formatting_config() {
   # Check if the actual value is the same as the desired value
   if [[ "$ACTUAL_VALUE" == "$CONFIG_VALUE" ]]; then
       echo "The git config value for $CONFIG_KEY is correctly set to $CONFIG_VALUE"
+  elif [[ "$ACTION_VALUE" != "" ]] && [[ "$ALLOW_DIFFERENT" -eq 1 ]]; then
+    echo "The git config value for $CONFIG_KEY is set to $ACTUAL_VALUE, but $CONFIG_VALUE is allowed."
   else
       echo "Setting up git-clang-format config for libc++..."
       # Prompt the user to set the git config key to the desired value
       echo "Git config key $CONFIG_KEY is not set or incorrect."
-      read -p "Would you like to set it to $CONFIG_VALUE [Y/n]? " -n 1 -r
+      read -p "Would you like to set it to $CONFIG_VALUE [Y/n]? " -n $FORCE -r
       echo
       if [[ $REPLY =~ ^[Yy]$ ]]
       then
@@ -52,24 +63,26 @@ function check_clang_format() {
 function check_pre_commit_hooks() {
   # Check if libcxx-formatting.sh is present in pre-commit hook
   PRE_COMMIT_FILE=".git/hooks/pre-commit"
-  EXPECTED_COMMIT_SCRIPT=". ./libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh"
+  EXPECTED_COMMIT_SCRIPT=". ./libcxx/utils/formatting/pre-commit-formatting-hook.sh"
   if grep -q -F "$EXPECTED_COMMIT_SCRIPT" "$PRE_COMMIT_FILE"; then
-      echo "libcxx-pre-commit-formatting-hook.sh is already installed in pre-commit hook."
+      echo "pre-commit-formatting-hook.sh is already installed in pre-commit hook."
   else
       # Offer to install it
-      read -p "libcxx-pre-commit-formatting-hook.sh is not installed. Would you like to install it [Y/n]? " -n 1 -r
+      read -p "pre-commit-formatting-hook.sh is not installed. Would you like to install it [Y/n]? " -n $FORCE -r
       echo
       if [[ $REPLY =~ ^[Yy]$ ]]
       then
-          echo "Installing libcxx-pre-commit-formatting-hook.sh..."
+          echo "Installing pre-commit-formatting-hook.sh..."
           echo "$EXPECTED_COMMIT_SCRIPT" >> "$PRE_COMMIT_FILE"
-          echo "Installed libcxx-pre-commit-formatting-hook.sh to pre-commit hook."
+          chmod +x "$PRE_COMMIT_FILE"
+          echo "Installed pre-commit-formatting-hook.sh to pre-commit hook."
       else
           echo "No changes were made to the pre-commit hook."
       fi
   fi
 }
 
-check_formatting_config
+check_formatting_config "clangFormat.extensions" "c,h,m,mm,cpp,cxx,hpp,," 0
+check_formatting_config "llvmPrecommit.formatPaths" "libcxx libcxxabi libunwind" 1
 check_clang_format
 check_pre_commit_hooks
diff --git a/libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh b/libcxx/utils/formatting/pre-commit-formatting-hook.sh
similarity index 55%
rename from libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh
rename to libcxx/utils/formatting/pre-commit-formatting-hook.sh
index 741b686e5e5a402..fb04a7ec318c73e 100755
--- a/libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh
+++ b/libcxx/utils/formatting/pre-commit-formatting-hook.sh
@@ -1,28 +1,42 @@
 #!/bin/bash
+set -x
+set -e
+FORCE=${LLVM_FORCE_FORMATTING:-0}
+VERBOSE=${VERBOSE:-0}
+
+for arg in "$@"
+do
+  if [ "$arg" = "--force" ]; then
+    FORCE=1
+  fi
+done
+
+FORMAT_PATHS=$(git config --get llvmPrecommit.formatPaths || echo ".")
+
+echo "FORMAT_PATHS is set to: $FORMAT_PATHS"
 
 
 # Define the staged files
-staged_files=$(git diff --cached --name-only --diff-filter=d -- libcxx/ libcxxabi/ libunwind/)
+staged_files=$(git diff --cached --name-only --diff-filter=d -- $FORMAT_PATHS)
 
 # Check if any of the staged files have not been properly formatted with git-clang-format
 
 echo "Checking $file..."
-formatting_diff=$(git-clang-format --diff -- "$staged_files") # quotes are used here
+formatting_diff=$(git-clang-format --diff -- $staged_files) # quotes are used here
 if [[ "$formatting_diff" != "no modified files to format" && "$formatting_diff" != "clang-format did not modify any files" ]]; then
     echo "$file has not been formatted with git-clang-format."
-    git-clang-format --diff -- "$staged_files"
-    read -p "Format the files [Y/n]? " -n 1 -r
+    git-clang-format --diff -- $staged_files
+    read -p "Format the files [Y/n]? " -n $FORCE -r
       echo
     if [[ $REPLY =~ ^[Yy]$ ]]
-      then
-          git-clang-format -- "$staged_files"
-          git add "$staged_files"
+    then
+          git-clang-format -- $staged_files
+          git add $staged_files
           exit 0
     else
           echo "No changes were made to the git config."
           exit 1
     fi
-
 fi
 
 

>From 8d0330002c17718933f70551df921682046f33ba Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 10:59:49 -0500
Subject: [PATCH 03/11] fix group name

---
 .github/workflows/libcxx-check-generated-files.yml | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index f6e9b8e04c89c00..96da89855fc4f5e 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -9,7 +9,8 @@ permissions:
 
 jobs:
   check_generated_files:
-    runs-on: [ libcxx-runners-1 ]
+    runs-on:
+      group: libcxx-runners-1
     steps:
       - name: Fetch LLVM sources
         uses: actions/checkout at v4

>From a5f5926891f71e03225e06614fdbfaaec6386a20 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:09:24 -0500
Subject: [PATCH 04/11] attempt to fix slow checkout

---
 .github/workflows/libcxx-check-generated-files.yml | 2 --
 1 file changed, 2 deletions(-)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index 96da89855fc4f5e..f9a5f4f45c581af 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -14,8 +14,6 @@ jobs:
     steps:
       - name: Fetch LLVM sources
         uses: actions/checkout at v4
-        with:
-          fetch-depth: 0
       - name: Install Precommit Hooks
         run: bash ./libcxx/utils/formatting/install-formatting-hooks.sh --force
 

>From 4e536d3a413ec2e30ac58eb7f10807dc378a62f6 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:10:29 -0500
Subject: [PATCH 05/11] attempt to fix other issue

---
 .github/workflows/libcxx-check-generated-files.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index f9a5f4f45c581af..01d0f28e062a881 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -22,7 +22,7 @@ jobs:
 
       # Reset the git branch to have all of the modified files staged but not committed.
       - name: Reset git state
-        run: git reset --soft ${{ github.base_ref }}
+        run: git reset --soft ${{ github.pull_request.base.sha }}
 
       - name: Check generated files
         run: bash ./libcxx/utils/formatting/pre-commit-formatting-hook.sh

>From 486dd9230404c16d3cb5b0d4f5ac10c08aa32656 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:14:19 -0500
Subject: [PATCH 06/11] I think I've figured out the secret sauce

[skip ci]
---
 .github/workflows/libcxx-check-generated-files.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index 01d0f28e062a881..8fe84d32db58d99 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -22,7 +22,7 @@ jobs:
 
       # Reset the git branch to have all of the modified files staged but not committed.
       - name: Reset git state
-        run: git reset --soft ${{ github.pull_request.base.sha }}
+        run: git reset --soft origin/${{ github.base_ref }}
 
       - name: Check generated files
         run: bash ./libcxx/utils/formatting/pre-commit-formatting-hook.sh

>From 583f6be715c00da04679009a18081592a0992e48 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:29:04 -0500
Subject: [PATCH 07/11] Add packages to docker file

---
 libcxx/utils/ci/Dockerfile | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libcxx/utils/ci/Dockerfile b/libcxx/utils/ci/Dockerfile
index 5510e4d6201b1f5..7f7851b870c8f00 100644
--- a/libcxx/utils/ci/Dockerfile
+++ b/libcxx/utils/ci/Dockerfile
@@ -94,6 +94,7 @@ RUN sudo apt-get update \
         wget \
         unzip \
         software-properties-common \
+        jq \
     && sudo rm -rf /var/lib/apt/lists/*
 
 
@@ -132,6 +133,10 @@ RUN <<EOF
   sudo /tmp/llvm.sh $(($LLVM_HEAD_VERSION - 1)) all  # latest release
   sudo /tmp/llvm.sh $LLVM_HEAD_VERSION          all  # current ToT
   sudo apt-get install -y libomp5-$LLVM_HEAD_VERSION
+
+  # Install the latest git-clang-format
+  sudo ln -s $(which git-clang-format-$LLVM_HEAD_VERSION) /usr/bin/clang-format
+
   sudo rm -rf /var/lib/apt/lists/*
 EOF
 

>From 4a1a5d85c776ebf529eecfa5e422a9e4abec3b32 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:49:27 -0500
Subject: [PATCH 08/11] try other checkout attempt

---
 .../workflows/libcxx-check-generated-files.yml   | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index 8fe84d32db58d99..6acfd0a9286e089 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -14,6 +14,22 @@ jobs:
     steps:
       - name: Fetch LLVM sources
         uses: actions/checkout at v4
+        with:
+          ref: ${{ github.event.pull_request.head.sha }}
+
+      - name: Checkout through merge base
+        uses: rmacklin/fetch-through-merge-base at v0
+        with:
+          base_ref: ${{ github.event.pull_request.base.ref }}
+          head_ref: ${{ github.event.pull_request.head.sha }}
+          deepen_length: 500
+
+      - name: Get changed files
+        id: changed-files
+        uses: tj-actions/changed-files at v39
+        with:
+          separator: ","
+          skip_initial_fetch: true
       - name: Install Precommit Hooks
         run: bash ./libcxx/utils/formatting/install-formatting-hooks.sh --force
 

>From bc691600f4dcccd62c135e57b71dad5d020a77d7 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:56:07 -0500
Subject: [PATCH 09/11] try again

---
 .github/workflows/libcxx-check-generated-files.yml | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/libcxx-check-generated-files.yml b/.github/workflows/libcxx-check-generated-files.yml
index 6acfd0a9286e089..fa9a97e0eb06947 100644
--- a/.github/workflows/libcxx-check-generated-files.yml
+++ b/.github/workflows/libcxx-check-generated-files.yml
@@ -24,12 +24,7 @@ jobs:
           head_ref: ${{ github.event.pull_request.head.sha }}
           deepen_length: 500
 
-      - name: Get changed files
-        id: changed-files
-        uses: tj-actions/changed-files at v39
-        with:
-          separator: ","
-          skip_initial_fetch: true
+
       - name: Install Precommit Hooks
         run: bash ./libcxx/utils/formatting/install-formatting-hooks.sh --force
 
@@ -38,7 +33,7 @@ jobs:
 
       # Reset the git branch to have all of the modified files staged but not committed.
       - name: Reset git state
-        run: git reset --soft origin/${{ github.base_ref }}
+        run: git reset --soft ${{ github.event.pull_request.base.ref }}
 
       - name: Check generated files
         run: bash ./libcxx/utils/formatting/pre-commit-formatting-hook.sh

>From 6773c98fa43acf3c4dd07aef4dfbf099ae02d8bf Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 11:57:15 -0500
Subject: [PATCH 10/11] try again

---
 libcxx/utils/formatting/pre-commit-formatting-hook.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/utils/formatting/pre-commit-formatting-hook.sh b/libcxx/utils/formatting/pre-commit-formatting-hook.sh
index fb04a7ec318c73e..7d9d00e86348eee 100755
--- a/libcxx/utils/formatting/pre-commit-formatting-hook.sh
+++ b/libcxx/utils/formatting/pre-commit-formatting-hook.sh
@@ -28,8 +28,7 @@ if [[ "$formatting_diff" != "no modified files to format" && "$formatting_diff"
     git-clang-format --diff -- $staged_files
     read -p "Format the files [Y/n]? " -n $FORCE -r
       echo
-    if [[ $REPLY =~ ^[Yy]$ ]]
-    then
+    if [[ $REPLY =~ ^[Yy]$ ]]; then
           git-clang-format -- $staged_files
           git add $staged_files
           exit 0

>From 9a9b191e3b1f5b6e8ca048fb6e885dca3a85ddb4 Mon Sep 17 00:00:00 2001
From: eric <eric at efcs.ca>
Date: Wed, 29 Nov 2023 12:07:40 -0500
Subject: [PATCH 11/11] add documentation

---
 libcxx/docs/Contributing.rst | 41 ++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/libcxx/docs/Contributing.rst b/libcxx/docs/Contributing.rst
index ae883841851698a..1c18cec3ea927ce 100644
--- a/libcxx/docs/Contributing.rst
+++ b/libcxx/docs/Contributing.rst
@@ -85,6 +85,47 @@ Other tips are:
   self-contained sub-tasks.
 
 
+Setting Up and Formatting for Contributions to libc++
+====================================================
+
+To ensure code consistency and avoid common formatting issues, it's recommended to use provided scripts to setup and
+format your libc++ changes. You can use ``install-formatting-hooks.sh`` and ``pre-commit-formatting-hook.sh`` scripts
+provided in the ``libcxx/utils/formatting/`` directory. Here is the guide to use these scripts:
+
+Setting Up the Formatting Hooks
+-------------------------------
+
+Run the ``install-formatting-hooks.sh`` script. This will check if ``git-clang-format`` is installed in your system,
+setup configuration for git-clang-format to correctly format files in libc++, and install ``pre-commit-formatting-hook.sh``
+in your pre-commit hook. Run using the following command:
+
+.. code-block:: bash
+
+   ./libcxx/utils/formatting/install-formatting-hooks.sh
+
+You have the following options:
+
+- ``--force``: automatically say 'yes' to prompts
+- Simply pressing ``ENTER`` or ``Y`` when prompted will set the necessary git config keys and install the pre-commit hooks.
+
+Using Pre-commit Formatting Hook
+--------------------------------
+
+Once the pre-commit formatting hook is installed, it will automatically run before every commit you make in your local
+development environment, checking if all staged files have been formatted with git-clang-format.
+
+If any of your staged files have not been correctly formatted, the hook will show a diff of your current staged files
+and the formatted version. After showing the diff, it will ask you whether it should format the files for you. Simply
+pressing ``ENTER`` or ``Y`` will format the files, and the hook will automatically stage the formatted changes.
+
+If there is any issue in formatting, or if you choose not to format the files when prompted, the hook will stop the
+commit from being made. You must reformat the files manually or choose to format them when prompted by the hook, if
+this happens.
+
+By using these two scripts, you can ensure that your changes to libc++ are always in the proper format, thereby
+reducing the chances of minor formatting-related revisions during the pull request review process.
+
+
 Resources
 =========
 



More information about the llvm-commits mailing list