diff --git a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs index be7d9c05e..cf77a01ce 100644 --- a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs +++ b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrDependencyTracker.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Microsoft.DotNet.DarcLib.Helpers; using Microsoft.DotNet.DarcLib.Models.VirtualMonoRepo; +using Microsoft.Extensions.Logging; #nullable enable namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; @@ -47,6 +48,7 @@ public class VmrDependencyTracker : IVmrDependencyTracker private readonly IVmrInfo _vmrInfo; private readonly IFileSystem _fileSystem; private readonly ISourceMappingParser _sourceMappingParser; + private readonly ILogger _logger; private IReadOnlyCollection? _mappings; public IReadOnlyCollection Mappings @@ -58,12 +60,14 @@ public VmrDependencyTracker( IVmrInfo vmrInfo, IFileSystem fileSystem, ISourceMappingParser sourceMappingParser, - ISourceManifest sourceManifest) + ISourceManifest sourceManifest, + ILogger logger) { _vmrInfo = vmrInfo; _sourceManifest = sourceManifest; _fileSystem = fileSystem; _sourceMappingParser = sourceMappingParser; + _logger = logger; _mappings = null; } @@ -103,6 +107,15 @@ public void UpdateDependencyVersion(VmrDependencyUpdate update) update.BarId); _fileSystem.WriteToFile(_vmrInfo.SourceManifestPath, _sourceManifest.ToJson()); + var gitInfoDirPath = _vmrInfo.VmrPath / VmrInfo.GitInfoSourcesDir; + + // Only update git-info files if the git-info directory exists + if (!_fileSystem.DirectoryExists(gitInfoDirPath)) + { + _logger.LogInformation("Skipped creating git-info files for {repo} as the git-info directory doesn't exist", update.Mapping.Name); + return; + } + // Root repository of an update does not have a package version associated with it // For installer, we leave whatever was there (e.g. 8.0.100) // For one-off non-recursive updates of repositories, we keep the previous @@ -125,13 +138,24 @@ public void UpdateDependencyVersion(VmrDependencyUpdate update) OutputPackageVersion = packageVersion, }; - gitInfo.SerializeToXml(GetGitInfoFilePath(update.Mapping)); + var gitInfoFilePath = GetGitInfoFilePath(update.Mapping); + gitInfo.SerializeToXml(gitInfoFilePath); + _logger.LogInformation("Updated git-info file {file} for {repo}", gitInfoFilePath, update.Mapping.Name); } public bool RemoveRepositoryVersion(string repo) { var hasChanges = false; + var gitInfoDirPath = _vmrInfo.VmrPath / VmrInfo.GitInfoSourcesDir; + + // Only try to delete git-info files if the git-info directory exists + if (!_fileSystem.DirectoryExists(gitInfoDirPath)) + { + _logger.LogInformation("Skipped removing git-info file for {repo} as the git-info directory doesn't exist", repo); + return hasChanges; + } + var gitInfoFilePath = GetGitInfoFilePath(repo); if (_fileSystem.FileExists(gitInfoFilePath)) { diff --git a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrManagerBase.cs b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrManagerBase.cs index fb1054aaa..66c3fb4cb 100644 --- a/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrManagerBase.cs +++ b/src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrManagerBase.cs @@ -109,9 +109,15 @@ protected async Task> UpdateRepoToRevisio var filesToAdd = new List { - VmrInfo.GitInfoSourcesDir, _vmrInfo.SourceManifestPath }; + + // Only add the git-info directory to staging if it exists + var gitInfoDirPath = _vmrInfo.VmrPath / VmrInfo.GitInfoSourcesDir; + if (_fileSystem.DirectoryExists(gitInfoDirPath)) + { + filesToAdd.Add(VmrInfo.GitInfoSourcesDir); + } await _localGitClient.StageAsync(_vmrInfo.VmrPath, filesToAdd, cancellationToken); diff --git a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrSyncAdditionalMappingsTest.cs b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrSyncAdditionalMappingsTest.cs index 188ad3a5d..d95387ebc 100644 --- a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrSyncAdditionalMappingsTest.cs +++ b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrSyncAdditionalMappingsTest.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading.Tasks; using Microsoft.DotNet.DarcLib.Helpers; using Microsoft.DotNet.DarcLib.Models.VirtualMonoRepo; @@ -22,7 +23,6 @@ internal class VmrSyncAdditionalMappingsTest : VmrTestsBase public async Task NonSrcContentIsSyncedTest() { // Initialize the repo - await InitializeRepoAtLastCommit(Constants.ProductRepoName, ProductRepoPath); var expectedFilesFromRepos = new List @@ -38,6 +38,9 @@ public async Task NonSrcContentIsSyncedTest() expectedFilesFromRepos ); + // The git-info files are not created in this test so we should not expect them + expectedFiles = [..expectedFiles.Where(f => !f.Path.Contains(new NativePath(VmrInfo.GitInfoSourcesDir)))]; + CheckDirectoryContents(VmrPath, expectedFiles); await GitOperations.CheckAllIsCommitted(VmrPath); @@ -69,6 +72,9 @@ protected override async Task CopyVmrForCurrentTest() { CopyDirectory(VmrTestsOneTimeSetUp.CommonVmrPath, VmrPath); + // In this test, we remove the git-info directory to see that it does not get created + Directory.Delete(VmrPath / "prereqs" / "git-info"); + var sourceMappings = new SourceMappingFile() { Mappings = diff --git a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsBase.cs b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsBase.cs index d4c617edf..878c13dbc 100644 --- a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsBase.cs +++ b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsBase.cs @@ -63,6 +63,10 @@ public async Task Setup() await CopyReposForCurrentTest(); await CopyVmrForCurrentTest(); + + // Ensure git-info directory exists for tests + Directory.CreateDirectory(VmrPath / "prereqs"); + Directory.CreateDirectory(VmrPath / "prereqs" / "git-info"); ServiceProvider = CreateServiceProvider().BuildServiceProvider(); ServiceProvider.GetRequiredService().VmrUri = VmrPath; diff --git a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsOneTimeSetUp.cs b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsOneTimeSetUp.cs index 8deabaccc..11e5c137f 100644 --- a/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsOneTimeSetUp.cs +++ b/test/Microsoft.DotNet.Darc.VirtualMonoRepo.E2E.Tests/VmrTestsOneTimeSetUp.cs @@ -45,6 +45,7 @@ public async Task OneTimeSetUp() Directory.CreateDirectory(TestsDirectory / Constants.VmrName); Directory.CreateDirectory(TestsDirectory / Constants.VmrName / VmrInfo.SourcesDir); + Directory.CreateDirectory(TestsDirectory / Constants.VmrName / VmrInfo.GitInfoSourcesDir); await _gitOperations.InitialCommit(TestsDirectory / Constants.VmrName); await CreateRepository(CommonProductRepoPath, Constants.ProductRepoName, Constants.GetRepoFileName(Constants.ProductRepoName));