# Retrieve list of all indexed files in Templates folder.
$files = git ls-files -s Templates;
$fileCount = $files.Count;
$currentFileIndex = 0;
foreach ($file in $files) {
$currentFileIndex++;
# Extract file mode and path of file.
$rgx = [regex]::match($file, '^(\d{6}) \w{40} \d+\t(.+)$').Groups;
# Check if file mode indicates that this should be a symbolic link. If not, skip it.
if ($rgx[1].Value -ne "120000") {
continue;
}
# Resolve the path of the file.
$destPath = Resolve-Path $rgx[2].Value;
# Check if file is already a symlink, and if so skip it.
if (Get-Item $destPath | Where-Object { $_.Attributes -match "ReparsePoint"} ) {
continue;
}
# Get the target of the symlink.
$relPath = Get-Content $destPath;
# Combine the file path and the relative target path to get an absolute path to the target file.
$source = Join-Path (Split-Path $destPath) $relPath -Resolve;
# Move to the parent folder of the symlink.
Push-Location (Split-Path $destPath);
# Get a working relative path to the target.
$relPath = Resolve-Path -Relative $relPath;
# Delete the broken symlink.
Remove-Item $destPath;
# Create a new symlink.
if (!(Get-Item $source).PSIsContainer) {
cmd /q /c mklink "$($destPath)" "$($relPath)" | Out-Null;
}
else {
cmd /q /c mklink /d "$($destPath)" "$($relPath)" | Out-Null;
}
# Return to previous working directory.
Pop-Location;
# Tell Git to ignore the file change.
git update-index --assume-unchanged $destPath;
# Show progress.
Write-Host -NoNewline "$($currentFileIndex) out of $($fileCount) symlinks fixed.`r";
}
Comments