Skip to content

Commit d9664a0

Browse files
committed
Allow relative path handling in diff validation and enhance root determination
- Updated `ValidateRedirects` method to support relative paths. - Improved handling of source directory root in diff validation. - Fixed path normalization in `IntegrationGitRepositoryTracker`.
1 parent 9bd478c commit d9664a0

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/tooling/docs-builder/Cli/DiffCommands.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@ IConfigurationContext configurationContext
2727
/// <summary>
2828
/// Validates redirect updates in the current branch using the redirect file against changes reported by git.
2929
/// </summary>
30-
/// <param name="path">The baseline path to perform the check</param>
30+
/// <param name="path"> -p, Defaults to the`{pwd}/docs` folder</param>
3131
/// <param name="ctx"></param>
3232
[SuppressMessage("Usage", "CA2254:Template should be a static expression")]
3333
[Command("validate")]
34-
public async Task<int> ValidateRedirects([Argument] string? path = null, Cancel ctx = default)
34+
public async Task<int> ValidateRedirects(string? path = null, Cancel ctx = default)
3535
{
3636
var runningOnCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));
37-
path ??= "docs";
3837

3938
await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService).StartAsync(ctx);
4039

4140
var fs = new FileSystem();
42-
var root = fs.DirectoryInfo.New(Paths.WorkingDirectoryRoot.FullName);
4341

44-
var buildContext = new BuildContext(collector, fs, fs, configurationContext, ExportOptions.MetadataOnly, root.FullName, null);
42+
var buildContext = new BuildContext(collector, fs, fs, configurationContext, ExportOptions.MetadataOnly, path, null);
4543
var sourceFile = buildContext.ConfigurationPath;
4644
var redirectFileName = sourceFile.Name.StartsWith('_') ? "_redirects.yml" : "redirects.yml";
4745
var redirectFileInfo = sourceFile.FileSystem.FileInfo.New(Path.Combine(sourceFile.Directory!.FullName, redirectFileName));
@@ -61,11 +59,20 @@ public async Task<int> ValidateRedirects([Argument] string? path = null, Cancel
6159
return collector.Errors;
6260
}
6361

64-
IRepositoryTracker tracker = runningOnCi ? new IntegrationGitRepositoryTracker(path) : new LocalGitRepositoryTracker(collector, root, path);
65-
var changed = tracker.GetChangedFiles();
62+
var root = Paths.DetermineSourceDirectoryRoot(buildContext.DocumentationSourceDirectory);
63+
if (root is null)
64+
{
65+
collector.EmitError(redirectFileInfo, $"Unable to determine the root of the source directory {buildContext.DocumentationSourceDirectory}.");
66+
await collector.StopAsync(ctx);
67+
return collector.Errors;
68+
}
69+
var relativePath = Path.GetRelativePath(root.FullName, buildContext.DocumentationSourceDirectory.FullName);
70+
_log.LogInformation("Using relative path {RelativePath} for validating changes", relativePath);
71+
IRepositoryTracker tracker = runningOnCi ? new IntegrationGitRepositoryTracker(relativePath) : new LocalGitRepositoryTracker(collector, root, relativePath);
72+
var changed = tracker.GetChangedFiles().ToArray();
6673

67-
if (changed.Any())
68-
_log.LogInformation("Found {Count} changes to files related to documentation in the current branch.", changed.Count());
74+
if (changed.Length != 0)
75+
_log.LogInformation("Found {Count} changes to files related to documentation in the current branch.", changed.Length);
6976

7077
foreach (var notFound in changed.DistinctBy(c => c.FilePath).Where(c => c.ChangeType is GitChangeType.Deleted or GitChangeType.Renamed
7178
&& !redirects.ContainsKey(c is RenamedGitChange renamed ? renamed.OldFilePath : c.FilePath)))

src/tooling/docs-builder/Tracking/IntegrationGitRepositoryTracker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Documentation.Builder.Tracking;
66

77
public class IntegrationGitRepositoryTracker(string lookupPath) : IRepositoryTracker
88
{
9-
private string LookupPath { get; } = $"{lookupPath}/";
9+
private string LookupPath { get; } = $"{lookupPath.Trim(['/', '\\'])}/";
1010
public IEnumerable<GitChange> GetChangedFiles()
1111
{
1212
var deletedFiles = Environment.GetEnvironmentVariable("DELETED_FILES") ?? string.Empty;

0 commit comments

Comments
 (0)