-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-multi-checkout.sh
executable file
·106 lines (88 loc) · 1.75 KB
/
git-multi-checkout.sh
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
#!/bin/bash
# Name: Git multi checkout
#
# Brief: Checks out remote branches into local branches.
#
# Flags:
#
# a - Checks out all branches of the specified remote.
#
# h - Prints usage information.
#
# Arguments:
#
# <remote>... - the repository to checkout branches from.
# Get current branch.
initial_branch="$(git symbolic-ref HEAD 2>/dev/null)"
initial_branch=${initial_branch##refs/heads/}
# Branches to merge into.
remote=
all=
usage=
function usage()
{
echo "Usage:"
echo " $0 [-p] <remote>"
return 0
}
# Reading flags.
while getopts ph name
do
case $name in
p) pretend=1;;
h) usage=1;;
esac
done
# Print usage and exit.
if [ -n "$usage" ]
then
usage
exit 0
fi
# Discarding flags from arguments.
shift $(($OPTIND - 1))
if [ $# -ne 1 ]
then
echo "Please, specify a single remote"
echo
usage
exit 2
fi
remote="$@"
if [ -z "$remote" ]
then
echo "Please, specify a remote repository"
echo
usage
exit 2
fi
if [ -n "$pretend" ]
then
echo "Pretending..."
fi
# When adding support to various remotes, change this to a for.
remote_branches=$(git branch -a | grep "^\ *remotes/${remote}/")
remote_branches=${remote_branches//remotes\/}
for remote_branch in $remote_branches
do
local_branch=${remote_branch/${remote}\/}
git show-ref --verify --quiet refs/heads/${local_branch}
if [ $? -ne 0 ]
then
if [ -n "$pretend" ]
then
echo "Checking out '$remote_branch' in '$local_branch'"
else
git checkout -b $local_branch $remote_branch
fi
else
echo "Branch '$local_branch' already exists"
failed_checkouts="$failed_checkouts $local_branch"
fi
done
# Checking out initial_branch
git checkout $initial_branch
if [ -n "$failed_checkouts" ]
then
echo "Failed checkouts: $failed_checkouts"
fi