/
branch
executable file
·109 lines (95 loc) · 3.07 KB
/
branch
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
#
# Usage: branch [branchname]
# Usage: branch [-D] [-d] [branchname]
# Usage: branch -
#
#
# Switch to an existing branch, create a new local-only branch or delete a local or remote branch.
# Adds remote tracking if a remote branch with the same name exists,
# in any remote, preferring the "origin" remote if there are duplicates.
#
# Executing without arguments prints all local & remote branches
#
# Abort if this isn't a git repository
git rev-parse --is-inside-work-tree >/dev/null || exit $?
remote="origin"
branch=$1
delete=
if [ "$1" == "-d" ] || [ "$1" == "-D" ]; then
delete=$1
branch=$2
fi
if [ -z $branch ]; then
echo -e "\nSwitch to or create a new branch:"
echo -e " $(basename $0) [name]"
echo -e "\nSwitch to the previous branch:"
echo -e " $(basename $0) -"
echo -e "\nDelete branch:"
echo -e " $(basename $0) [-D] [-d] <name>"
echo -e "\nCurrent branches: "
branches="git branch -a -vv"
$branches
echo
exit 0
fi
# Switch to the previous branch
if [ "$branch" == "-" ]; then
git checkout -
exit 0
fi
local_branch_exists=$(git branch --no-color | egrep " $branch\$")
remote_branch_exists=$(git branch -r --no-color | egrep " $remote/$branch\$")
remotes=( $(git remote) )
remotes_with_branch=()
origin_has_branch=
# Delete branch
if [ "$delete" ]; then
echo "💀 Removing local branch..."
git branch $delete $branch
exit 0
fi
for remote in "${remotes[@]}";
do
remote_branch_exists=$(git branch -r --no-color | egrep " $remote/$branch\$")
if [ "$remote_branch_exists" ] ; then
remotes_with_branch=("${remotes_with_branch[@]}" "$remote")
if [ "$remote" = "origin" ] ; then
origin_has_branch=1
fi
fi
done
if [ ${#remotes_with_branch[@]} -gt 0 ]; then
# if there's an origin remote with the named branch, use it
if [ "$origin_has_branch" = "1" ] ; then
remote=origin
else # track the first matching branch alphabetically
remote=${remotes[0]}
fi
remote_branch_exists="$remote/$branch"
fi
# If local exists already, switch to it
if [ -n "$local_branch_exists" ] && [ ! "$local_branch_exists" == '' ]; then
echo "👓 Switching to existing local branch..."
git checkout $branch
# Track remote branch if not already
if [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
tracking=$(git branch -vv | grep "*" | awk '{ print $4 '})
# echo "Remote branch exists. Local branch is tracking: $tracking"
if [[ ! "$tracking" =~ "$remote" ]]; then
echo "⚒️ Your local branch is not tracking the corresponding remote branch, fixing..."
git branch --set-upstream-to $branch $remote/$branch
fi
# else
# echo "Remote branch does not exist, not doing anything"
fi
# If remote exists, create a local branch that tracks the remote
elif [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
echo "📡 Tracking existing remote branch '$remote_branch_exists'..."
git checkout -b $branch --track $remote/$branch
# Otherwise create a new local branch
else
echo "✏️ Creating new local branch..."
git checkout -b $branch --no-track
fi
exit 0