File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ local M = {}
22
33local  job  =  require (" plenary.job" 
44local  path  =  require (" plenary.path" 
5+ local  ssh  =  require (" gitlinker.ssh" 
56
67--  wrap the git command to do the right thing always
78local  function  git (args , cwd )
@@ -69,6 +70,8 @@ local function strip_protocol(uri, errs)
6970
7071  local  stripped_uri  =  uri :match (protocol_schema  ..  " (.+)$" 
7172    or  uri :match (ssh_schema  ..  " (.+)$" 
73+     or  ssh .fix_hostname (uri )
74+ 
7275  if  not  stripped_uri  then 
7376    table.insert (
7477      errs ,
Original file line number Diff line number Diff line change 1+ local  M  =  {}
2+ 
3+ local  job  =  require (" plenary.job" 
4+ 
5+ --  wrap the ssh command to do the right thing always
6+ local  function  ssh (args )
7+   local  output 
8+   local  p  =  job :new ({
9+     command  =  " ssh" 
10+     args  =  args ,
11+   })
12+   p :after_success (function (j )
13+     output  =  j :result ()
14+   end )
15+   p :sync ()
16+   return  output  or  {}
17+ end 
18+ 
19+ local  function  as_table (data )
20+   local  result  =  {}
21+   for  _ , line  in  ipairs (data ) do 
22+     for  key , value  in  string.gmatch (line , " (%w+)%s+(.*)" do 
23+       result [key ] =  value 
24+     end 
25+   end 
26+   return  result 
27+ end 
28+ 
29+ local  function  get_configuration (alias )
30+   local  configuration  =  ssh ({ " -G" alias  })
31+   return  as_table (configuration )
32+ end 
33+ 
34+ local  function  get_hostname (alias )
35+   return  get_configuration (alias )[" hostname" 
36+ end 
37+ 
38+ ---  Fixes aliased remote uri using the hostname set in ssh config.
39+ --  In some cases, the user can create an alias for a given user/hostname.
40+ --  So this function replaces the aliased name with the hostname set in ssh
41+ --  config.
42+ --  @params uri Remote uri from which alias will be replaced
43+ --  @return  uri with replaced alias or nil
44+ function  M .fix_hostname (uri )
45+   local  alias  =  string.match (uri , " ([^:]+):.*" 
46+   local  hostname  =  get_hostname (alias )
47+ 
48+   if  alias  ==  hostname  then 
49+     return  nil 
50+   end 
51+ 
52+   return  string.gsub (uri , alias , hostname )
53+ end 
54+ 
55+ return  M 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments