-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathP4ObliterateIgnoredFiles.ps1
executable file
·155 lines (124 loc) · 3.73 KB
/
P4ObliterateIgnoredFiles.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env pwsh
#
# P4ObliterateIgnoredFiles.ps1
#
# Iterate through the local files, and for every file that SHOULD be ignored,
# yet exists in the P4 depot anyway, obliterate it from P4.
#
# Pass the -y flag to actually obliterate, otherwise it will just tell you what
# it would have done if you had passed the -y flag.
#
# Note that you need to have permission to obliterate on the P4 server for this
# to work. ($env:P4USER = "admin" will do the trick, if you have the password).
#
[CmdletBinding()]
param(
[switch] $y,
[string] $Path = "."
)
# Make sure the powershell version is good, or throw an exception
& $PSScriptRoot/PSVersionCheck.ps1
# Import the P4 helper module
Import-Module -Name $PSScriptRoot/Modules/P4.psm1
$BatchSize = 50
$IgnoredFiles = New-Object System.Collections.ArrayList
$TestFiles = New-Object System.Collections.ArrayList
function HandlePendingIgnoredFiles
{
#Write-Debug "HandlePendingIgnoredFiles $($script:IgnoredFiles.Count)"
if ($script:IgnoredFiles.Count -gt 0)
{
$result =& P4_FStat -Paths $script:IgnoredFiles 2> $null
$obliterates = New-Object System.Collections.ArrayList
foreach ($file in $script:IgnoredFiles)
{
$found = $false
foreach ($fstat in $result)
{
if ($fstat.clientFile -ieq $file)
{
$found = $true
break;
}
}
if ($found)
{
# The depot contains this file, which is supposed to be ignored.
Write-Debug "OBLITERATE: $file"
$obliterates.Add($file) > $null
}
}
# If we need to obliterate some files then do it
if ($obliterates.Count -gt 0)
{
if ($y)
{
# The "-y" switch was passed, so actually do obliterate these files
p4 obliterate -y @obliterates
}
else
{
# No "-y" switch on the command line, so DO NOT actually obliterate the files,
# instead just show which files WOULD BE obliterated with the "-y" switch.
p4 obliterate @obliterates
}
}
}
$script:IgnoredFiles.Clear()
}
function ProcessIgnoredFile
{
[CmdletBinding()]
param(
[string] $File
)
$script:IgnoredFiles.Add($File) > $null
if ($script:IgnoredFiles.Count -ge $BatchSize)
{
&HandlePendingIgnoredFiles
}
}
function ObliterateIgnoredFiles
{
[CmdletBinding()]
param(
[System.Collections.ArrayList] $Files
)
#Write-Debug "ObliterateIgnoredFiles $($Files.Count)"
$result =& P4_FilterIgnoredPaths -Paths $Files
# If there are any files that should be ignored in this batch,
# determine if they need to be obliterated
foreach ($file in $result.IgnoredPaths)
{
&ProcessIgnoredFile -File $file
}
}
function ObliterateFileIfIgnored
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true)] $File
)
process
{
if ($File -and $File.Exists)
{
$script:TestFiles.Add($File.FullName) > $null
if ($script:TestFiles.Count -ge $BatchSize)
{
&ObliterateIgnoredFiles -Files $script:TestFiles
$script:TestFiles.Clear()
}
}
}
}
Write-Debug "Processing Path: $Path"
Get-ChildItem -Path $Path -Recurse -File | ObliterateFileIfIgnored
# Finish processing the test files buffer if needed
if ($TestFiles.Count -gt 0)
{
&ObliterateIgnoredFiles -Files $TestFiles
$TestFiles.Clear()
}
# Finish processing the obliterate buffer if needed
&HandlePendingIgnoredFiles