Skip to content

Commit be19eed

Browse files
authored
Do not report warning if submodule dir is missing (#1078)
* Do not report warning if submodule dir is missing * Update test
1 parent 726f8c7 commit be19eed

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,9 @@ public void GetSourceRoots_RepoWithoutCommitsWithSubmodules()
534534
}, warnings.Select(TestUtilities.InspectDiagnostic));
535535
}
536536

537-
[Fact]
538-
public void GetSourceRoots_RepoWithCommitsWithSubmodules()
537+
[Theory]
538+
[CombinatorialData]
539+
public void GetSourceRoots_RepoWithCommitsWithSubmodules(bool warnOnMissingCommit)
539540
{
540541
var repo = CreateRepository(
541542
commitSha: "0000000000000000000000000000000000000000",
@@ -547,16 +548,23 @@ public void GetSourceRoots_RepoWithCommitsWithSubmodules()
547548
CreateSubmodule("2", "sub/2", "http://2.com", "2222222222222222222222222222222222222222")));
548549

549550
var warnings = new List<(string, object?[])>();
550-
var items = GitOperations.GetSourceRoots(repo, remoteName: null, warnOnMissingCommit: false, (message, args) => warnings.Add((message, args)));
551+
var items = GitOperations.GetSourceRoots(repo, remoteName: null, warnOnMissingCommit, (message, args) => warnings.Add((message, args)));
551552

552553
AssertEx.Equal(new[]
553554
{
554555
$@"'{_workingDir}{s}' SourceControl='git' RevisionId='0000000000000000000000000000000000000000'",
555556
$@"'{_workingDir}{s}sub{s}2{s}' SourceControl='git' RevisionId='2222222222222222222222222222222222222222' NestedRoot='sub/2/' ContainingRoot='{_workingDir}{s}' ScmRepositoryUrl='http://github.com/sub-2'",
556557
}, items.Select(TestUtilities.InspectSourceRoot));
557558

558-
AssertEx.Equal(new[] { string.Format(Resources.SourceCodeWontBeAvailableViaSourceLink, string.Format(Resources.SubmoduleWithoutCommit, "1")) },
559-
warnings.Select(TestUtilities.InspectDiagnostic));
559+
if (warnOnMissingCommit)
560+
{
561+
AssertEx.Equal(new[] { string.Format(Resources.SourceCodeWontBeAvailableViaSourceLink, string.Format(Resources.SubmoduleWithoutCommit, "1")) },
562+
warnings.Select(TestUtilities.InspectDiagnostic));
563+
}
564+
else
565+
{
566+
Assert.Empty(warnings);
567+
}
560568
}
561569

562570
[Fact]

src/Microsoft.Build.Tasks.Git.UnitTests/GitRepositoryTests.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,7 @@ public void Submodules_Errors()
453453
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
454454

455455
var submodules = repository.GetSubmodules();
456-
AssertEx.Equal(new[]
457-
{
458-
"S10: 'sub10' 'http://github.com'",
459-
"S11: 'sub11' 'http://github.com'",
460-
"S6: 'sub6' 'http://github.com'",
461-
"S9: 'sub9' 'http://github.com'"
462-
}, submodules.Select(s => $"{s.Name}: '{s.WorkingDirectoryRelativePath}' '{s.Url}'"));
456+
Assert.Empty(submodules);
463457

464458
var diagnostics = repository.GetSubmoduleDiagnostics();
465459
AssertEx.Equal(new[]
@@ -468,12 +462,6 @@ public void Submodules_Errors()
468462
string.Format(Resources.InvalidSubmodulePath, "S1", " "),
469463
// The path of submodule 'S2' is missing or invalid: ''
470464
string.Format(Resources.InvalidSubmodulePath, "S2", ""),
471-
// Could not find a part of the path 'sub3\.git'.
472-
TestUtilities.GetExceptionMessage(() => File.ReadAllText(Path.Combine(workingDir.Path, "sub3", ".git"))),
473-
// Could not find a part of the path 'sub4\.git'.
474-
TestUtilities.GetExceptionMessage(() => File.ReadAllText(Path.Combine(workingDir.Path, "sub4", ".git"))),
475-
// Could not find a part of the path 'sub5\.git'.
476-
TestUtilities.GetExceptionMessage(() => File.ReadAllText(Path.Combine(workingDir.Path, "sub5", ".git"))),
477465
// The format of the file 'sub7\.git' is invalid.
478466
string.Format(Resources.FormatOfFileIsInvalid, Path.Combine(workingDir.Path, "sub7", ".git")),
479467
// Path specified in file 'sub8\.git' is invalid.
@@ -514,8 +502,8 @@ public void GetSubmoduleHeadCommitSha()
514502
submoduleRefsHeadsDir.CreateFile("master").WriteAllText("0000000000000000000000000000000000000000");
515503
submoduleGitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/master");
516504

517-
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
518-
Assert.Equal("0000000000000000000000000000000000000000", repository.ReadSubmoduleHeadCommitSha(submoduleWorkingDir.Path));
505+
Assert.Equal("0000000000000000000000000000000000000000",
506+
GitRepository.GetSubmoduleReferenceResolver(submoduleWorkingDir.Path)?.ResolveHeadReference());
519507
}
520508

521509
[Fact]
@@ -534,8 +522,22 @@ public void GetOldStyleSubmoduleHeadCommitSha()
534522
oldStyleSubmoduleRefsHeadDir.CreateFile("branch1").WriteAllText("1111111111111111111111111111111111111111");
535523
oldStyleSubmoduleGitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/branch1");
536524

537-
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
538-
Assert.Equal("1111111111111111111111111111111111111111", repository.ReadSubmoduleHeadCommitSha(oldStyleSubmoduleWorkingDir.Path));
525+
Assert.Equal("1111111111111111111111111111111111111111",
526+
GitRepository.GetSubmoduleReferenceResolver(oldStyleSubmoduleWorkingDir.Path)?.ResolveHeadReference());
527+
}
528+
529+
[Fact]
530+
public void GetSubmoduleHeadCommitSha_NoGitFile()
531+
{
532+
using var temp = new TempRoot();
533+
534+
var gitDir = temp.CreateDirectory();
535+
var workingDir = temp.CreateDirectory();
536+
537+
var submoduleGitDir = temp.CreateDirectory();
538+
var submoduleWorkingDir = workingDir.CreateDirectory("sub").CreateDirectory("abc");
539+
540+
Assert.Null(GitRepository.GetSubmoduleReferenceResolver(submoduleWorkingDir.Path)?.ResolveHeadReference());
539541
}
540542
}
541543
}

src/Microsoft.Build.Tasks.Git/GitDataReader/GitRepository.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,30 @@ public static GitRepository OpenRepository(GitRepositoryLocation location, GitEn
182182
=> _referenceResolver.ResolveHeadReference();
183183

184184
/// <summary>
185-
/// Reads and resolves the commit SHA of the current HEAD tip of the specified submodule.
185+
/// Creates <see cref="GitReferenceResolver"/> for a submodule located in the specified <paramref name="submoduleWorkingDirectoryFullPath"/>.
186186
/// </summary>
187187
/// <exception cref="IOException"/>
188188
/// <exception cref="InvalidDataException"/>
189-
/// <returns>Null if the HEAD tip reference can't be resolved.</returns>
190-
internal string? ReadSubmoduleHeadCommitSha(string submoduleWorkingDirectoryFullPath)
189+
/// <returns>Null if the submodule can't be located.</returns>
190+
public static GitReferenceResolver? GetSubmoduleReferenceResolver(string submoduleWorkingDirectoryFullPath)
191191
{
192192
// Submodules don't usually have their own .git directories but this is still legal.
193193
// This can occur with older versions of Git or other tools, or when a user clones one
194194
// repo into another's source tree (but it was not yet registered as a submodule).
195195
// See https://git-scm.com/docs/gitsubmodules#_forms for more details.
196196
var dotGitPath = Path.Combine(submoduleWorkingDirectoryFullPath, GitDirName);
197197

198-
var gitDirectory = Directory.Exists(dotGitPath) ? dotGitPath : ReadDotGitFile(dotGitPath);
199-
if (!IsGitDirectory(gitDirectory, out var commonDirectory))
198+
var gitDirectory =
199+
Directory.Exists(dotGitPath) ? dotGitPath :
200+
File.Exists(dotGitPath) ? ReadDotGitFile(dotGitPath) : null;
201+
202+
if (gitDirectory == null || !IsGitDirectory(gitDirectory, out var commonDirectory))
200203
{
201204
return null;
202205
}
203206

204-
var resolver = new GitReferenceResolver(gitDirectory, commonDirectory);
205-
return resolver.ResolveHeadReference();
206-
}
207+
return new GitReferenceResolver(gitDirectory, commonDirectory);
208+
}
207209

208210
private string GetWorkingDirectory()
209211
=> WorkingDirectory ?? throw new InvalidOperationException(Resources.RepositoryDoesNotHaveWorkingDirectory);
@@ -263,7 +265,15 @@ void reportDiagnostic(string diagnostic)
263265
string? headCommitSha;
264266
try
265267
{
266-
headCommitSha = ReadSubmoduleHeadCommitSha(fullPath);
268+
var resolver = GetSubmoduleReferenceResolver(fullPath);
269+
if (resolver == null)
270+
{
271+
// If we can't locate the submodule directory then it won't have any source files
272+
// and we can safely ignore the submodule.
273+
continue;
274+
}
275+
276+
headCommitSha = resolver.ResolveHeadReference();
267277
}
268278
catch (Exception e) when (e is IOException or InvalidDataException)
269279
{

src/Microsoft.Build.Tasks.Git/GitOperations.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ public static ITaskItem[] GetSourceRoots(GitRepository repository, string? remot
280280
var commitSha = submodule.HeadCommitSha;
281281
if (commitSha == null)
282282
{
283-
logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink,
284-
new[] { string.Format(Resources.SubmoduleWithoutCommit, new[] { submodule.Name }) });
283+
if (warnOnMissingCommit)
284+
{
285+
logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink,
286+
new[] { string.Format(Resources.SubmoduleWithoutCommit, new[] { submodule.Name }) });
287+
}
285288

286289
continue;
287290
}

0 commit comments

Comments
 (0)