-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ticket
executable file
·207 lines (173 loc) · 5.35 KB
/
git-ticket
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
# ==========================================================================
# Common functionality - from gitflow-common
# --------------------------------------------------------------------------
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }
# ==========================================================================
# Basic ticket information functions
# --------------------------------------------------------------------------
_ticket_name()
{
echo "ticket/${1}"
}
_ticket_number()
{
git branch 2>/dev/null \
| grep -e '^* ticket' \
| sed -e 's/* ticket[\/-]\([0-9]\{1,\}\)/\1/'
}
_current_branch()
{
git branch 2>/dev/null \
| grep -e '^* ' \
| sed -e 's/* \(.*\)/\1/'
}
_branch_exists()
{
`git show-ref --verify --quiet refs/heads/"$1"` \
&& echo !$?
}
_remote_branch_exists()
{
`git show-ref --verify --quiet remotes/origin/"$1"` \
&& echo !$?
}
_tracker()
{
local tracker=$(git config --get ticket.tracker)
test $? && die "Git config 'ticket.tracker' is undefined"
$tracker "$@"
}
# ==========================================================================
# Git macros
# --------------------------------------------------------------------------
_git_delete_branch()
{
branch=$1
test -z $branch && die "branch required."
if [ $(_branch_exists $branch) ]; then
git branch -d $branch
else
die "No local branch '$branch' to delete."
fi
if [ $(_remote_branch_exists $branch) ]; then
git branch -d -r origin/$branch
git push origin :refs/heads/$branch
else
die "No remote branch of ${branch}."
fi
exit 0
}
_git_count_incoming()
{
branch=$1
current=$(_current_branch)
git log --pretty=%p "$branch" "^$current" | wc -l
}
# ==========================================================================
# The Subcommands
# --------------------------------------------------------------------------
usage()
{
echo "usage: git ticket <number|subcommand>"
echo
echo "Available subcommands are:"
echo " number Get the number of the current ticket."
echo " list Show all of your ticket/#... branches."
echo " open Create or switch to a ticket branch."
echo " browse Launch the tracking page for current ticket url."
echo " status Show information about a ticket from tracking system."
echo " stage* Merge a ticket's changes into current branch."
echo " close* Merge and delete specified ticket's branches."
echo
}
main()
{
if [ $# -lt 1 ]; then
usage
exit 1
fi
case "$1" in
number|no)
ticket=$(_ticket_number)
branch=$(_current_branch)
test -z "$ticket" && exit 1
# && echo "'$branch' is not a ticket branch." 1>&2
echo "$ticket"
exit $?
;;
list)
shift;
git branch "$@" | grep -e 'ticket[/-]#\?[0-9]\+'
;;
view|browse|update|status|subject|edit)
_tracker "$@"
exit $?
;;
stage)
shift;
test -z $1 && die "ticket <number> required."
number="$1"; shift
subject=$(_tracker subject "$number")
percent=$(echo "${1:-100}" | sed -e 's/\([0-9]\{1,\}\)$/\1%/')
branch=$(_ticket_name "$number")
if [ $(_git_count_incoming $branch) -gt 1 ]; then
git merge --no-ff $branch -e -m"[#${number}] ${subject} (Staged ${percent})"
else
git merge $branch
fi
exit 0
;;
close)
shift;
test -z $1 && die "ticket <number> required."
number="$1"; shift
subject=$(_tracker subject "$number")
branch=$(_ticket_name "$number")
percent=$(echo "${1:-100}" | sed -e 's/\([0-9]\{1,\}\)$/\1%/')
if [ $(_git_count_incoming $branch) -gt 1 ]; then
git merge --no-ff $branch -e -m"[#${number}] ${subject} (Closed ${percent})" \
&& _git_delete_branch $branch
else
git merge $branch && _git_delete_branch $branch
fi
exit 0
;;
not-in)
shift
other="${1:-dev-master}"
current=$(_current_branch)
git log "$(_current_branch)" "^$other"
;;
delete|del)
test -z $2 && die "ticket <number> required."
branch=$(_ticket_name $2)
_git_delete_branch $branch
# echo "You're gonna have to pull the trigger yourself:"
# echo git branch -D $branch
;;
create)
die "Ticket creation is not supported."
#_curl_redmine_create "${@:2}"
;;
*|open|start|switch)
case "$1" in
("start"|"open"|"switch") shift;;
esac
if [ ! -z "${1##*[!0-9]*}" ]; then
branch=$(_ticket_name $1)
else
die "ticket <number> required."
fi
if [ ! $(_branch_exists $branch) ]; then
git checkout -b $branch &> /dev/null
else
git checkout $branch &> /dev/null
fi
exit $?
;;
esac
exit $?
}
main "$@"