|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 CloudWeGo Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# |
| 17 | +# Shared utility functions for release scripts |
| 18 | +# |
| 19 | + |
| 20 | +# Function to show changes between two commits/tags |
| 21 | +# Usage: show_changes <from_ref> <to_ref> [title] |
| 22 | +show_changes() { |
| 23 | + local from_ref=$1 |
| 24 | + local to_ref=$2 |
| 25 | + local title=${3:-"Changes"} |
| 26 | + |
| 27 | + echo |
| 28 | + echo "📋 $title:" |
| 29 | + echo "$(printf '%*s' ${#title} | tr ' ' '=')" |
| 30 | + |
| 31 | + # Check if there are any commits between the references |
| 32 | + local commit_count=$(git rev-list --count "$from_ref..$to_ref") |
| 33 | + if [ "$commit_count" -eq 0 ]; then |
| 34 | + echo "✅ No new commits. Nothing to release." |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + |
| 38 | + # Show changes grouped by author email |
| 39 | + git log --pretty=format:"%ae|%h %s" "$from_ref..$to_ref" | sort | awk -F'|' '{ |
| 40 | + if ($1 != prev_email) { |
| 41 | + if (prev_email != "") print "" |
| 42 | + print "👤 " $1 ":" |
| 43 | + prev_email = $1 |
| 44 | + } |
| 45 | + print " " $2 |
| 46 | + }' |
| 47 | + |
| 48 | + return 0 |
| 49 | +} |
| 50 | + |
| 51 | +# Function to show changes for first release (no previous version) |
| 52 | +# Usage: show_first_release_changes <commit> [limit] |
| 53 | +show_first_release_changes() { |
| 54 | + local commit=$1 |
| 55 | + local limit=${2:-10} |
| 56 | + |
| 57 | + echo |
| 58 | + echo "📋 This is the first release - showing recent commits:" |
| 59 | + echo "====================================================" |
| 60 | + |
| 61 | + git log --pretty=format:"%ae|%h %s" -"$limit" "$commit" | sort | awk -F'|' '{ |
| 62 | + if ($1 != prev_email) { |
| 63 | + if (prev_email != "") print "" |
| 64 | + print "👤 " $1 ":" |
| 65 | + prev_email = $1 |
| 66 | + } |
| 67 | + print " " $2 |
| 68 | + }' |
| 69 | +} |
| 70 | + |
| 71 | +# Function to ask user for confirmation of changes |
| 72 | +# Usage: confirm_changes |
| 73 | +confirm_changes() { |
| 74 | + echo |
| 75 | + echo "📝 Please review the changes above." |
| 76 | + read -p "Do you want to proceed with these changes? (y/N): " confirm_changes |
| 77 | + if [ "$confirm_changes" != "y" ] && [ "$confirm_changes" != "Y" ]; then |
| 78 | + echo "❌ Release cancelled by user" |
| 79 | + return 1 |
| 80 | + fi |
| 81 | + return 0 |
| 82 | +} |
| 83 | + |
| 84 | +# Function to check if commit exists on origin |
| 85 | +# Usage: check_commit_exists <commit> <branch> |
| 86 | +check_commit_exists() { |
| 87 | + local commit=$1 |
| 88 | + local branch=$2 |
| 89 | + if ! git cat-file -e "$commit" 2>/dev/null; then |
| 90 | + echo "❌ Error: Commit $commit does not exist locally" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + if ! git branch -r --contains "$commit" | grep -q "origin/$branch$"; then |
| 95 | + echo "❌ Error: Commit $commit does not exist on origin/$branch" |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +# Function to get latest version tag |
| 101 | +# Usage: get_latest_version |
| 102 | +get_latest_version() { |
| 103 | + local latest_tag=$(git tag -l "v*.*.*" | sort -V | tail -n1) |
| 104 | + if [ -z "$latest_tag" ]; then |
| 105 | + echo "v0.0.0" |
| 106 | + else |
| 107 | + echo "$latest_tag" |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +# Function to get latest patch version for a given minor version |
| 112 | +# Usage: get_latest_patch_version <minor_version> |
| 113 | +get_latest_patch_version() { |
| 114 | + local minor_version=$1 |
| 115 | + # Remove 'v' prefix and '.x' suffix if present |
| 116 | + minor_version=${minor_version#v} |
| 117 | + minor_version=${minor_version%.x} |
| 118 | + |
| 119 | + local latest_patch=$(git tag -l "v${minor_version}.*" | sort -V | tail -n1) |
| 120 | + if [ -z "$latest_patch" ]; then |
| 121 | + echo "No tags found for minor version v${minor_version}.x" |
| 122 | + return 1 |
| 123 | + else |
| 124 | + echo "$latest_patch" |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# Function to increment version |
| 129 | +# Usage: increment_version <version> <type> |
| 130 | +# where type is one of: major, minor, patch |
| 131 | +increment_version() { |
| 132 | + local version=$1 |
| 133 | + local type=$2 |
| 134 | + |
| 135 | + # Remove 'v' prefix |
| 136 | + version=${version#v} |
| 137 | + |
| 138 | + IFS='.' read -r major minor patch <<< "$version" |
| 139 | + |
| 140 | + case $type in |
| 141 | + "major") |
| 142 | + major=$((major + 1)) |
| 143 | + minor=0 |
| 144 | + patch=0 |
| 145 | + ;; |
| 146 | + "minor") |
| 147 | + minor=$((minor + 1)) |
| 148 | + patch=0 |
| 149 | + ;; |
| 150 | + "patch") |
| 151 | + patch=$((patch + 1)) |
| 152 | + ;; |
| 153 | + esac |
| 154 | + |
| 155 | + echo "v$major.$minor.$patch" |
| 156 | +} |
| 157 | + |
| 158 | +# Function to increment patch version (convenience wrapper) |
| 159 | +# Usage: increment_patch_version <version> |
| 160 | +increment_patch_version() { |
| 161 | + local version=$1 |
| 162 | + increment_version "$version" "patch" |
| 163 | +} |
0 commit comments