Skip to content

Commit

Permalink
Fixes faulty LFS file check error
Browse files Browse the repository at this point in the history
We need files to be restored from LFS before we can use them - otherwise they are empty!

The previous check I had could not distinguish between an empty file and file that had been modified.

I've now written a far more detailed script that should be able to handle new and modified files.

It also now skips running if it was run withing the last hour
  • Loading branch information
atruskie committed Apr 24, 2020
1 parent 7e4a8e9 commit ffe546c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions AudioAnalysis.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
tests\Acoustics.Test\.runsettings = tests\Acoustics.Test\.runsettings
src\AP.CopyFiles.targets = src\AP.CopyFiles.targets
src\AP.RequireLfsAssets.targets = src\AP.RequireLfsAssets.targets
src\AP.VersionBuild.targets = src\AP.VersionBuild.targets
src\AssemblyMetadata.cs.template = src\AssemblyMetadata.cs.template
src\AssemblyMetadata.Generated.targets = src\AssemblyMetadata.Generated.targets
Expand Down
3 changes: 2 additions & 1 deletion src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
AssemblyMetadata.Generated.cs
AssemblyMetadata.Generated.targets
AssemblyMetadata.Generated.targets
.lfs_check_last_check
9 changes: 4 additions & 5 deletions src/AP.RequireLfsAssets.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

<!-- Register our task that as something to run before standard build target -->
<Target Name="APRequireLfsAssets" BeforeTargets="PrepareForBuild">
<!-- <Message Text="[APVersionBeforeBuild] Debug: $(RuntimeIdentifier)" Importance="High" /> -->
<Exec Command="git lfs ls-files" ConsoleToMSBuild="True" EchoOff="false" StandardOutputImportance="low" WorkingDirectory="$(MSBuildThisFileDirectory)..">
<Output TaskParameter="ConsoleOutput" ItemName="FileStatus" />
<PropertyGroup>
<CheckLfsCommand>pwsh –NonInteractive -noprofile -File "$(MSBuildThisFileDirectory)lfs_check.ps1"</CheckLfsCommand>
</PropertyGroup>
<Exec Command="$(CheckLfsCommand)" EchoOff="false" StandardOutputImportance="High" WorkingDirectory="$(MSBuildThisFileDirectory)..">
</Exec>
<!--<Error File="$([System.String]::new(%(FileStatus.Identity)).Substring(13).Trim())" Code="AP001" Text="Git LFS BLOB has not been restored and only contains a LFS pointer to the file." Condition="$([System.String]::new(%(FileStatus.Identity)).Contains(' - ')) And Exists('$(MSBuildThisFileDirectory)../$([System.String]::new(%(FileStatus.Identity)).Substring(13).Trim())')" ContinueOnError="ErrorAndContinue"/> -->

<!-- <Error Condition="'$(MSBuildLastTaskResult)'=='false'" Text="AP build cannot continue there are Git LFS assets that have not been restored. Please follow the instructions at https://github.com/QutEcoacoustics/audio-analysis/blob/master/CONTRIBUTING.md#AP001" /> -->
</Target>
</Project>
48 changes: 48 additions & 0 deletions src/lfs_check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env pwsh
$cache_file = "$PSScriptRoot\.lfs_check_last_check"
if ([System.IO.File]::Exists($cache_file) -and [DateTime]::Now.Subtract([System.IO.File]::GetLastWriteTime($cache_file)).TotalSeconds -lt 3600) {
Write-Output "Skipping lfs check because .lfs_check_last_check exists"
exit 0
}

$solution_root = Resolve-Path "$PSScriptRoot/.."
$slash = [System.IO.Path]::DirectorySeparatorChar

if ($null -eq (Get-Command "git-lfs" -ErrorAction SilentlyContinue )) {
Write-Output "$solution_root${slash}src${slash}AP.RequireLfsAssets.targets`:git-lfs error AP0003: git-lfs must be installed and must be available on PATH."
exit;
}

Push-Location
Set-Location $solution_root
$errored = $false
foreach($line in (git-lfs ls-files)) {
# format:
# 1da5b69f92 * tests/Fixtures/whip bird2.wav
$status = $line[11]
if ($status -eq '*') {
continue
}

if ($status -eq '-') {
$file = $line.SubString(13)
$status = git status --porcelain $file
$has_changed = ($null -ne $status)
if (-not $has_changed) {
Write-Output "$file`:git-lfs error AP0001: Git LFS BLOB has not been restored. The file is empty! It contains only a LFS pointer."
$errored = $true
}
}
}

if ($errored -eq $true) {
Write-Output "$solution_root${slash}src${slash}AP.RequireLfsAssets.targets`:git-lfs error AP0002: AP build cannot continue there are Git LFS assets that have not been restored. Please follow the instructions at https://github.com/QutEcoacoustics/audio-analysis/blob/master/CONTRIBUTING.md#AP001"
}
else {
$null >> $cache_file
}

Pop-Location


exit 0

0 comments on commit ffe546c

Please sign in to comment.