-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitFileDump.ps1
77 lines (60 loc) · 2.43 KB
/
GitFileDump.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
param (
[Parameter(Mandatory=$true)]
[string]$fileName,
[Parameter(Mandatory=$true)]
[int]$samplePercent,
[Parameter(Mandatory=$false)]
[string]$outputFolder = "dump"
)
# Validate percentage
if ($samplePercent -lt 1 -or $samplePercent -gt 100) {
Write-Error "Percentage must be between 1 and 100"
exit 1
}
# Create or verify output folder
if (-not (Test-Path -Path $outputFolder)) {
Write-Host "Creating output folder: $outputFolder"
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}
else {
Write-Host "Using existing folder: $outputFolder"
}
# Get file extension
$extension = [System.IO.Path]::GetExtension($fileName)
$baseFileName = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
# Get all commits for the file
$commits = git log --follow --format="%H|%ad" --date=format:"%Y%m%d_%H%M%S" -- $fileName
$commitArray = $commits -split "`n"
# Calculate number of commits to sample
$totalCommits = $commitArray.Count
$samplesToTake = [math]::Ceiling(($totalCommits * $samplePercent) / 100)
Write-Host "Total commits: $totalCommits"
Write-Host "Taking $samplesToTake samples"
# Calculate step size to evenly distribute samples
$stepSize = $totalCommits / $samplesToTake
# Process commits
for ($i = 0; $i -lt $totalCommits; $i += $stepSize) {
$index = [math]::Floor($i)
$commit = $commitArray[$index]
if ($commit -match "([^|]+)\|(.+)") {
$hash = $matches[1]
$date = $matches[2]
# Create new filename with full path
$newFileName = Join-Path -Path $outputFolder -ChildPath "${baseFileName}_${date}_${hash}${extension}"
# Checkout file from this commit
git show "$hash`:$fileName" > $newFileName
# Check if file is empty (0 bytes)
if ((Get-Item $newFileName).Length -eq 0) {
Remove-Item $newFileName
Write-Host "Deleted empty file: $newFileName"
continue
}
# Convert date string to DateTime object
# Date format is "YYYYMMDD_HHMMSS"
$dateTime = [DateTime]::ParseExact($date, "yyyyMMdd_HHmmss", $null)
# Set both LastWriteTime and CreationTime
(Get-Item $newFileName).LastWriteTime = $dateTime
(Get-Item $newFileName).CreationTime = $dateTime
Write-Host "Created $newFileName with timestamp $dateTime"
}
}