1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
+ using System . Diagnostics . CodeAnalysis ;
5
+ using System . Linq ;
4
6
using System . Text . RegularExpressions ;
5
7
using System . Threading . Tasks ;
6
8
@@ -20,23 +22,43 @@ public class GitMissingOrNotGitRepoException : RepoInspectionException
20
22
/// <seealso cref="IRepoInspector"/>
21
23
public sealed class GitRepoInspector : IRepoInspector
22
24
{
25
+ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
26
+ // TODO: Remove all of these enumerated permutations of the old optional args in 2.0.0
27
+ [ ExcludeFromCodeCoverage ]
28
+ [ Obsolete ( "Use FromPath(path, remote, log, commandRunner)" , error : false ) ]
29
+ public static Task < GitRepoInspector > FromPath ( string path , ILogger ? log , ICommandRunner ? commandRunner )
30
+ => FromPath ( path , "origin" , log , commandRunner ?? new SystemCommandRunner ( ) ) ;
31
+ [ ExcludeFromCodeCoverage ]
32
+ [ Obsolete ( "Use FromPath(path, remote, log, commandRunner)" , error : false ) ]
33
+ public static Task < GitRepoInspector > FromPath ( string path , ILogger ? log )
34
+ => FromPath ( path , "origin" , log , new SystemCommandRunner ( ) ) ;
35
+ [ ExcludeFromCodeCoverage ]
36
+ [ Obsolete ( "Use FromPath(path, remote, log, commandRunner)" , error : false ) ]
37
+ public static Task < GitRepoInspector > FromPath ( string path , ICommandRunner ? commandRunner )
38
+ => FromPath ( path , "origin" , null , commandRunner ?? new SystemCommandRunner ( ) ) ;
39
+ [ ExcludeFromCodeCoverage ]
40
+ [ Obsolete ( "Use FromPath(path, remote, log, commandRunner)" , error : false ) ]
41
+ public static Task < GitRepoInspector > FromPath ( string path )
42
+ => FromPath ( path , "origin" , null , new SystemCommandRunner ( ) ) ;
43
+ #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
44
+
23
45
/// <summary>
24
46
/// Creates an inspector from the specified path.
25
47
/// </summary>
26
48
/// <param name="path">The path of the Git repository.</param>
49
+ /// <param name="remote">The remote endpoint.</param>
27
50
/// <param name="log">A logger for diagnostics.</param>
28
- /// <param name="commandRunner">A command runner to use. Defaults to <see cref="SystemCommandRunner"/> if null is given. </param>
51
+ /// <param name="commandRunner">A command runner to use.</param>
29
52
/// <exception cref="GitMissingOrNotGitRepoException">Thrown if the path is not a Git repository.</exception>
30
53
/// <returns>A task containing the Git repo inspector.</returns>
31
- public static async Task < GitRepoInspector > FromPath ( string path , ILogger ? log = null , ICommandRunner ? commandRunner = null )
54
+ public static async Task < GitRepoInspector > FromPath ( string path , string remote , ILogger ? log , ICommandRunner commandRunner )
32
55
{
33
- commandRunner ??= new SystemCommandRunner ( ) ;
34
-
35
56
try
36
57
{
37
58
var ( root , _) = await commandRunner . Run ( path , "git" , new string [ ] { "rev-parse" , "--show-toplevel" } ) ;
38
59
var ret = new GitRepoInspector (
39
60
root ,
61
+ remote ,
40
62
log ,
41
63
commandRunner ) ;
42
64
await ret . CacheParents ( ) ;
@@ -60,10 +82,12 @@ public static async Task<GitRepoInspector> FromPath(string path, ILogger? log =
60
82
public string Root { get ; }
61
83
private Dictionary < Commit , Commit > CachedParents { get ; } = new ( ) ;
62
84
private ( int depth , bool shallow , Commit deepest ) ? FetchDepth { get ; set ; }
85
+ public string Remote { get ; }
63
86
64
- private GitRepoInspector ( string root , ILogger ? log , ICommandRunner commandRunner )
87
+ private GitRepoInspector ( string root , string remote , ILogger ? log , ICommandRunner commandRunner )
65
88
{
66
89
Root = root ;
90
+ Remote = remote ;
67
91
Log = log ;
68
92
CommandRunner = commandRunner ;
69
93
}
@@ -169,11 +193,9 @@ private async Task Deepen()
169
193
try
170
194
{
171
195
if ( DeepenFromCommitSupported )
172
- _ = await Git ( "fetch" , "origin" , FetchDepth . Value . deepest . Id , $ "--depth={ deltaDepth } ") ;
196
+ _ = await Git ( "fetch" , Remote , FetchDepth . Value . deepest . Id , $ "--depth={ deltaDepth } ") ;
173
197
else
174
- _ = await Git ( "fetch" , $ "--depth={ newDepth } ") ;
175
-
176
- await CacheParents ( ) ;
198
+ _ = await Git ( "fetch" , Remote , $ "--depth={ newDepth } ") ;
177
199
}
178
200
catch ( CommandException ex )
179
201
when (
@@ -226,9 +248,18 @@ private async Task CacheParents()
226
248
227
249
/// <inheritdoc/>
228
250
public async Task < Commit ? > GetParent ( Commit commit )
251
+ {
252
+ var parents = await GetParents ( commit ) ;
253
+ return parents
254
+ . Select ( p => ( Commit ? ) p )
255
+ . FirstOrDefault ( ) ;
256
+ }
257
+
258
+ /// <inheritdoc/>
259
+ public async Task < IReadOnlyList < Commit > > GetParents ( Commit commit )
229
260
{
230
261
if ( CachedParents . TryGetValue ( commit , out Commit ret ) )
231
- return ret ;
262
+ return new [ ] { ret } ;
232
263
233
264
var contents = await GetCommitObject ( commit ) ;
234
265
var parent = ParseCommitObjectParent ( contents ) ;
@@ -237,7 +268,7 @@ private async Task CacheParents()
237
268
CachedParents [ commit ] = parent . Value ;
238
269
239
270
Log ? . Verbatim ( $ "GetParent() -> { parent } ") ;
240
- return parent ;
271
+ return parent is not null ? new Commit [ ] { parent . Value } : Array . Empty < Commit > ( ) ;
241
272
}
242
273
243
274
private static readonly Regex RefsTagRegex = new Regex (
@@ -265,7 +296,7 @@ public async Task<TagContainer> GetTags(QueryTarget queryTarget)
265
296
try
266
297
{
267
298
Log ? . Verbatim ( $ "GetTags(): Reading remote tags.") ;
268
- var ( response , _) = await Git ( "ls-remote" , "--tags" ) ;
299
+ var ( response , _) = await Git ( "ls-remote" , "--tags" , Remote , "*" ) ;
269
300
270
301
foreach ( Tag tag in MatchTags ( response ) )
271
302
{
@@ -308,5 +339,11 @@ public async Task FetchTag(Tag tag, string remote)
308
339
else
309
340
await Git ( "fetch" , remote , $ "refs/tags/{ tag . Name } :refs/tags/{ tag . Name } ") ;
310
341
}
342
+
343
+ /// <inheritdoc/>
344
+ public Task FetchTag ( Tag tag )
345
+ {
346
+ return FetchTag ( tag , Remote ) ;
347
+ }
311
348
}
312
349
}
0 commit comments