Skip to content

Commit 49650ed

Browse files
authored
git-feature: add configurable branch separator (#1072)
* git-feature: add configurable branch separator Closes #1069 This allows the use of a separator other than `/` for feature or other alias branches. While a command-line option has been provided (`-s` or `--separator`), this will most often be used via `git-extras.feature.separator`. * Significant update to bin/git-feature - Changed option parsing so that `--alias` requires an argument and will fail with an error unless provided. Applied the same logic to `--separator`. - Changed `finish` parsing to capture it as a variable flag during argument parsing. This could be extended so that if `finish` is already true, a second `finish` results in the word being added to the argument list. ```console $ git feature -- finish remote $ git feature finish finish remote ``` This has not been done because it is a bit of an inconsistent handling for documentation purposes. - Add handling of `--` to permit options or `finish` to be made part of the feature branch name. - Since `finish` is now a variable flag, simplify the name-building logic to always use `concatargs "${argv[@]}"`. This means that `git feature finish ...` and `git feature ...` behave the same in terms of feature branch name building. - Basically rewrote the man page to include better descriptions of the options as well as adding a GIT CONFIG section and additional EXAMPLES for the new features/behaviour.
1 parent 76cd9bd commit 49650ed

File tree

4 files changed

+280
-98
lines changed

4 files changed

+280
-98
lines changed

bin/git-feature

+37-14
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,63 @@ if [ -z "$branch_prefix" ]; then
66
branch_prefix="feature"
77
fi
88

9+
branch_separator=$(git config --get git-extras.feature.separator)
10+
11+
if [ -z "$branch_separator" ]; then
12+
branch_separator="/"
13+
fi
14+
915
merge_mode="--no-ff"
16+
finish=false
1017
declare -a argv
1118
while test $# != 0
1219
do
1320
case $1 in
1421
-a|--alias )
15-
if [[ -n $2 ]]
22+
if [[ -n $2 ]] && [[ $2 != -- ]]
1623
then
1724
shift # shift -a|-alias
1825
branch_prefix=$1
1926
else
20-
argv+=("$1") # treat tail '-a' as <name>
27+
echo >&2 "option $1 requires a value"
28+
exit 1
2129
fi
2230
;;
2331
-r|--remote )
24-
if [[ -n $2 ]]
32+
if [[ -n $2 ]] && [[ $2 != -- ]]
2533
then
2634
remote=$2
2735
shift
2836
else
2937
remote="origin"
3038
fi
3139
;;
40+
-s|--separator )
41+
if [[ -n $2 ]] && [[ $2 != -- ]]
42+
then
43+
branch_separator=$2
44+
shift
45+
else
46+
echo >&2 "option $1 requires a value"
47+
exit 1
48+
fi
49+
;;
3250
--squash )
3351
merge_mode="--squash"
3452
;;
3553
--from )
3654
start_point=$2
3755
shift
3856
;;
57+
-- )
58+
# terminate argument parsing
59+
shift
60+
argv+=("$@")
61+
break
62+
;;
63+
finish )
64+
finish=true
65+
;;
3966
* )
4067
argv+=("$1")
4168
;;
@@ -45,21 +72,17 @@ done
4572

4673
concatargs() {
4774
str=$(IFS='-'; echo "$*")
48-
branch="$branch_prefix"/$str
75+
branch="$branch_prefix$branch_separator$str"
4976
}
5077

51-
if test "${argv[0]}" = "finish"; then
52-
test -z "${argv[1]}" && echo "$branch_prefix" "<name> required." 1>&2 && exit 1
53-
branch="$branch_prefix"/"${argv[1]}"
78+
test -z "${argv[0]}" && echo "$branch_prefix" "<name> required." 1>&2 && exit 1
79+
80+
concatargs "${argv[@]}"
81+
82+
if "${finish}"
83+
then
5484
git merge ${merge_mode} "$branch" && git delete-branch "$branch"
5585
else
56-
test -z "${argv[0]}" && echo "$branch_prefix" "<name> required." 1>&2 && exit 1
57-
if test -n "${argv[1]}"; then
58-
concatargs "${argv[@]}"
59-
else
60-
branch="$branch_prefix"/"${argv[0]}"
61-
fi
62-
6386
if [[ -n $remote ]] && [[ -z $start_point ]]
6487
then
6588
git create-branch -r "$remote" "$branch"

man/git-feature.1

+106-26
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,85 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "GIT\-FEATURE" "1" "November 2020" "" "Git Extras"
4+
.TH "GIT\-FEATURE" "1" "September 2023" "" "Git Extras"
55
.
66
.SH "NAME"
77
\fBgit\-feature\fR \- Create/Merge feature branch
88
.
99
.SH "SYNOPSIS"
10-
\fBgit\-feature\fR [\-a|\-\-alias branch_prefix] [\-r|\-\-remote [remote_name]] <name>
10+
\fBgit\-feature\fR [\-a|\-\-alias \fIPREFIX\fR] [\-s|\-\-separator \fISEPARATOR\fR] [\-r|\-\-remote [REMOTE_NAME]] [\-\-from START_POINT] \fINAME\fR\.\.\.
1111
.
12-
.br
13-
\fBgit\-feature\fR [\-a|\-\-alias branch_prefix] finish [\-\-squash] <name>
12+
.P
13+
\fBgit\-feature\fR [\-a|\-\-alias \fIPREFIX\fR] [\-s|\-\-separator \fISEPARATOR\fR] finish [\-\-squash] \fINAME\fR\.\.\.
1414
.
1515
.SH "DESCRIPTION"
16-
Create/Merge the given feature branch
17-
.
18-
.SH "OPTIONS"
19-
<\-a|\-\-alias branch_prefix>
16+
Create or merge the given feature branch\. The feature branch name is made from the \fIPREFIX\fR, the \fISEPARATOR\fR, and the \fINAME\fR joined together\.
2017
.
2118
.P
22-
use \fBbranch_prefix\fR instead of \fBfeature\fR
19+
The default \fIPREFIX\fR is \fBfeature\fR and \fISEPARATOR\fR is \fB/\fR, which can be changed (see OPTIONS and GIT CONFIG for details)\.
2320
.
2421
.P
25-
<\-r|\-\-remote [remote_name]>
22+
The branch \fINAME\fR may be specified as multiple words which will be joined with \fB\-\fR\. If the branch name contains the word \fBfinish\fR or is another OPTION, \fB\-\-\fR should be passed to stop OPTION parsing\. See the EXAMPLES for details\.
2623
.
27-
.P
24+
.SH "OPTIONS"
25+
.
26+
.TP
27+
\fB\-a\fR \fIPREFIX\fR, \fB\-\-alias\fR \fIPREFIX\fR:
28+
.
29+
.IP
30+
The branch prefix to use, or \fBfeature\fR if not supplied\.
31+
.
32+
.TP
33+
\fB\-s\fR \fISEPARATOR\fR, \fB\-\-separator\fR \fISEPARATOR\fR:
34+
.
35+
.IP
36+
The separator to use for joining the branch prefix and the branch name, or \fB/\fR if not supplied\.
37+
.
38+
.TP
39+
\fB\-r\fR [REMOTE_NAME], \fB\-\-remote\fR [REMOTE_NAME]:
40+
.
41+
.IP
2842
Setup a remote tracking branch using \fBremote_name\fR\. If \fBremote_name\fR is not supplied, use \fBorigin\fR by default\.
2943
.
30-
.P
31-
<\-\-from [start_point]>
44+
.TP
45+
\fB\-\-from\fR START_POINT:
3246
.
33-
.P
34-
Setup a start point when the branch created\. If \fB\-\-from\fR is not supplied, use the current branch by default\.
47+
.IP
48+
Setup a start point when the branch created\. If \fB\-\-from\fR is not supplied, use the current branch by default\. This option will be ignored when \fBfinish\fRing a branch\.
3549
.
36-
.P
37-
<finish>
50+
.TP
51+
\fBfinish\fR:
3852
.
39-
.P
53+
.IP
4054
Merge and delete the feature branch\.
4155
.
42-
.P
43-
<\-\-squash>
56+
.TP
57+
\fB\-\-squash\fR:
4458
.
45-
.P
46-
Run a squash merge\.
59+
.IP
60+
Run a squash merge when \fBfinish\fRing the feature branch\.
4761
.
48-
.P
49-
<name>
62+
.TP
63+
\fINAME\fR:
5064
.
51-
.P
65+
.IP
5266
The name of the feature branch\.
5367
.
68+
.SH "GIT CONFIG"
69+
You can configure the default branch prefix and separator via git config options\.
70+
.
71+
.TP
72+
\fBgit\-extras\.feature\.prefix\fR:
73+
.
74+
.IP
75+
$ git config \-\-global add git\-extras\.feature\.prefix "prefix"
76+
.
77+
.TP
78+
\fBgit\-extras\.feature\.separator\fR:
79+
.
80+
.IP
81+
$ git config \-\-global add git\-extras\.feature\.separator "\-"
82+
.
5483
.SH "EXAMPLES"
5584
.
5685
.TP
@@ -107,6 +136,54 @@ $ (features/dependencies) git checkout master
107136
.br
108137
$ git features finish dependencies
109138
.
139+
.TP
140+
Use custom branch separator:
141+
.
142+
.IP
143+
$ git feature \-s \- dependencies
144+
.
145+
.br
146+
$ (feature\-dependencies) \.\.\.
147+
.
148+
.br
149+
$ (feature\-dependencies) git checkout master
150+
.
151+
.br
152+
$ git feature \-s \- finish dependencies
153+
.
154+
.TP
155+
Use custom branch prefix and separator from git config with multiple words:
156+
.
157+
.IP
158+
$ git config \-\-global \-\-add git\-extras\.feature\.prefix "features"
159+
.
160+
.br
161+
$ git config \-\-global \-\-add git\-extras\.feature\.separator "\."
162+
.
163+
.br
164+
$ git feature dependency tracking
165+
.
166+
.br
167+
$ (features\.dependency\-tracking) \.\.\.
168+
.
169+
.br
170+
$ (features\.dependency\-tracking) git checkout master
171+
.
172+
.br
173+
$ git feature finish dependency tracking
174+
.
175+
.TP
176+
Use a \fBgit\-feature\fR option flag as part of a branch name:
177+
.
178+
.IP
179+
$ git feature \-\- finish remote
180+
.
181+
.br
182+
\&\.\.\.
183+
.
184+
.br
185+
$ (feature/finish\-remote) git commit \-m "Some changes"
186+
.
110187
.SH "AUTHOR"
111188
Written by Jesús Espino <\fIjespinog@gmail\.com\fR>
112189
.
@@ -116,8 +193,11 @@ Modified by Mark Pitman <\fImark\.pitman@gmail\.com\fR>
116193
.br
117194
Modified by Carlos Prado <\fIcarlos\.prado@cpradog\.com\fR>
118195
.
196+
.br
197+
Modified by Austin Ziegler <\fIhalostatue@gmail\.com\fR>
198+
.
119199
.SH "REPORTING BUGS"
120200
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
121201
.
122202
.SH "SEE ALSO"
123-
<\fIhttps://github\.com/tj/git\-extras\fR>
203+
<\fIhttps://github\.com/tj/git\-extras\fR>, git\-create\-branch(1), git\-delete\-branch(1)

0 commit comments

Comments
 (0)