github.com/openshift-online/ocm-sdk-go@v0.1.473/hack/bump-version.sh (about)

     1  #!/bin/bash
     2  # Script to bump the patch version and suggest git commands for tagging
     3  
     4  set -euo pipefail
     5  
     6  # Repository URLs
     7  OFFICIAL_REPO_SSH="git@github.com:openshift-online/ocm-sdk-go.git"
     8  OFFICIAL_REPO_HTTPS="https://github.com/openshift-online/ocm-sdk-go.git"
     9  
    10  # Colors for output
    11  RED='\033[0;31m'
    12  GREEN='\033[0;32m'
    13  YELLOW='\033[1;33m'
    14  BLUE='\033[0;34m'
    15  NC='\033[0m' # No Color
    16  
    17  # Function to print colored output
    18  print_info() {
    19      echo -e "${BLUE}INFO:${NC} $1"
    20  }
    21  
    22  print_success() {
    23      echo -e "${GREEN}SUCCESS:${NC} $1"
    24  }
    25  
    26  print_warning() {
    27      echo -e "${YELLOW}WARNING:${NC} $1"
    28  }
    29  
    30  print_error() {
    31      echo -e "${RED}ERROR:${NC} $1"
    32  }
    33  
    34  # Check if we're in a git repository
    35  if ! git rev-parse --git-dir > /dev/null 2>&1; then
    36      print_error "Not in a git repository"
    37      exit 1
    38  fi
    39  
    40  # Check if we're on the main branch
    41  CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "")
    42  if [ "$CURRENT_BRANCH" != "main" ]; then
    43      print_error "You must be on the 'main' branch to create a release tag"
    44      print_error "Current branch: $CURRENT_BRANCH"
    45      print_error "Switch to main branch with: git checkout main"
    46      exit 1
    47  fi
    48  
    49  # Get the latest tag
    50  print_info "Finding the latest release tag..."
    51  LATEST_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -1)
    52  
    53  if [ -z "$LATEST_TAG" ]; then
    54      print_error "No semantic version tags found (expected format: vX.Y.Z)"
    55      exit 1
    56  fi
    57  
    58  print_success "Latest tag found: $LATEST_TAG"
    59  
    60  # Parse the version components
    61  if [[ $LATEST_TAG =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
    62      MAJOR=${BASH_REMATCH[1]}
    63      MINOR=${BASH_REMATCH[2]}
    64      PATCH=${BASH_REMATCH[3]}
    65  else
    66      print_error "Invalid tag format: $LATEST_TAG (expected format: vX.Y.Z)"
    67      exit 1
    68  fi
    69  
    70  # Bump patch version
    71  NEW_PATCH=$((PATCH + 1))
    72  NEW_TAG="v${MAJOR}.${MINOR}.${NEW_PATCH}"
    73  
    74  print_info "Version bump analysis:"
    75  echo "  Current version: $LATEST_TAG"
    76  echo "  New version:     $NEW_TAG"
    77  echo "  Change:          Patch version bumped from $PATCH to $NEW_PATCH"
    78  
    79  # Get git remote information
    80  # Find the remote that points to the official openshift-online repository
    81  REMOTE_NAME=""
    82  REMOTE_URL=""
    83  
    84  # Look for the official repository (SSH or HTTPS) with (push)
    85  while read -r line; do
    86      if [[ "$line" == *"$OFFICIAL_REPO_SSH"*"(push)" ]] || \
    87         [[ "$line" == *"$OFFICIAL_REPO_HTTPS"*"(push)" ]]; then
    88          REMOTE_NAME=$(echo "$line" | awk '{print $1}')
    89          REMOTE_URL=$(echo "$line" | awk '{print $2}')
    90          break
    91      fi
    92  done < <(git remote -v)
    93  
    94  # If we didn't find the official remote, error out
    95  if [ -z "$REMOTE_NAME" ]; then
    96      print_error "Could not find a remote pointing to the official repository"
    97      print_error "Looking for: $OFFICIAL_REPO_SSH or $OFFICIAL_REPO_HTTPS"
    98      print_error "Available remotes:"
    99      git remote -v | while read line; do echo "  $line"; done
   100      print_error "Please add the official repository as a remote, for example:"
   101      print_error "  git remote add upstream $OFFICIAL_REPO_SSH"
   102      exit 1
   103  fi
   104  
   105  # Current branch was already determined above during branch check
   106  
   107  echo ""
   108  print_info "Git repository information:"
   109  echo "  Remote:  $REMOTE_NAME ($REMOTE_URL)"
   110  echo "  Branch:  $CURRENT_BRANCH"
   111  
   112  # Check if tag already exists
   113  if git tag | grep -q "^${NEW_TAG}$"; then
   114      print_error "Tag $NEW_TAG already exists!"
   115      exit 1
   116  fi
   117  
   118  # Check for uncommitted changes
   119  if ! git diff --quiet --exit-code || ! git diff --cached --quiet --exit-code; then
   120      print_warning "There are uncommitted changes in the repository"
   121      echo "  Consider committing or stashing changes before creating a tag"
   122  fi
   123  
   124  # Display suggested commands
   125  echo ""
   126  print_success "Suggested commands to create and push the new tag:"
   127  echo ""
   128  echo -e "${GREEN}# Create the new tag:${NC}"
   129  echo "git tag -a -m 'Release ${NEW_TAG#v}' $NEW_TAG"
   130  echo ""
   131  echo -e "${GREEN}# Push the tag to remote:${NC}"
   132  echo "git push $REMOTE_NAME $NEW_TAG"
   133  echo ""
   134