Skip to content

Commit 363d054

Browse files
lovefpaulirish
authored andcommitted
Git styled -h and --issue options (#94)
1 parent 80cb341 commit 363d054

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Type `git open` to open the repo website (GitHub, GitLab, Bitbucket) in your bro
99
```sh
1010
git open [remote-name] [branch-name]
1111

12-
git open issue
12+
git open --issue
1313
```
1414

15-
(`git open` works with these [hosted repo providers](#supported-remote-repositories), `git open issue` currently only works with GitHub)
15+
(`git open` works with these [hosted repo providers](#supported-remote-repositories), `git open --issue` currently only works with GitHub)
1616

1717
### Examples
1818

@@ -26,7 +26,7 @@ $ git open someremote
2626
$ git open someremote somebranch
2727
# opens https://github.com/PROVIDED_REMOTE_USER/CURRENT_REPO/tree/PROVIDED_BRANCH
2828

29-
$ git open issue
29+
$ git open --issue
3030
# If branches use naming convention of issues/#123,
3131
# opens https://github.com/TRACKED_REMOTE_USER/CURRENT_REPO/issues/123
3232
```

git-open

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
#!/usr/bin/env bash
22

3-
# Opens the GitHub page for a repo/branch in your browser.
4-
# https://github.com/paulirish/git-open/
5-
#
6-
# git open
7-
# git open [remote] [branch]
8-
9-
10-
# are we in a git repo?
11-
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
12-
echo "Not a git repository." 1>&2
13-
exit 1
14-
fi
3+
# Use git-sh-setup, similar to git-rebase
4+
# https://www.kernel.org/pub/software/scm/git/docs/git-sh-setup.html
5+
# https://github.com/git/git/blob/master/git-rebase.sh
6+
# shellcheck disable=SC2034
7+
OPTIONS_STUCKLONG=t
8+
# shellcheck disable=SC2034
9+
OPTIONS_KEEPDASHDASH=
10+
# shellcheck disable=SC2034
11+
OPTIONS_SPEC="\
12+
git open [options]
13+
git open [remote] [branch]
14+
--
15+
Opens the GitHub page for a repo/branch in your browser.
16+
https://github.com/paulirish/git-open/
17+
18+
Available options are
19+
i,issue! open issues page
20+
"
21+
22+
# https://github.com/koalaman/shellcheck/wiki/SC1090
23+
# shellcheck source=/dev/null
24+
. "$(git --exec-path)/git-sh-setup"
1525

1626
# Defaults
1727
is_issue=0
1828
protocol="https"
1929

20-
# If the first argument is 'issue', we want to load the issue page
21-
if [[ "$1" == 'issue' ]]; then
22-
is_issue=1
23-
24-
# Allow the user to provide other args, aka `git open issue upstream 79`
30+
while test $# != 0; do
31+
case "$1" in
32+
--issue) is_issue=1;;
33+
--) shift; break ;;
34+
esac
2535
shift
36+
done
37+
38+
# are we in a git repo?
39+
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
40+
echo "Not a git repository." 1>&2
41+
exit 1
2642
fi
2743

2844
# choose remote. priority to: provided argument, detected tracked remote, 'origin'

test/git-open.bats

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ setup() {
2020
assert [ -e "$foldername/.git" ]
2121
}
2222

23+
##
24+
## Help
25+
##
26+
27+
@test "help text" {
28+
run ../git-open -h
29+
assert_output --partial "usage: git open"
30+
}
31+
32+
@test "invalid option" {
33+
run ../git-open --invalid-option
34+
assert_output --partial "error: unknown option \`invalid-option'"
35+
assert_output --partial "usage: git open"
36+
}
37+
2338
##
2439
## GitHub
2540
##
@@ -74,17 +89,20 @@ setup() {
7489
assert_output "https://github.com/user/repo"
7590
}
7691

77-
@test "gh: git open issue" {
92+
@test "gh: git open --issue" {
7893
# https://github.com/paulirish/git-open/pull/46
7994
git remote set-url origin "github.com:paulirish/git-open.git"
8095
git checkout -B "issues/#12"
81-
run ../git-open "issue"
96+
run ../git-open "--issue"
8297
assert_output "https://github.com/paulirish/git-open/issues/12"
8398

84-
# https://github.com/paulirish/git-open/pull/86
85-
git checkout -B "fix-issue-36"
86-
run ../git-open "issue"
87-
assert_output "https://github.com/paulirish/git-open/issues/36"
99+
git checkout -B "fix-issue-37"
100+
run ../git-open "--issue"
101+
assert_output "https://github.com/paulirish/git-open/issues/37"
102+
103+
git checkout -B "fix-issue-38"
104+
run ../git-open "-i"
105+
assert_output "https://github.com/paulirish/git-open/issues/38"
88106
}
89107

90108
@test "gh: gist" {

0 commit comments

Comments
 (0)