Skip to content

Commit

Permalink
Ability to resolve using OpenSSH client configuration file
Browse files Browse the repository at this point in the history
So I have this setup where I use a different github account and a
different private key configured like this in my ~/.ssh/config

Host github+myorg
  IdentitiesOnly yes
  HostName github.com
  IdentityFile ~/.ssh/myorg_id_rsa

This PR provides the ability to support such configuration. I have
disabled it by default since this is not a common setup most peoples
have.
  • Loading branch information
psibi committed Mar 3, 2024
1 parent aded958 commit df41160
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion git-link.el
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ See its docs."
:type 'boolean
:group 'git-link)

(defcustom git-link-consider-ssh-config nil
"Consider ssh configuration file for resolving hostnames."
:type 'boolean
:group 'git-link)

(defcustom git-link-remote-alist
'(("git.sr.ht" git-link-sourcehut)
("codeberg.org" git-link-codeberg)
Expand Down Expand Up @@ -431,10 +436,16 @@ return (FILENAME . REVISION) otherwise nil."
(when (string-match ":" host)
(let ((parts (split-string host ":" t))
(case-fold-search t))
(string-match (concat (car parts) ":\\(" (cadr parts) "\\)/") url)
(string-match (concat (regexp-quote (car parts)) ":\\(" (cadr parts) "\\)/") url)
(setq host (car parts)
path (concat (match-string 1 url) "/" path))))


(when git-link-consider-ssh-config
(let* ((ssh-resolved-host (git-link--ssh-resolve-hostname host)))
(when ssh-resolved-host
(setq host ssh-resolved-host))))

;; Fix-up Azure SSH URLs
(when (string= "ssh.dev.azure.com" host)
(setq host "dev.azure.com")
Expand Down Expand Up @@ -471,6 +482,15 @@ return (FILENAME . REVISION) otherwise nil."

(list host path))))

(defun git-link--ssh-resolve-hostname (hostname)
"Resolve HOSTNAME using ssh client."
(let ((output (shell-command-to-string (format "ssh -G %s" hostname)))
(host nil))
(dolist (line (split-string output "\n"))
(when (string-match "^hostname \\(.*\\)" line)
(setq host (match-string 1 line))))
host))

(defun git-link--using-git-timemachine ()
(and (boundp 'git-timemachine-revision)
git-timemachine-revision))
Expand Down

0 comments on commit df41160

Please sign in to comment.