-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-rbranch
executable file
·152 lines (133 loc) · 4.09 KB
/
git-rbranch
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
function usage() {
echo "Usage: git rbranch [-c] [-e] [-p] [<remote>] <branch> [<from>]"
echo
echo "If you want to create a local branch that will later track a remote"
echo "branch, but it does not yet exist, you can use this command instead"
echo "of setting up configuration manually or deleting and re-creating"
echo "your local branch later, after it's been pushed."
echo
echo "Args:"
echo " -c - Only create the branch, don't check it out"
echo " -e - Don't create an empty commit at the start of the branch (default"
echo " is to create such a commit)"
echo " -p - Only create the branch, don't check it out"
echo " <remote> - A remote repository, defaults to 'origin'"
echo " <branch> - The name of a remote branch"
echo " <from> - Starting point for the new branch"
}
# Default values for options
checkout=yes
empty=yes
push=no
GETOPT=$(getopt -u -o hcep -- "$@")
if [[ $? != 0 ]]; then
usage
exit 1
fi
set -- $GETOPT
while [[ 0 != $# ]]; do
case "$1" in
-c)
checkout=no
;;
-e)
empty=no
;;
-p)
push=yes
;;
-h)
usage
exit 0
;;
--)
shift
break
;;
*)
echo "Unrecognized option ($1)??" >&2
usage
exit 1
;;
esac
shift
done
if [[ $# -lt 1 || 3 -lt $# ]]; then
usage
exit 1
fi
remote=$1; shift;
# If remote is not recognized, assume origin
if [[ -z $(git config --get "remote.$remote.url") ]]; then
branch=$remote
remote="origin"
else
branch=$1; shift;
fi
# If a remote is actually given as part of the branch, split them and use it
if [[ $(echo $branch | awk '$1 ~ /\// { print }') ]]; then
remote="${branch%%/*}"
branch="${branch##*/}"
fi
if [[ -z $(git config --get "remote.$remote.url") ]]; then
echo "Unrecognized remote repository, $remote." >&2
exit 1
fi
from=$1
# Now that we have our arguments, first check to see if the branch already exists
if [[ -n $(git for-each-ref "refs/heads/$branch") ]]; then
echo "$branch already exists, not creating."
if [[ -z $(git for-each-ref "refs/remotes/$remote/$branch") ]]; then
echo "Warning: The tracking branch $remote/$branch does not (yet) exists" >&2
fi
# It does. Is it already tracking?
current_remote=$(git config --get "branch.$branch.remote")
current_merge=$(git config --get "branch.$branch.merge")
if [[ -z $current_merge ]]; then
# Not tracking, set it up
git config "branch.$branch.remote" "$remote"
git config "branch.$branch.merge" "refs/heads/$branch"
echo "$branch set up to track $remote/$branch"
else
if [ "$remote" = "$current_remote" -a "$current_merge" = "refs/heads/$branch" ]; then
echo "$branch is already tracking $remote/$branch"
else
echo "$branch is tracking $current_remote/$current_remote_head instead!"
exit 1
fi
fi
else
# It doesn't, so let's create it
echo "] git branch $branch" $from
# Don't quote $from here because passing an empty string is an error
git branch --no-track "$branch" $from || exit $?
git config "branch.$branch.remote" "$remote"
git config "branch.$branch.merge" "refs/heads/$branch"
echo -n "Created branch $branch tracking $remote/$branch from "
if [[ -n $from ]]; then
where=$from
else
head=$(git symbolic-ref HEAD 2>/dev/null)
head=${head##refs/heads/}
if [[ -n $head ]]; then
where=$head
else
where=HEAD
fi
fi
echo $where
commit_message="Created branch $branch from $where"
created_branch=1
fi
if [[ yes = $checkout ]]; then
echo "] git checkout $branch"
git checkout "$branch"
fi
if [[ -n $created_branch && yes = $empty ]]; then
git commit --allow-empty -m "$commit_message"
fi
if [[ -n $created_branch && yes = $push ]]; then
git pushme
fi