|
| 1 | +#!/usr/bin/env zsh |
| 2 | +#===--- git-clang-format-all.sh --------------------------------------------===# |
| 3 | +# |
| 4 | +# This source file is part of the Swift.org open source project |
| 5 | +# |
| 6 | +# Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 7 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +# |
| 9 | +# See https://swift.org/LICENSE.txt for license information |
| 10 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +# |
| 12 | +#===------------------------------------------------------------------------===# |
| 13 | +# |
| 14 | +# This is a script that uses git-clang-format, git commit --fixup, and git |
| 15 | +# rebase --autosquash to apply git-clang-format fixups automatically to all |
| 16 | +# commits up to the parent commit of the passed in hash. The intent is that one |
| 17 | +# can work on a branch, get it to the point one is ready to commit, run this |
| 18 | +# script so that all git commits on ones branch are formatted and the create a |
| 19 | +# PR. |
| 20 | +# |
| 21 | +#===------------------------------------------------------------------------===# |
| 22 | + |
| 23 | +set -e |
| 24 | + |
| 25 | +if [[ $# -ne 1 ]]; then |
| 26 | + echo "Usage: $0 <git-hash>" |
| 27 | + echo "Applies git-clang-format to each commit backwards from HEAD to the given git hash and squashes the fixes into those commits using git commit --fixup and git-rebase --autosquash" |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +TARGET_HASH="$1^" |
| 32 | + |
| 33 | +if ! git rev-parse --verify "$TARGET_HASH" >/dev/null 2>&1; then |
| 34 | + echo "Error: '$TARGET_HASH' is not a valid git hash" |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +if ! command -v git-clang-format >/dev/null 2>&1; then |
| 39 | + echo "Error: git-clang-format not found in PATH" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +if ! git diff --quiet >/dev/null; then |
| 44 | + echo "Have changes in tree. Must not have changes in tree to run this script" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +if ! git diff --cached --quiet >/dev/null; then |
| 49 | + echo "Have staged changes in tree. Must not have staged changes in tree to run this script" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +echo "Formatting up to $(git log --oneline -1 ${TARGET_HASH})" |
| 54 | + |
| 55 | +COMMITS=($(git rev-list "$TARGET_HASH"..HEAD)) |
| 56 | + |
| 57 | +if [[ ${#COMMITS[@]} -eq 0 ]]; then |
| 58 | + echo "No commits found between $TARGET_HASH and HEAD" |
| 59 | + exit 0 |
| 60 | +fi |
| 61 | + |
| 62 | +echo "Found ${#COMMITS[@]} commits to format" |
| 63 | + |
| 64 | +for commit in "${COMMITS[@]}"; do |
| 65 | + echo "Processing commit: $commit ($(git log --oneline -1 $commit))" |
| 66 | + |
| 67 | + if ! git-clang-format "${commit}^" >/dev/null ; then |
| 68 | + echo " Committed fixup commit with formatting changes" |
| 69 | + git commit --quiet -a --fixup "${commit}" |
| 70 | + else |
| 71 | + echo " No formatting changes needed" |
| 72 | + fi |
| 73 | +done |
| 74 | + |
| 75 | +echo "Squashing fixup commits using git rebase autosquash" |
| 76 | +git -c sequence.editor=: rebase -i --autosquash --quiet "${TARGET_HASH}^" |
| 77 | + |
| 78 | +echo "All commits formatted successfully" |
0 commit comments