-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-switch-create.sh
More file actions
executable file
·43 lines (30 loc) · 1.16 KB
/
git-switch-create.sh
File metadata and controls
executable file
·43 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
set -euo pipefail
# Check if git command exists
if ! command -v git &> /dev/null; then
echo "Error: git command not found. Please install Git."
exit 1
fi
PREFIX=$1
# Check if the prefix is provided
if [ -z "${PREFIX}" ]; then
read -p "Please enter the prefix (e.g., 'feature'): " PREFIX
fi
# Check if the prefix is valid
if [[ ! "${PREFIX}" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo "Error: Invalid prefix. Only alphanumeric characters, dots, underscores, and hyphens are allowed."
exit 1
fi
BRANCH_NAME=$2
# Check if the branch name is provided
if [ -z "${BRANCH_NAME}" ]; then
read -p "Please enter the branch name (e.g., 'new-branch'): " BRANCH_NAME
fi
# Trim the branch name from all special characters, convert to lowercase, convert all spaces to hyphens
BRANCH_NAME=$(echo "${BRANCH_NAME}" | sed 's/[^a-zA-Z0-9]/-/g; s/-\+/-/g; s/\/-/-/g; s/-$//g' | tr '[:upper:]' '[:lower:]')
CHAR_LIMIT=64
BRANCH_FULLNAME="${PREFIX}/${BRANCH_NAME}"
# Trim the branch name to the character limit
BRANCH_FULLNAME=$(echo "${BRANCH_FULLNAME}" | cut -c1-${CHAR_LIMIT})
echo "Creating and switching to branch: ${BRANCH_FULLNAME}"
git switch -c ${BRANCH_FULLNAME}