Advertisement
anden3

Fix Symlinks

Jun 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Retrieve list of all indexed files in Templates folder.
  2. $files = git ls-files -s Templates;
  3.  
  4. $fileCount = $files.Count;
  5. $currentFileIndex = 0;
  6.  
  7. foreach ($file in $files) {
  8.     $currentFileIndex++;
  9.  
  10.     # Extract file mode and path of file.
  11.     $rgx = [regex]::match($file, '^(\d{6}) \w{40} \d+\t(.+)$').Groups;
  12.  
  13.     # Check if file mode indicates that this should be a symbolic link. If not, skip it.
  14.     if ($rgx[1].Value -ne "120000") {
  15.         continue;
  16.     }
  17.  
  18.     # Resolve the path of the file.
  19.     $destPath = Resolve-Path $rgx[2].Value;
  20.  
  21.     # Check if file is already a symlink, and if so skip it.
  22.     if (Get-Item $destPath | Where-Object { $_.Attributes -match "ReparsePoint"} ) {
  23.         continue;
  24.     }
  25.  
  26.     # Get the target of the symlink.
  27.     $relPath = Get-Content $destPath;
  28.  
  29.     # Combine the file path and the relative target path to get an absolute path to the target file.
  30.     $source = Join-Path (Split-Path $destPath) $relPath -Resolve;
  31.  
  32.     # Move to the parent folder of the symlink.
  33.     Push-Location (Split-Path $destPath);
  34.  
  35.     # Get a working relative path to the target.
  36.     $relPath = Resolve-Path -Relative $relPath;
  37.  
  38.     # Delete the broken symlink.
  39.     Remove-Item $destPath;
  40.  
  41.     # Create a new symlink.
  42.     if (!(Get-Item $source).PSIsContainer) {
  43.         cmd /q /c mklink "$($destPath)" "$($relPath)" | Out-Null;
  44.     }
  45.     else {
  46.         cmd /q /c mklink /d "$($destPath)" "$($relPath)" | Out-Null;
  47.     }
  48.  
  49.     # Return to previous working directory.
  50.     Pop-Location;
  51.  
  52.     # Tell Git to ignore the file change.
  53.     git update-index --assume-unchanged $destPath;
  54.  
  55.     # Show progress.
  56.     Write-Host -NoNewline "$($currentFileIndex) out of $($fileCount) symlinks fixed.`r";
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement