From 7adaeb31286279d5aba9ad5158ef003ff0d2623e Mon Sep 17 00:00:00 2001 From: Vincent Yan Date: Sat, 14 Feb 2026 17:14:08 -0500 Subject: [PATCH 1/2] feat: add URL-based browser configuration Allow configuring which browser to use via git config, with URL-specific matching. This enables opening different repos in different browsers (e.g., work repos in Edge, personal in Safari). Usage: git config --global open.browser "open -a Safari" git config --global "open.https://github.com/org.browser" "open -a 'Microsoft Edge'" Co-Authored-By: Claude Opus 4.6 --- git-open | 5 ++++- git-open.1.md | 28 ++++++++++++++++++++++++++ test/git-open.bats | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/git-open b/git-open index 7dcae92..a0a11fd 100755 --- a/git-open +++ b/git-open @@ -279,6 +279,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';; @@ -298,4 +301,4 @@ if (( print_only )); then fi # open it in a browser -${BROWSER:-$open} "$openurl" +${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 9f7d1c8..03818b3 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -659,6 +659,56 @@ 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: 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" From 01813c1d9d613745c3ef36589072ec8640520b36 Mon Sep 17 00:00:00 2001 From: Vincent Yan Date: Sat, 14 Feb 2026 17:17:17 -0500 Subject: [PATCH 2/2] fix: use eval for browser command to handle quoted arguments Browser commands like `open -a 'Microsoft Edge'` need eval so quotes inside the variable are interpreted as shell syntax, not literal characters. Co-Authored-By: Claude Opus 4.6 --- git-open | 2 +- test/git-open.bats | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/git-open b/git-open index a0a11fd..ba6328f 100755 --- a/git-open +++ b/git-open @@ -301,4 +301,4 @@ if (( print_only )); then fi # open it in a browser -${BROWSER:-${browser:-$open}} "$openurl" +eval "${BROWSER:-${browser:-$open}}" \"\$openurl\" diff --git a/test/git-open.bats b/test/git-open.bats index 03818b3..f5e74f4 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -701,6 +701,15 @@ setup() { 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"