diff --git a/git-open b/git-open index 8581e6c..c0d3359 100755 --- a/git-open +++ b/git-open @@ -288,6 +288,9 @@ if [ "$suffix" ]; then openurl="$openurl/$suffix" fi +# get browser from git config, with URL-specific matching +browser=$(getConfig "browser") + # get current open browser command case $( uname -s ) in Darwin) open='open';; @@ -307,4 +310,4 @@ if (( print_only )); then fi # open it in a browser -${BROWSER:-$open} "$openurl" +eval "${BROWSER:-${browser:-$open}}" \"\$openurl\" diff --git a/git-open.1.md b/git-open.1.md index 504669a..37b459c 100644 --- a/git-open.1.md +++ b/git-open.1.md @@ -158,6 +158,34 @@ git config [--global] "open.https://git.internal.biz.protocol" "http" ``` +### Browser options + +By default, `git open` uses your system's default browser (or the `BROWSER` environment variable if set). You can configure a specific browser globally or per URL pattern using git config's URL matching. + +`open.browser` + The default browser command to use for opening URLs. + +`open.[gitdomain].browser` + The browser command to use for URLs matching the given domain/path pattern. The most specific match wins. + +This is useful when you want to open different repositories in different browsers (e.g., work repos in Edge, personal repos in Safari). + +**Example** + +```sh +# Default: open in Safari +git config --global open.browser "open -a Safari" + +# Company repos: open in Edge +git config --global "open.https://github.com/company-org.browser" "open -a 'Microsoft Edge'" + +# Self-hosted GitLab: open in Firefox +git config --global "open.https://gitlab.internal.biz.browser" "open -a Firefox" +``` + +The `BROWSER` environment variable, if set, takes priority over all git config browser settings. + + ## DEBUGGING You can run `git-open` in `echo` mode, which doesn't open your browser, but just prints the URL to stdout: diff --git a/test/git-open.bats b/test/git-open.bats index e891be5..d785965 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -666,6 +666,65 @@ setup() { assert_output "https://cnb.cool/repos/repo/-/issues/10" } +## +## Browser configuration +## + +@test "browser: default browser via open.browser config" { + git remote set-url origin "git@github.com:user/repo.git" + git checkout -B "master" + git config --local open.browser "echo" + unset BROWSER + run ../git-open + assert_output "https://github.com/user/repo" +} + +@test "browser: URL-specific browser via open..browser config" { + git remote set-url origin "git@github.com:company-org/repo.git" + git checkout -B "master" + git config --local "open.https://github.com/company-org.browser" "echo URL_MATCH" + unset BROWSER + run ../git-open + assert_output "URL_MATCH https://github.com/company-org/repo" +} + +@test "browser: URL-specific overrides default browser config" { + git remote set-url origin "git@github.com:company-org/repo.git" + git checkout -B "master" + git config --local open.browser "echo DEFAULT" + git config --local "open.https://github.com/company-org.browser" "echo OVERRIDE" + unset BROWSER + run ../git-open + assert_output "OVERRIDE https://github.com/company-org/repo" +} + +@test "browser: non-matching URL falls back to default browser config" { + git remote set-url origin "git@github.com:personal/repo.git" + git checkout -B "master" + git config --local open.browser "echo DEFAULT" + git config --local "open.https://github.com/company-org.browser" "echo COMPANY" + unset BROWSER + run ../git-open + assert_output "DEFAULT https://github.com/personal/repo" +} + +@test "browser: handles quoted arguments in browser command" { + git remote set-url origin "git@github.com:user/repo.git" + git checkout -B "master" + git config --local open.browser "echo 'spaced arg'" + unset BROWSER + run ../git-open + assert_output "spaced arg https://github.com/user/repo" +} + +@test "browser: BROWSER env var takes priority over config" { + git remote set-url origin "git@github.com:user/repo.git" + git checkout -B "master" + git config --local open.browser "echo CONFIG" + BROWSER="echo" run ../git-open + assert_output "https://github.com/user/repo" +} + teardown() { cd .. rm -rf "$foldername"