Skip to content

Commit d3bc760

Browse files
committed
Merge branch 'feature/allow-prefixes-as-name-arg-on-finish' into develop
2 parents 7ae320b + 1ee37e7 commit d3bc760

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

git-flow-feature

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ FLAG_FETCH=0
1818
usage() {
1919
echo "usage: git flow feature [list]"
2020
echo " git flow feature start <name> [<base>]"
21-
echo " git flow feature finish <name> [<base>]"
21+
echo " git flow feature finish <name|nameprefix> [<base>]"
2222
echo " git flow feature publish <name>"
2323
echo " git flow feature track <name>"
24-
echo " git flow feature diff <name>"
24+
echo " git flow feature diff <name|nameprefix>"
2525
# TODO
2626
#echo ""
2727
#echo "options:"
@@ -55,12 +55,44 @@ cmd_help() {
5555
exit 0
5656
}
5757

58-
parse_args() {
58+
resolve_name_by_prefix() {
59+
# first, check if there is a perfect match
60+
if has "$LOCAL_BRANCHES" "$PREFIX$1"; then
61+
echo "$1"
62+
return 0
63+
fi
64+
65+
MATCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX$1")"
66+
NUM_MATCHES=$(echo "$MATCHES" | wc -l)
67+
if [ $NUM_MATCHES -eq 1 ]; then
68+
# sed arg looks a bit weird, but $PREFIX should not contain spaces,
69+
# so this one is safe
70+
echo "$MATCHES" | sed "s $PREFIX g"
71+
elif [ $NUM_MATCHES -eq 0 ]; then
72+
# no prefix match, so take it literally
73+
echo "$1"
74+
else
75+
# multiple matches, cannot decide
76+
warn "Multiple branches match for prefix '$1':"
77+
for match in $MATCHES; do
78+
warn "- $match"
79+
done
80+
die "Aborting. Use an unambiguous prefix."
81+
fi
82+
}
83+
84+
get_name_by_prefix() {
85+
NAME=$(resolve_name_by_prefix "$1")
86+
if [ -z "$NAME" ]; then
87+
exit 1
88+
fi
89+
}
90+
91+
parse_args_common() {
5992
# TODO: When we have a nice structured way of parsing flags with getopt,
6093
# implement the following flags:
6194
# --fetch, to set FLAG_FETCH=1
6295
# --no-fetch, to set FLAG_FETCH=0
63-
NAME="$1"
6496
BASE="${2:-$DEVELOP_BRANCH}"
6597
if [ "$NAME" = "" ]; then
6698
echo "Missing argument <name>."
@@ -70,6 +102,16 @@ parse_args() {
70102
BRANCH=$PREFIX$NAME
71103
}
72104

105+
parse_args_with_name_prefix() {
106+
get_name_by_prefix "$1"
107+
parse_args_common
108+
}
109+
110+
parse_args() {
111+
NAME="$1"
112+
parse_args_common
113+
}
114+
73115
cmd_start() {
74116
parse_args "$@"
75117

@@ -99,7 +141,7 @@ cmd_start() {
99141
}
100142

101143
cmd_finish() {
102-
parse_args "$@"
144+
parse_args_with_name_prefix "$@"
103145

104146
# sanity checks
105147
gitflow_require_branch $BRANCH
@@ -251,7 +293,7 @@ cmd_track() {
251293
}
252294

253295
cmd_diff() {
254-
parse_args "$@"
296+
parse_args_with_name_prefix "$@"
255297
# TODO: if this feature has been based on a non-develop branch, we really
256298
# should not be comparing to $DEVELOP. How to deal with this?
257299
git diff $DEVELOP_BRANCH..$BRANCH

0 commit comments

Comments
 (0)