Skip to content

Add default behaviour for darc vmr diff #5183

@premun

Description

@premun

Context

We have a command darc vmr diff which needs to be told exactly against which repo it should diff. When only one repo is supplied, it uses the cwd as the other repo.
This means we can call it from the VMR and diff against other repos.

We use this command to diff the current VMR commit against all the repos and their SHAs currently synced in the VMR (by going to the source-manifest.json and getting the SHAs to compare against).

We have a following script to do that:

#!/usr/bin/env pwsh

<#
.SYNOPSIS
    Runs darc vmr diff for each repository in source-manifest.json
.DESCRIPTION
    This script parses the source-manifest.json file and runs 'darc vmr diff --name-only'
    for each repository, displaying the output in separate blocks for easy identification.
    If a specific repository is specified, it will run a full diff (without --name-only) for that repository only.
.PARAMETER RepoName
    Optional. Name of a specific repository to run full diff on. If specified, only this repository will be processed with full diff output.
.EXAMPLE
    .\run-vmr-diff.ps1
.EXAMPLE
    .\run-vmr-diff.ps1 -ManifestPath "d:\repos\dotnet\src\source-manifest.json"
.EXAMPLE
    .\run-vmr-diff.ps1 -RepoName "arcade"
#>

param(
    [Parameter(Mandatory = $false)]
    [string]$RepoName = $null
)

$ManifestPath = ".\src\source-manifest.json"

# Check if the manifest file exists
if (-not (Test-Path $ManifestPath)) {
    Write-Error "Source manifest file not found: $ManifestPath"
    exit 1
}

# Check if darc command is available
try {
    $null = Get-Command darc -ErrorAction Stop
}
catch {
    Write-Error "darc command not found. Please ensure darc is installed and available in PATH."
    exit 1
}

Write-Host "Loading source manifest from: $ManifestPath" -ForegroundColor Green
Write-Host ("=" * 80) -ForegroundColor Gray

try {
    # Load and parse the JSON file
    $manifestContent = Get-Content $ManifestPath -Raw | ConvertFrom-Json

    if (-not $manifestContent.repositories) {
        Write-Error "No 'repositories' array found in the manifest file."
        exit 1
    }

    if ($RepoName) {
        foreach ($repo in $manifestContent.repositories) {
            if ($repo.path -ieq $RepoName) {
                $diffArg = "$($repo.remoteUri):$($repo.commitSha)"

                Write-Host "Diffing $($repo.path) / $($repo.commitSha)" -ForegroundColor Yellow
                Write-Host ("-" * 80) -ForegroundColor Gray

                try {
                    # Run the darc vmr diff command without --name-only for full diff
                    darc vmr diff $diffArg 2>&1

                    if ($LASTEXITCODE -eq 0) {
                        Write-Host "(No differences found)" -ForegroundColor DarkGray
                    }
                }
                catch {
                    Write-Host "ERROR: Failed to execute darc command: $($_.Exception.Message)" -ForegroundColor Red
                }

                Write-Host ""
                Write-Host ("=" * 80) -ForegroundColor Gray
                Write-Host ""
                exit 0
            }
        }
        Write-Error "Repository with path '$RepoName' not found in the manifest."
        exit 1
    }

    $totalRepos = $manifestContent.repositories.Count
    Write-Host "Found $totalRepos repositories in the manifest." -ForegroundColor Cyan
    Write-Host ""

    $currentRepo = 0

    foreach ($repo in $manifestContent.repositories) {
        $currentRepo++

        # Create the diff argument in the format remoteUri:commitSha
        $diffArg = "$($repo.remoteUri):$($repo.commitSha)"

        Write-Host "[$currentRepo/$totalRepos] Diffing $($repo.path) / $($repo.commitSha)" -ForegroundColor Yellow
        Write-Host ("-" * 80) -ForegroundColor Gray

        try {
            # Run the darc vmr diff command
            darc vmr diff --name-only $diffArg 2>&1

            if ($LASTEXITCODE -eq 0) {
                Write-Host "(No differences found)" -ForegroundColor DarkGray
            }
        }
        catch {
            Write-Host "ERROR: Failed to execute darc command: $($_.Exception.Message)" -ForegroundColor Red
        }

        Write-Host ""
        Write-Host ("=" * 80) -ForegroundColor Gray
        Write-Host ""
    }

    Write-Host "Completed processing all $totalRepos repositories." -ForegroundColor Green
}
catch {
    Write-Error "Failed to parse the manifest file: $($_.Exception.Message)"
    exit 1
}

Goal

We should not need a script like this. Instead we should make it so that if we call darc vmr diff from the VMR folder without the repo arguments, it should do what the script does - compare the contents with each repo based on the manifest.

It should ideally have two behaviours based on if we supply --name-only.

  • If we supply --name-only, print out the list of differing files in a similar manner to the script above - with sections per repo easily distinguishable. Easy to read for a human.
  • If we don't supply it, do not output anything but a full diff of files.

Further, if we call darc vmr diff from a VMR folder and supply a name of a mapping (repo), we should detect that it's not a path or URI to a repo to diff against and do what the script above does - diff that one repo only based on the rules above.

The command returns 0 or 1 just like normally - based on if there are differences or not.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions