|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Git Flow Helper Script |
| 4 | +# This script provides helper functions for the Git Flow branching model |
| 5 | +# |
| 6 | +# Git Flow Branching Model: |
| 7 | +# - main: Production-ready code (always deployable) |
| 8 | +# - develop: Integration branch for features (default development branch) |
| 9 | +# - feature/*: New features (branched from develop, merged back to develop) |
| 10 | +# - release/*: Release preparation (branched from develop, merged to main and develop) |
| 11 | +# - hotfix/*: Critical production fixes (branched from main, merged to main and develop) |
| 12 | +# |
| 13 | +# Usage examples: |
| 14 | +# ./scripts/git-flow.sh feature start my-feature |
| 15 | +# ./scripts/git-flow.sh feature finish my-feature |
| 16 | +# ./scripts/git-flow.sh release start 1.0.0 |
| 17 | +# ./scripts/git-flow.sh hotfix start critical-bug |
| 18 | + |
| 19 | +set -e |
| 20 | + |
| 21 | +# Colors for output |
| 22 | +RED='\033[0;31m' |
| 23 | +GREEN='\033[0;32m' |
| 24 | +YELLOW='\033[1;33m' |
| 25 | +NC='\033[0m' # No Color |
| 26 | + |
| 27 | +# Function to print colored messages |
| 28 | +print_info() { |
| 29 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 30 | +} |
| 31 | + |
| 32 | +print_warn() { |
| 33 | + echo -e "${YELLOW}[WARN]${NC} $1" |
| 34 | +} |
| 35 | + |
| 36 | +print_error() { |
| 37 | + echo -e "${RED}[ERROR]${NC} $1" |
| 38 | +} |
| 39 | + |
| 40 | +# Ensure we're in a git repository |
| 41 | +if ! git rev-parse --git-dir > /dev/null 2>&1; then |
| 42 | + print_error "Not a git repository" |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +# Ensure develop branch exists |
| 47 | +ensure_develop() { |
| 48 | + if ! git show-ref --verify --quiet refs/heads/develop; then |
| 49 | + print_warn "develop branch does not exist. Creating it from main..." |
| 50 | + git checkout -b develop main |
| 51 | + print_info "Created develop branch" |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Start a feature branch |
| 56 | +# Usage: feature start <feature-name> |
| 57 | +feature_start() { |
| 58 | + local feature_name=$1 |
| 59 | + if [ -z "$feature_name" ]; then |
| 60 | + print_error "Feature name is required" |
| 61 | + echo "Usage: $0 feature start <feature-name>" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + |
| 65 | + ensure_develop |
| 66 | + git checkout develop |
| 67 | + git pull origin develop 2>/dev/null || true |
| 68 | + git checkout -b "feature/$feature_name" develop |
| 69 | + print_info "Created and switched to feature/$feature_name" |
| 70 | +} |
| 71 | + |
| 72 | +# Finish a feature branch |
| 73 | +# Usage: feature finish <feature-name> |
| 74 | +feature_finish() { |
| 75 | + local feature_name=$1 |
| 76 | + if [ -z "$feature_name" ]; then |
| 77 | + print_error "Feature name is required" |
| 78 | + echo "Usage: $0 feature finish <feature-name>" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + |
| 82 | + local branch="feature/$feature_name" |
| 83 | + if ! git show-ref --verify --quiet refs/heads/$branch; then |
| 84 | + print_error "Branch $branch does not exist" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + git checkout $branch |
| 89 | + git checkout develop |
| 90 | + git merge --no-ff $branch -m "Merge feature/$feature_name into develop" |
| 91 | + git branch -d $branch |
| 92 | + print_info "Merged and deleted $branch" |
| 93 | +} |
| 94 | + |
| 95 | +# Start a release branch |
| 96 | +# Usage: release start <version> |
| 97 | +release_start() { |
| 98 | + local version=$1 |
| 99 | + if [ -z "$version" ]; then |
| 100 | + print_error "Version is required" |
| 101 | + echo "Usage: $0 release start <version>" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + |
| 105 | + ensure_develop |
| 106 | + git checkout develop |
| 107 | + git pull origin develop 2>/dev/null || true |
| 108 | + git checkout -b "release/$version" develop |
| 109 | + print_info "Created and switched to release/$version" |
| 110 | +} |
| 111 | + |
| 112 | +# Finish a release branch |
| 113 | +# Usage: release finish <version> |
| 114 | +release_finish() { |
| 115 | + local version=$1 |
| 116 | + if [ -z "$version" ]; then |
| 117 | + print_error "Version is required" |
| 118 | + echo "Usage: $0 release finish <version>" |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + |
| 122 | + local branch="release/$version" |
| 123 | + if ! git show-ref --verify --quiet refs/heads/$branch; then |
| 124 | + print_error "Branch $branch does not exist" |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + |
| 128 | + git checkout $branch |
| 129 | + git checkout main |
| 130 | + git merge --no-ff $branch -m "Release $version" |
| 131 | + git tag -a "v$version" -m "Release version $version" |
| 132 | + |
| 133 | + git checkout develop |
| 134 | + git merge --no-ff $branch -m "Merge release/$version into develop" |
| 135 | + |
| 136 | + git branch -d $branch |
| 137 | + print_info "Merged release/$version to main and develop, created tag v$version" |
| 138 | +} |
| 139 | + |
| 140 | +# Start a hotfix branch |
| 141 | +# Usage: hotfix start <hotfix-name> |
| 142 | +hotfix_start() { |
| 143 | + local hotfix_name=$1 |
| 144 | + if [ -z "$hotfix_name" ]; then |
| 145 | + print_error "Hotfix name is required" |
| 146 | + echo "Usage: $0 hotfix start <hotfix-name>" |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + |
| 150 | + git checkout main |
| 151 | + git pull origin main 2>/dev/null || true |
| 152 | + git checkout -b "hotfix/$hotfix_name" main |
| 153 | + print_info "Created and switched to hotfix/$hotfix_name" |
| 154 | +} |
| 155 | + |
| 156 | +# Finish a hotfix branch |
| 157 | +# Usage: hotfix finish <hotfix-name> |
| 158 | +hotfix_finish() { |
| 159 | + local hotfix_name=$1 |
| 160 | + if [ -z "$hotfix_name" ]; then |
| 161 | + print_error "Hotfix name is required" |
| 162 | + echo "Usage: $0 hotfix finish <hotfix-name>" |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + |
| 166 | + local branch="hotfix/$hotfix_name" |
| 167 | + if ! git show-ref --verify --quiet refs/heads/$branch; then |
| 168 | + print_error "Branch $branch does not exist" |
| 169 | + exit 1 |
| 170 | + fi |
| 171 | + |
| 172 | + git checkout $branch |
| 173 | + git checkout main |
| 174 | + git merge --no-ff $branch -m "Hotfix $hotfix_name" |
| 175 | + |
| 176 | + ensure_develop |
| 177 | + git checkout develop |
| 178 | + git merge --no-ff $branch -m "Merge hotfix/$hotfix_name into develop" |
| 179 | + |
| 180 | + git branch -d $branch |
| 181 | + print_info "Merged hotfix/$hotfix_name to main and develop" |
| 182 | +} |
| 183 | + |
| 184 | +# Main command handler |
| 185 | +case "$1" in |
| 186 | + feature) |
| 187 | + case "$2" in |
| 188 | + start) |
| 189 | + feature_start "$3" |
| 190 | + ;; |
| 191 | + finish) |
| 192 | + feature_finish "$3" |
| 193 | + ;; |
| 194 | + *) |
| 195 | + print_error "Unknown feature command: $2" |
| 196 | + echo "Usage: $0 feature {start|finish} <name>" |
| 197 | + exit 1 |
| 198 | + ;; |
| 199 | + esac |
| 200 | + ;; |
| 201 | + release) |
| 202 | + case "$2" in |
| 203 | + start) |
| 204 | + release_start "$3" |
| 205 | + ;; |
| 206 | + finish) |
| 207 | + release_finish "$3" |
| 208 | + ;; |
| 209 | + *) |
| 210 | + print_error "Unknown release command: $2" |
| 211 | + echo "Usage: $0 release {start|finish} <version>" |
| 212 | + exit 1 |
| 213 | + ;; |
| 214 | + esac |
| 215 | + ;; |
| 216 | + hotfix) |
| 217 | + case "$2" in |
| 218 | + start) |
| 219 | + hotfix_start "$3" |
| 220 | + ;; |
| 221 | + finish) |
| 222 | + hotfix_finish "$3" |
| 223 | + ;; |
| 224 | + *) |
| 225 | + print_error "Unknown hotfix command: $2" |
| 226 | + echo "Usage: $0 hotfix {start|finish} <name>" |
| 227 | + exit 1 |
| 228 | + ;; |
| 229 | + esac |
| 230 | + ;; |
| 231 | + *) |
| 232 | + print_error "Unknown command: $1" |
| 233 | + echo "Usage: $0 {feature|release|hotfix} {start|finish} <name>" |
| 234 | + exit 1 |
| 235 | + ;; |
| 236 | +esac |
0 commit comments