ToggleGitTracking.ps1 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. param (
  2. [Parameter(Position=0, Mandatory=$false)]
  3. [ValidateSet("t", "u", "d")]
  4. [string]$option
  5. )
  6. # Get the current directory
  7. $currentDir = Get-Location
  8. # Define target directories
  9. $targetDir = "components\wifi-manager\webapp"
  10. $distDir = "$targetDir\dist"
  11. # Get list of files from the 'dist' directory
  12. $fsFiles = Get-ChildItem -Recurse $distDir -File | ForEach-Object {
  13. $_.FullName.Substring($currentDir.Path.Length + 1).Replace("\", "/")
  14. }
  15. # Define additional files to include
  16. $additionalFiles = @("webpack.c", "webpack.h", "webapp.cmake")
  17. # Check if additional files exist in $targetDir and format them
  18. $additionalFilesFormatted = @()
  19. Get-ChildItem $targetDir -File | ForEach-Object {
  20. if ($additionalFiles -contains $_.Name) {
  21. $formatted = $_.FullName.Substring($currentDir.Path.Length + 1).Replace("\", "/")
  22. $additionalFilesFormatted += $formatted
  23. Write-Host "Found $formatted"
  24. }
  25. }
  26. # Get list of files from the Git index
  27. $indexFiles = git ls-files -s $distDir | ForEach-Object {
  28. ($_ -split "\s+")[3]
  29. }
  30. # Combine and remove duplicates
  31. $allFiles = $fsFiles + $additionalFilesFormatted + $indexFiles | Sort-Object -Unique
  32. # ... (previous code remains unchanged)
  33. # Apply the git command based on the option
  34. $allFiles | ForEach-Object {
  35. $relativePath = $_
  36. $isInIndex = $indexFiles -contains $relativePath
  37. if ($null -eq $option) {
  38. $gitStatus = & git status --porcelain -- $relativePath
  39. if ($gitStatus) {
  40. $status = ($gitStatus -split "\s")[0]
  41. Write-Host "$relativePath has Git status: $status"
  42. } else {
  43. Write-Host "$relativePath is not tracked"
  44. }
  45. }
  46. elseif ($isInIndex) {
  47. if ($option -eq "d") {
  48. $resetResult = & git reset -- $relativePath 2>&1
  49. if ($resetResult -match 'error:') {
  50. Write-Host "Error resetting ${relativePath}: $resetResult"
  51. continue
  52. }
  53. $checkoutResult = & git checkout -- $relativePath 2>&1
  54. if ($checkoutResult -match 'error:') {
  55. Write-Host "Error checking out ${relativePath}: $checkoutResult"
  56. continue
  57. }
  58. Write-Host "Discarded changes in $relativePath"
  59. }
  60. # ... (rest of the code remains unchanged)
  61. }
  62. # else {
  63. # # if ($option -eq "d") {
  64. # # Remove-Item -Path $relativePath -Force
  65. # # Write-Host "Removed untracked file $relativePath"
  66. # # } else {
  67. # # Write-Host "File $relativePath is not tracked."
  68. # # }
  69. # }
  70. else {
  71. if ($option -eq "t") {
  72. git add $relativePath
  73. git update-index --no-skip-worktree $relativePath
  74. Write-Host "Started tracking changes in $relativePath"
  75. } else {
  76. Write-Host "File $relativePath is not tracked."
  77. }
  78. }
  79. }