title: How to match SSH config based on URL path or SSH arguments (for github deploy keys)?
body: <p>I'm trying to use multiple deploy keys for github, which means I need to match my SSH configs based on the repository name. I cannot match based on hostname, port or user because they are all the same. Only repo name and deploy keys are different.</p>
  
  <p>I know this question has been asked before but I can't find a satisfactory answer because I am not interested in adding a subdomain or user to my github ssh URL. This question is specifically on how to match a URL PATH or SSH argument or other workaround that do not involve modifying the default github ssh URL.</p>
  
  <p>The default ssh github url is <code>git@github.com:githubusername/githubreponame.git</code></p>
  
  <p>The usual SSH configs are:</p>
  
  <pre><code># repo 1
  # MATCH or HOST xxxxxxxxxxx
      HostName github.com
      User git
      IdentityFile ~/.ssh/myrepo1name_rsa
  
  # repo 2
  # MATCH or HOST xxxxxxxxxxx
      HostName github.com
      User git
      IdentityFile ~/.ssh/myrepo2name_rsa
  </code></pre>
  
  <p>As per the ssh config manual <a href="http://man7.org/linux/man-pages/man5/ssh_config.5.html" rel="noreferrer">http://man7.org/linux/man-pages/man5/ssh_config.5.html</a> you can use 2 parameters to match a config, HOST or MATCH. </p>
  
  <p>We already know that HOST cannot do it. So MATCH is the only way:</p>
  
  <blockquote>
    <p>The available criteria keywords for MATCH are:
                 canonical, final, exec, host, originalhost, user, and
                 localuser.</p>
  </blockquote>
  
  <p>From those MATCH criteria keywords, it seems none of them have access to the repo name or the SSH arguments or anything useful.</p>
  
  <p>The MATCH <code>exec</code> criteria keyword seems promising:</p>
  
  <blockquote>
    <p>The exec keyword executes the specified command under the
                 user's shell.  If the command returns a zero exit status then
                 the condition is considered true.</p>
  </blockquote>
  
  <p><code>exec</code> has access to the following variables <a href="http://man7.org/linux/man-pages/man5/ssh_config.5.html#TOKENS" rel="noreferrer">http://man7.org/linux/man-pages/man5/ssh_config.5.html#TOKENS</a>:</p>
  
  <pre><code>%h    The remote hostname.
  %i    The local user ID.
  %L    The local hostname.
  %l    The local hostname, including the domain name.
  %n    The original remote hostname, as given on the command line.
  %p    The remote port.
  %r    The remote username.
  %u    The local username.
  </code></pre>
  
  <p>None of these variables appear useful to me.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>Match exec "pwd | grep myreponame1"
      HostName github.com
      User git
      IdentityFile ~/.ssh/myreponame1_rsa
  </code></pre>
  
  <p>It is partly successful, as usually when I am using git I am inside the repo directory so <code>pwd | grep reponame</code> will exit true. But it doesn't work for cloning or if I use a different directory name. </p>
  
  <p>I am looking for a better alternative, which would probably also use <code>MATCH exec</code> in some way.</p>
  
  <p>Edit: the best solution I found so far is:</p>
  
  <pre><code>Match exec "git remote get-url origin | grep reponame || echo $REPO | grep reponame"
  </code></pre>
  
  <p>But I'm not 100% happy with it because I need to add an env variable before git clone.</p>