-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-corb
executable file
·107 lines (98 loc) · 3.07 KB
/
git-corb
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
#!/bin/bash
#
# git Check Out Remote Branch
# Track a branch and check it out with as little typing as possible
#
function usage() {
echo "Usage: git corb [-f | --fetch] [<remote>] <branch>"
echo
echo "Check Out Remote Branch"
echo
echo "If you want to create a local branch that tracks a remote branch,"
echo "while using the same name, you can use this command instead of"
echo "typing the name several times in lengthy, error-prone command(s)."
echo "Lots of special cases are detected and handled."
echo
echo "Args:"
echo " -f - Fetch from the remote before creating a branch"
echo " <remote> - A remote repository, defaults to 'origin'"
echo " <branch> - The name of a remote branch"
}
GETOPT=$(getopt -u -o hf --long fetch -- "$@")
if [[ $? != 0 ]]; then
usage
exit 1
fi
set -- $GETOPT
while [[ 0 != $# ]]; do
case "$1" in
-f|--fetch)
fetch=yes
;;
-h)
usage
exit 0
;;
--)
shift
break
;;
*)
echo "Unrecognized option ($1)??" >&2
usage
exit 1
;;
esac
shift
done
if [[ $# = 0 ]]; then
usage
exit 1
fi
remote=$1;
branch=$2;
# If no remote was specified, assume origin
if [[ -z $branch ]]; then
branch=$remote
remote="origin"
fi
# If a remote is actually given as part of the branch, split them and use it
if [[ -n $(echo $branch | awk '$1 ~ /\// { print }') ]]; then
remote="${branch%%/*}"
branch="${branch##*/}"
fi
# Fetch the branch from the remote, if requested
if [[ -n $fetch ]]; then
echo "] git fetch $remote $branch"
git fetch "$remote" "$branch"
fi
track_remote=$(git config "branch.$branch.remote")
track_branch=$(git config "branch.$branch.merge")
track_branch="${track_branch##*/}"
if [[ $remote = $track_remote && $branch = $track_branch ]]; then
echo "$branch already tracks $remote/$branch."
echo "] git checkout $branch"
git checkout "$branch"
elif [[ -n $track_remote ]]; then
echo "$branch tracks $track_remote/$track_branch, not $remote/$branch!"
exit 1
elif [[ -z $(git for-each-ref "refs/remotes/$remote/$branch") ]]; then
echo "There is no $remote/$branch (tracking) branch!"
exit 1
elif [[ -z $(git for-each-ref "refs/heads/$branch") ]]; then
echo "Attempting to checkout and have $branch track from $remote:"
echo "] git checkout -b $branch $remote/$branch"
git checkout -b "$branch" "$remote/$branch"
elif git rev-list "$remote/$branch..$branch" | grep -q .; then
echo "Making $branch track $remote/$branch would lose information!"
exit 1
else
echo "Replacing $branch to track $remote/$branch:"
echo "] git checkout $remote/$branch # Make sure we don't have $branch checked out"
git checkout "$remote/$branch" || exit $?
echo "] git branch -D $branch # Delete non-tracking local branch"
git branch -D "$branch" || exit $?
echo "] git checkout -b $branch $remote/$branch"
git checkout -b "$branch" "$remote/$branch"
fi
exit $?