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

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 29 06:42:44 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Eric (EricWF)

<details>
<summary>Changes</summary>

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


---
Full diff: https://github.com/llvm/llvm-project/pull/73798.diff


2 Files Affected:

- (added) libcxx/utils/formatting/libcxx-pre-commit-formatting-hook.sh (+31) 
- (added) libcxx/utils/formatting/setup-formatting-config.sh (+75) 


``````````diff
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

``````````

</details>


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


More information about the libcxx-commits mailing list