Skip to content

Commit e52690a

Browse files
authored
Add the darc vmr diff --name-only option (#4880)
Adds a new option that only lists the differing file names dotnet/source-build#5201
1 parent 9c4d990 commit e52690a

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/Microsoft.DotNet.Darc/Darc/Operations/VirtualMonoRepo/VmrDiffOperation.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ await PrepareReposAsync(repo2, repo1, tmpPath) :
4646
await PrepareReposAsync(repo1, repo2, tmpPath);
4747

4848
IReadOnlyCollection<string> exclusionFilters = await GetDiffFilters(tmpVmrProductRepo / ".." / "..", repo1.IsVmr ? repo1.Ref : repo2.Ref, mapping);
49-
await AddRemoteAndGenerateDiffAsync(tmpProductRepo, tmpVmrProductRepo, repo2.Ref, exclusionFilters);
49+
await GenerateDiff(tmpProductRepo, tmpVmrProductRepo, repo2.Ref, exclusionFilters);
5050
}
5151
finally
5252
{
@@ -242,7 +242,7 @@ private async Task VerifyInput(Repo repo1, Repo repo2)
242242
private async Task<bool> IsRepoVmrAsync(string uri, string branch)
243243
=> await gitRepoFactory.CreateClient(uri).IsRepoVmrAsync(uri, branch);
244244

245-
private async Task AddRemoteAndGenerateDiffAsync(string repo1, string repo2, string repo2Branch, IReadOnlyCollection<string> filters)
245+
private async Task GenerateDiff(string repo1, string repo2, string repo2Branch, IReadOnlyCollection<string> filters)
246246
{
247247
string remoteName = Guid.NewGuid().ToString();
248248

@@ -285,10 +285,52 @@ await processManager.ExecuteGit(repo1, [
285285
includeAdditionalMappings: false,
286286
CancellationToken.None);
287287

288-
// If tmpDirectory is not null, it means the output path was not provided, so we just want to
289-
// print out the whole diff
290-
if (!string.IsNullOrEmpty(tmpDirectory))
288+
try
289+
{
290+
await OutputDiff(patches);
291+
}
292+
finally
293+
{
294+
if (!string.IsNullOrEmpty(tmpDirectory))
295+
{
296+
fileSystem.DeleteDirectory(tmpDirectory, true);
297+
}
298+
}
299+
}
300+
301+
private async Task OutputDiff(List<VmrIngestionPatch> patches)
302+
{
303+
if (options.NameOnly)
304+
{
305+
var files = new List<UnixPath>();
306+
307+
// For name-only mode, we'll print the filenames directly from the git patch summary lines
308+
foreach (var patch in patches)
309+
{
310+
files.AddRange(await patchHandler.GetPatchedFiles(patch.Path, CancellationToken.None));
311+
}
312+
313+
var list = files
314+
.Select(f => f.Path)
315+
.OrderBy(f => f);
316+
317+
// If the output path was provided, the list will be stored there
318+
// Otherwise we want to print it
319+
if (string.IsNullOrEmpty(options.OutputPath))
320+
{
321+
foreach (var file in list)
322+
{
323+
Console.WriteLine(file);
324+
}
325+
}
326+
else
327+
{
328+
await File.WriteAllLinesAsync(options.OutputPath, list);
329+
}
330+
}
331+
else
291332
{
333+
// For regular diff mode, we print the full diff content
292334
foreach (var patch in patches)
293335
{
294336
using FileStream fs = new(patch.Path, FileMode.Open, FileAccess.Read);
@@ -299,7 +341,6 @@ await processManager.ExecuteGit(repo1, [
299341
Console.WriteLine(line);
300342
}
301343
}
302-
fileSystem.DeleteDirectory(tmpDirectory, true);
303344
}
304345
}
305346

src/Microsoft.DotNet.Darc/Darc/Options/VirtualMonoRepo/VmrDiffOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.DotNet.Darc.Operations.VirtualMonoRepo;
66

77
namespace Microsoft.DotNet.Darc.Options.VirtualMonoRepo;
8+
89
[Verb("diff", HelpText = "Diffs the VMR and the product repositories. Outputs the diff to stdout, "
910
+ "or saves it to a patch file (or multiple if patch > 1 GB), if --output-path is provided")]
1011
internal class VmrDiffOptions : VmrCommandLineOptions<VmrDiffOperation>
@@ -17,4 +18,7 @@ internal class VmrDiffOptions : VmrCommandLineOptions<VmrDiffOperation>
1718
"where remote can be a local path or remote URI. Alternatively, only one target can be provided in " +
1819
"which case current directory will be used as the source for the diff")]
1920
public string Repositories { get; set; }
21+
22+
[Option("name-only", Required = false, HelpText = "Only list differing files without the diffs")]
23+
public bool NameOnly { get; set; }
2024
}

src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrPatchHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ public async Task ApplyPatch(
293293
/// <summary>
294294
/// Resolves a list of all files that are part of a given patch diff.
295295
/// </summary>
296-
/// <param name="repoPath">Path (to the repo) the patch applies onto</param>
297296
/// <param name="patchPath">Path to the patch file</param>
298297
/// <returns>List of all files (paths relative to repo's root) that are part of a given patch diff</returns>
299298
public async Task<IReadOnlyCollection<UnixPath>> GetPatchedFiles(string patchPath, CancellationToken cancellationToken)
@@ -305,7 +304,7 @@ public async Task<IReadOnlyCollection<UnixPath>> GetPatchedFiles(string patchPat
305304
return files;
306305
}
307306

308-
var result = await _processManager.ExecuteGit(_vmrInfo.VmrPath, ["apply", "--numstat", patchPath], cancellationToken: cancellationToken);
307+
var result = await _processManager.ExecuteGit(_vmrInfo.TmpPath, ["apply", "--numstat", patchPath], cancellationToken: cancellationToken);
309308
result.ThrowIfFailed($"Failed to enumerate files from a patch at `{patchPath}`");
310309

311310
foreach (var line in result.StandardOutput.Split(Environment.NewLine))

0 commit comments

Comments
 (0)