-
Notifications
You must be signed in to change notification settings - Fork 254
Support domain expansion from sshconfig #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,12 +56,51 @@ if [[ -z "$giturl" ]]; then | |
| exit 1 | ||
| fi | ||
|
|
||
| ssh_config=${ssh_config:-~/.ssh/config} | ||
| # Resolves an ssh alias defined in ssh_config to it's corresponding hostname | ||
| # echos out result, should be used within subshell $( ssh_resolve $host ) | ||
| # echos out nothing if alias could not be resolved | ||
| function ssh_resolve() { | ||
| domain="$1" | ||
| ssh_found=true | ||
| # Filter to only ssh_config lines that start with "Host" or "HostName" | ||
| resolved=$(while read -r ssh_line; do | ||
| # Split each line by spaces, of the form: | ||
| # Host alias [alias...] | ||
| # Host regex | ||
| # HostName resolved.domain.com | ||
| read -r -a ssh_array <<<"${ssh_line}" | ||
| ssh_optcode="${ssh_array[0]}" | ||
| if [[ ${ssh_optcode^^} == HOST ]]; then | ||
| # Host | ||
| ssh_found=false | ||
| # Iterate through aliases looking for a match | ||
| for ssh_index in $(seq 1 $((${#ssh_array[@]} - 1))); do | ||
| ssh_host=${ssh_array[$ssh_index]} | ||
| # shellcheck disable=SC2053 | ||
| if [[ $domain == $ssh_host ]]; then | ||
| # Found a match, next HostName entry will be returned while matched | ||
| ssh_found=true | ||
| break | ||
| fi | ||
| done | ||
| elif $ssh_found && [[ ${ssh_optcode^^} == HOSTNAME ]]; then | ||
| # HostName, but only if ssh_found is true (the last Host entry matched) | ||
| # Replace all instances of %h with the Host alias | ||
| echo "${ssh_array[1]//%h/$domain}" | ||
| fi | ||
| done < <(grep -iE "^\\s*Host(Name)?\\s+" "$ssh_config")) | ||
| # Take only the last resolved hostname (multiple are overridden) | ||
| tail -1 <<<"$resolved" | ||
| } | ||
|
|
||
| # From git-fetch(5), native protocols: | ||
| # ssh://[user@]host.xz[:port]/path/to/repo.git/ | ||
| # git://host.xz[:port]/path/to/repo.git/ | ||
| # http[s]://host.xz[:port]/path/to/repo.git/ | ||
| # ftp[s]://host.xz[:port]/path/to/repo.git/ | ||
| # [user@]host.xz:path/to/repo.git/ - scp-like but is an alternative to ssh. | ||
| # [user@]hostalias:path/to/repo.git/ - handles host aliases defined in ssh_config(5) | ||
|
|
||
| # Determine whether this is a url (https, ssh, git+ssh...) or an scp-style path | ||
| if [[ "$giturl" =~ ^[a-z\+]+://.* ]]; then | ||
|
|
@@ -85,6 +124,14 @@ else | |
| # Split on first ':' to get server name and path | ||
| domain=${uri%%:*} | ||
| urlpath=${uri#*:} | ||
|
|
||
| # Resolve sshconfig aliases | ||
| if [[ -e "$ssh_config" ]]; then | ||
| domain_resolv=$(ssh_resolve "$domain") | ||
| if [[ ! -z "$domain_resolv" ]]; then | ||
| domain="$domain_resolv" | ||
| fi | ||
| fi | ||
| fi | ||
|
|
||
| # Trim "/" from beginning of URL; "/" and ".git" from end of URL | ||
|
|
@@ -171,7 +218,9 @@ case $( uname -s ) in | |
| esac | ||
|
|
||
| # Allow printing the url if BROWSER=echo | ||
| if [[ $BROWSER != "echo" ]]; then | ||
| if [[ $BROWSER == "echo" ]]; then | ||
| openopt='' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's this about? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's to fix BATS testing on WSL, which still doesn't work after d97b7f3. Click to expand test output:[avalera:~/git-open-clean] master*
± npm test
> [email protected] test /home/avalera/git-open-clean
> npm run unit && npm run lint:package && npm run lint:readme && npm run lint:editorconfig
> [email protected] unit /home/avalera/git-open-clean
> bats test/
✓ test environment
✓ help text
✓ invalid option
✗ gh: basic
(from function `assert_output' in file test/test_helper/bats-assert/src/assert.bash, line 239,
in test file test/git-open.bats, line 46)
`assert_output "https://github.com/user/repo"' failed
Switched to a new branch 'master'
Reset branch 'master'
-- output differs --
expected : https://github.com/user/repo
actual : Start https://github.com/user/repo
--
✗ gh: branch
(from function `assert_output' in file test/test_helper/bats-assert/src/assert.bash, line 239,
in test file test/git-open.bats, line 53)
`assert_output "https://github.com/user/repo/tree/mybranch"' failed
Switched to a new branch 'master'
Switched to a new branch 'mybranch'
-- output differs --
expected : https://github.com/user/repo/tree/mybranch
actual : Start https://github.com/user/repo/tree/mybranch
--
...The outputted URL's are correct, but prefixed with the word |
||
| else | ||
| exec &>/dev/null | ||
| fi | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think these need to be quoted just in case the user's home dir has a space in it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shellcheckdidn't catch this one 😄. I'll make the fix.