-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-getb
executable file
·51 lines (41 loc) · 1.78 KB
/
git-getb
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
44
45
46
47
48
49
50
51
#!/bin/bash
# Copyright (c) 2019-2020 Benjamin Holt -- MIT License
if [ "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
usage: git getb [B] [ALIAS] Checks out branch B as ALIAS; B defaults to
ALLGIT_BRANCH, ALIAS defults to initials from the
last segment of B
git getb -h|--help Print this message and exit
USAGE
exit 0
fi
# Bold=`tput bold`
# Off=`tput sgr0`
Branch="$1" # If both branch and alias are specified, use them
Alias="$2"
if [ "$#" -lt 2 ]; then # If zero or one are specified...
Branch="${ALLGIT_BRANCH:-$1}" # ...try to get branch from allgit, or assume it was given
if [ "$ALLGIT_BRANCH" ]; then # If the branch came from allgit, any argument must be the alias
Alias="$1"
fi
fi
if [ -z "$Branch" ]; then
echo "Must specify a branch directly or with allgit" >&2
exit 1
fi
ConfigRemote=`git config bettergit.defaultremote`
: ${DefaultRemote:=${ConfigRemote:-"origin"}}
FoundBranch=`git branch --list --all "$DefaultRemote/$Branch"`
if [ -z "$FoundBranch" ]; then
echo "$DefaultRemote/$Branch: branch not found" >&2
exit 1
fi
if [ -z "$Alias" ]; then
# Alias=`echo "$Branch" | sed -e 's:.*/::'` # Derrive default alias from last segment of branch name
Alias=`echo "$Branch" | sed -e 's:.*/::g' -e 's/[A-Z]*-*[0-9][0-9]*//' | tr -d 'a-z0-9_.-' | tr 'A-Z' 'a-z'` # Derive default alias from CamelCaseInitials (ignoring any JIRA-123- prefix)
if [ -z "$Alias" ]; then
Alias=`echo "$Branch" | sed -e 's:.*/::g' -e 's/[A-Z]*-*[0-9][0-9]*//' -e 's/^\([A-Za-z]\)[^_.-]*/\1/' -e 's/[_.-]\([A-Za-z]\)[^_.-]*/\1/g'` # Derive default alias from dash-separated-initials (again ignoring JIRA-123-)
fi
fi
git checkout -b "$Alias" --track "$DefaultRemote/$Branch"
###