This commit is contained in:
Vincent Yan 2026-02-24 08:09:45 -08:00 committed by GitHub
commit 7a6d43c220
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 91 additions and 1 deletions

View file

@ -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\"

View file

@ -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:

View file

@ -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.<url>.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"