forked from cmcculloh/GitScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trackbranch.sh
executable file
·122 lines (106 loc) · 3.04 KB
/
trackbranch.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
## /*
# @usage trackbranch [<branch_name>]
#
# @description
# This script allows an easy way to set the upstream for an existing branch.
#
# If there is a remote branch of the same name in existance, it will choose it.
#
# If it does not find a remote branch it will push the <branch_name> to the remote.
#
# It will then set the upstream for the local branch to the remote branch of the same name.
# description@
#
# @notes
# - It uses the git command git branch --set-upstream foo upstream/foo
# notes@
#
# @examples
# 1) trackbranch
# 2) trackbranch foo
# examples@
#
# @dependencies
# *checkout.sh
# clear-screen.sh
# functions/5000.branch_merge_set.sh
# functions/5000.branch_exists_local.sh
# functions/5000.branch_exists_remote.sh
# functions/5000.parse_git_branch.sh
# functions/5000.set_remote.sh
# dependencies@
#
# @file trackbranch.sh
## */
$loadfuncs
echo
if [ -z "$1" ]; then
# no parameter supplied, we shall determine current branch
startingBranch=$(__parse_git_branch)
if [ -z "$startingBranch" ]; then
echo ${E}" Error: A failure has occurred. "
echo " Unable to determine current branch, and a branch name was not supplied. Exiting now. "${X}
exit 1
fi
elif __branch_exists_local "$1"; then
# parameter supplied --- and branch exists
startingBranch="$1"
elif __branch_exists_remote "$1"; then
# parameter supplied --- and branch exists
echo ${Q}"Branch does not exist locally, but it does on the remote -- check it out? (y) n"${X}
read decision
if [ -z "$decision" ] || [ "$decision" = "y" ] || [ "$decision" = "Y" ]; then
"${gitscripts_path}"checkout.sh "$1"
fi
echo
startingBranch="$1"
else
# parameter supplied --- and branch does not exist
echo ${E}" Error: a failure has occurred. "
echo " The branch \`${1}\` does not appear to exist. Exiting now... "${X}
exit 1
fi
echo
echo ${H1}${H1HL}
echo " Going to set up a remote tracking branch for: ${H1B}\`${startingBranch}\`${H1} "
echo ${H1HL}${X}
echo
echo
if __branch_merge_set $startingBranch; then
echo ${W}"Alert:${X} the branch ${B}\`${startingBranch}\`${X} is already tracking a remote branch. Exiting now..."
exit 1
else
echo "The branch ${B}\`${startingBranch}\`${X} is not yet tracking a remote branch. Determining remote now..."
__set_remote
if [ -n "$_remote" ]; then
echo
echo "Remote: ${COL_GREEN}${_remote}${COL_NORM}"
echo
echo ${O}${H2HL}
echo "$ git fetch --all --prune"
git fetch --all --prune
echo ${O}${H2HL}${X}
echo
echo
# if a remote exists, push to it.
echo "Remote determined to be ${COL_GREEN}${_remote}${COL_NORM}. Pushing to remote now..."
echo ${O}${H2HL}
echo "$ git push ${_remote} ${startingBranch}"
git push $_remote $startingBranch
echo ${O}${H2HL}${X}
fi
echo ${O}${H2HL}${X}
git config branch.$startingBranch.remote $_remote
git config branch.$startingBranch.merge refs/heads/$startingBranch
if __branch_merge_set "$startingBranch"; then
exit 0
else
echo
echo ${E}" Error: a failure has occurred. "
echo " Unable to set a tracking branch. Exiting now... "${X}
exit 1
fi
fi
__clear
exit