|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +GREEN='\033[0;32m' |
| 6 | +RED='\033[0;31m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +NC='\033[0m' |
| 9 | + |
| 10 | +REBASE_BRANCH=rebased-clangir-onto-llvm-upstream |
| 11 | +TARGET_BRANCH=$1 |
| 12 | +RUN_CHECK=false |
| 13 | + |
| 14 | +log() { printf "%b[%s]%b %s\n" "$1" "$2" "$NC" "$3"; } |
| 15 | +log_info() { log "$GREEN" "INFO" "$1"; } |
| 16 | +log_warn() { log "$YELLOW" "WARN" "$1"; } |
| 17 | +log_error() { log "$RED" "ERROR" "$1" >&2; } |
| 18 | + |
| 19 | +error_exit() { |
| 20 | + log_error "$1" |
| 21 | + exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +is_rebasing() { |
| 25 | + git rev-parse --git-path rebase-merge >/dev/null 2>&1 || |
| 26 | + git rev-parse --git-path rebase-apply >/dev/null 2>&1 |
| 27 | +} |
| 28 | + |
| 29 | +while [[ $# -gt 0 ]]; do |
| 30 | + case "$1" in |
| 31 | + --checked) |
| 32 | + RUN_CHECK=true |
| 33 | + shift |
| 34 | + ;; |
| 35 | + *) |
| 36 | + TARGET_BRANCH="$1" |
| 37 | + shift |
| 38 | + ;; |
| 39 | + esac |
| 40 | +done |
| 41 | + |
| 42 | +git rev-parse --is-inside-work-tree >/dev/null 2>&1 || |
| 43 | + error_exit "Not in a Git repository." |
| 44 | + |
| 45 | +git remote get-url upstream >/dev/null 2>&1 || |
| 46 | + error_exit "Upstream remote not found." |
| 47 | + |
| 48 | +log_info "Fetching latest changes from upstream..." |
| 49 | + |
| 50 | +git fetch upstream main || |
| 51 | + error_exit "Failed to fetch from upstream." |
| 52 | + |
| 53 | +REBASING_CIR_BRANCH=$(git branch --show-current) |
| 54 | + |
| 55 | +if [ -z "$COMMON_ANCESTOR" ]; then |
| 56 | + COMMON_ANCESTOR=$(git merge-base upstream/main "$REBASING_CIR_BRANCH") || |
| 57 | + error_exit "Could not find common ancestor." |
| 58 | + log_info "Common ancestor commit: $COMMON_ANCESTOR" |
| 59 | +fi |
| 60 | + |
| 61 | +if [ "$REBASING_CIR_BRANCH" != "$REBASE_BRANCH" ]; then |
| 62 | + if git rev-parse --verify "$REBASE_BRANCH" >/dev/null 2>&1; then |
| 63 | + git branch -D "$REBASE_BRANCH" >/dev/null 2>&1 || |
| 64 | + log_warn "Failed to delete existing branch $REBASE_BRANCH." |
| 65 | + fi |
| 66 | + |
| 67 | + git switch -c "$REBASE_BRANCH" "$COMMON_ANCESTOR" || |
| 68 | + error_exit "Failed to create branch $REBASE_BRANCH." |
| 69 | +fi |
| 70 | + |
| 71 | +# |
| 72 | +# Rebase upstream changes |
| 73 | +# |
| 74 | +log_info "Processing upstream commits..." |
| 75 | +git rebase upstream/main || |
| 76 | + error_exit "Failed to rebase." |
| 77 | + |
| 78 | +# |
| 79 | +# Reverse upstream CIR commits |
| 80 | +# |
| 81 | +log_info "Reverting upstream CIR commits..." |
| 82 | +git log --grep="\[CIR\]" --format="%H %s" "$COMMON_ANCESTOR..upstream/main" | while read -r HASH MESSAGE; do |
| 83 | + log_info "Reverting: $MESSAGE" |
| 84 | + |
| 85 | + if ! git revert --no-edit "$HASH"; then |
| 86 | + error_exit "Failed to revert commit $HASH" |
| 87 | + fi |
| 88 | +done |
| 89 | + |
| 90 | +# |
| 91 | +# Rebase CIR commits |
| 92 | +# |
| 93 | +log_info "Rebasing CIR incubator commits..." |
| 94 | + |
| 95 | +if [ -z "$TARGET_BRANCH" ]; then |
| 96 | + log_error "Target branch not specified." |
| 97 | + exit 1 |
| 98 | +fi |
| 99 | + |
| 100 | +if git rev-parse --verify "$TARGET_BRANCH" >/dev/null 2>&1; then |
| 101 | + git branch -D "$TARGET_BRANCH" >/dev/null 2>&1 || |
| 102 | + error_exit "Failed to delete existing branch $TARGET_BRANCH." |
| 103 | +fi |
| 104 | + |
| 105 | +git switch "$REBASING_CIR_BRANCH" || |
| 106 | + error_exit "Failed to switch to $REBASING_CIR_BRANCH." |
| 107 | +git checkout -b "$TARGET_BRANCH" || |
| 108 | + error_exit "Failed to checkout $TARGET_BRANCH." |
| 109 | + |
| 110 | +if [ "$RUN_CHECK" = true ]; then |
| 111 | + git rebase --exec "ninja -C build check-clang-cir" "$REBASE_BRANCH" "$TARGET_BRANCH" || |
| 112 | + error_exit "Failed to rebase." |
| 113 | +else |
| 114 | + git rebase "$REBASE_BRANCH" "$TARGET_BRANCH" || |
| 115 | + error_exit "Failed to rebase." |
| 116 | +fi |
| 117 | + |
| 118 | +log_info "Rebase completed successfully!" |
0 commit comments