88using GitHub . Services ;
99using GitHub . Extensions ;
1010using System . Threading . Tasks ;
11+ using GitHub . Exports ;
1112
1213namespace GitHub . Models
1314{
@@ -57,11 +58,12 @@ public void Refresh()
5758 /// Generates a http(s) url to the repository in the remote server, optionally
5859 /// pointing to a specific file and specific line range in it.
5960 /// </summary>
61+ /// <param name="linkType">Type of link to generate</param>
6062 /// <param name="path">The file to generate an url to. Optional.</param>
6163 /// <param name="startLine">A specific line, or (if specifying the <paramref name="endLine"/> as well) the start of a range</param>
6264 /// <param name="endLine">The end of a line range on the specified file.</param>
6365 /// <returns>An UriString with the generated url, or null if the repository has no remote server configured or if it can't be found locally</returns>
64- public async Task < UriString > GenerateUrl ( string path = null , int startLine = - 1 , int endLine = - 1 )
66+ public async Task < UriString > GenerateUrl ( LinkType linkType , string path = null , int startLine = - 1 , int endLine = - 1 )
6567 {
6668 if ( CloneUrl == null )
6769 return null ;
@@ -97,22 +99,24 @@ public async Task<UriString> GenerateUrl(string path = null, int startLine = -1,
9799 endLine = - 1 ;
98100 }
99101
100- return new UriString ( GenerateUrl ( CloneUrl . ToRepositoryUrl ( ) . AbsoluteUri , sha , path , startLine , endLine ) ) ;
102+ return new UriString ( GenerateUrl ( linkType , CloneUrl . ToRepositoryUrl ( ) . AbsoluteUri , sha , path , startLine , endLine ) ) ;
101103 }
102104
103105 const string CommitFormat = "{0}/commit/{1}" ;
104106 const string BlobFormat = "{0}/blob/{1}/{2}" ;
107+ const string BlameFormat = "{0}/blame/{1}/{2}" ;
105108 const string StartLineFormat = "{0}#L{1}" ;
106109 const string EndLineFormat = "{0}-L{1}" ;
107- static string GenerateUrl ( string basePath , string sha , string path , int startLine = - 1 , int endLine = - 1 )
110+ static string GenerateUrl ( LinkType linkType , string basePath , string sha , string path , int startLine = - 1 , int endLine = - 1 )
108111 {
109112 if ( sha == null )
110113 return basePath ;
111114
112115 if ( String . IsNullOrEmpty ( path ) )
113116 return String . Format ( CultureInfo . InvariantCulture , CommitFormat , basePath , sha ) ;
114117
115- var ret = String . Format ( CultureInfo . InvariantCulture , BlobFormat , basePath , sha , path . Replace ( @"\" , "/" ) ) ;
118+ var ret = String . Format ( CultureInfo . InvariantCulture , GetLinkFormat ( linkType ) , basePath , sha , path . Replace ( @"\" , "/" ) ) ;
119+
116120 if ( startLine < 0 )
117121 return ret ;
118122 ret = String . Format ( CultureInfo . InvariantCulture , StartLineFormat , ret , startLine ) ;
@@ -121,6 +125,26 @@ static string GenerateUrl(string basePath, string sha, string path, int startLin
121125 return String . Format ( CultureInfo . InvariantCulture , EndLineFormat , ret , endLine ) ;
122126 }
123127
128+ /// <summary>
129+ /// Selects the proper format for the link type, defaults to the blob url when link type is not selected.
130+ /// </summary>
131+ /// <param name="linkType">Type of link to generate</param>
132+ /// <returns>The string format of the selected link type</returns>
133+ static string GetLinkFormat ( LinkType linkType )
134+ {
135+ switch ( linkType )
136+ {
137+ case LinkType . Blame :
138+ return BlameFormat ;
139+
140+ case LinkType . Blob :
141+ return BlobFormat ;
142+
143+ default :
144+ return BlobFormat ;
145+ }
146+ }
147+
124148 /// <summary>
125149 /// Gets the local path of the repository.
126150 /// </summary>
0 commit comments