|
| 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 | + |
| 13 | +log() { printf "%b[%s]%b %s\n" "$1" "$2" "$NC" "$3"; } |
| 14 | +log_info() { log "$GREEN" "INFO" "$1"; } |
| 15 | +log_warn() { log "$YELLOW" "WARN" "$1"; } |
| 16 | +log_error() { log "$RED" "ERROR" "$1" >&2; } |
| 17 | + |
| 18 | +error_exit() { |
| 19 | + log_error "$1" |
| 20 | + exit 1 |
| 21 | +} |
| 22 | + |
| 23 | +is_rebasing() { |
| 24 | + git rev-parse --git-path rebase-merge >/dev/null 2>&1 || |
| 25 | + git rev-parse --git-path rebase-apply >/dev/null 2>&1 |
| 26 | +} |
| 27 | + |
| 28 | +git rev-parse --is-inside-work-tree >/dev/null 2>&1 || |
| 29 | + error_exit "Not in a Git repository." |
| 30 | + |
| 31 | +git remote get-url upstream >/dev/null 2>&1 || |
| 32 | + error_exit "Upstream remote not found." |
| 33 | + |
| 34 | +log_info "Fetching latest changes from upstream..." |
| 35 | + |
| 36 | +git fetch upstream main || |
| 37 | + error_exit "Failed to fetch from upstream." |
| 38 | + |
| 39 | +REBASING_CIR_BRANCH=$(git branch --show-current) |
| 40 | + |
| 41 | +if [ -z "$COMMON_ANCESTOR" ]; then |
| 42 | + COMMON_ANCESTOR=$(git merge-base upstream/main "$REBASING_CIR_BRANCH") || |
| 43 | + error_exit "Could not find common ancestor." |
| 44 | + log_info "Common ancestor commit: $COMMON_ANCESTOR" |
| 45 | +fi |
| 46 | + |
| 47 | +if [ "$REBASING_CIR_BRANCH" != "$REBASE_BRANCH" ]; then |
| 48 | + if git rev-parse --verify "$REBASE_BRANCH" >/dev/null 2>&1; then |
| 49 | + git branch -D "$REBASE_BRANCH" >/dev/null 2>&1 || |
| 50 | + log_warn "Failed to delete existing branch $REBASE_BRANCH." |
| 51 | + fi |
| 52 | + |
| 53 | + git switch -c "$REBASE_BRANCH" "$COMMON_ANCESTOR" || |
| 54 | + error_exit "Failed to create branch $REBASE_BRANCH." |
| 55 | +fi |
| 56 | + |
| 57 | +# |
| 58 | +# Rebase upstream changes |
| 59 | +# |
| 60 | +log_info "Processing upstream commits..." |
| 61 | +git rebase upstream/main || |
| 62 | + error_exit "Failed to rebase." |
| 63 | + |
| 64 | +# |
| 65 | +# Reverse upstream CIR commits |
| 66 | +# |
| 67 | +log_info "Reverting upstream CIR commits..." |
| 68 | +git log --grep="\[CIR\]" --format="%H %s" "$COMMON_ANCESTOR..upstream/main" | while read -r HASH MESSAGE; do |
| 69 | + log_info "Reverting: $MESSAGE" |
| 70 | + |
| 71 | + if ! git revert --no-edit "$HASH"; then |
| 72 | + error_exit "Failed to revert commit $HASH" |
| 73 | + fi |
| 74 | +done |
| 75 | + |
| 76 | +# |
| 77 | +# Rebase CIR commits |
| 78 | +# |
| 79 | +log_info "Rebasing CIR incubator commits..." |
| 80 | + |
| 81 | +if [ -z "$TARGET_BRANCH" ]; then |
| 82 | + log_error "Target branch not specified." |
| 83 | + exit 1 |
| 84 | +fi |
| 85 | + |
| 86 | +if git rev-parse --verify "$TARGET_BRANCH" >/dev/null 2>&1; then |
| 87 | + git branch -D "$TARGET_BRANCH" >/dev/null 2>&1 || |
| 88 | + error_exit "Failed to delete existing branch $TARGET_BRANCH." |
| 89 | +fi |
| 90 | + |
| 91 | +git switch "$REBASING_CIR_BRANCH" || |
| 92 | + error_exit "Failed to switch to $REBASING_CIR_BRANCH." |
| 93 | +git checkout -b "$TARGET_BRANCH" || |
| 94 | + error_exit "Failed to checkout $TARGET_BRANCH." |
| 95 | +git rebase "$REBASE_BRANCH" "$TARGET_BRANCH" || |
| 96 | + error_exit "Failed to rebase." |
| 97 | + |
| 98 | +log_info "Rebase completed successfully!" |
0 commit comments