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
|
param (
[Parameter(Mandatory=$true)][string]$source,
[Parameter(Mandatory=$true)][string]$target
)
clear
Write-Host("Source is {0}" -f $source) -ForegroundColor Green
Write-Host("Target is {0}" -f $target) -ForegroundColor Green
# get current branch
$branch = & git branch --show-current | Out-String
$branch = $branch.Trim()
Write-Host("Branch is {0}" -f $branch) -ForegroundColor Green
# get commit count for file
$length = & git rev-list --count $branch $source | Out-String
$length = $length.Trim()
Write-Host("Commit count is {0}" -f $length) -ForegroundColor Green
# make patches
Write-Host("Generating patches") -ForegroundColor Yellow
$patchs = & git format-patch -$length $source | Out-String
$patchs = $patchs.Trim()
foreach ($patch in $($patchs -split "`r`n"))
{
$patch
}
# create target directory
$tgtdir = Split-Path $target | Out-String
$tgtdir = $tgtdir.Trim()
Write-Host("Creating target directory {0}" -f $tgtdir) -ForegroundColor Yellow
New-Item $tgtdir -ItemType Directory -Force | Out-Null
# apply patches, pass good syntax, add historical data, commit, cleanup
Write-Host("Applying patches") -ForegroundColor Yellow
$gitdir = $tgtdir.Substring(2).Replace("\","/")
$gitdir = $gitdir.Trim()
$dirlen = $gitdir.Split("/").Count
foreach ($patch in $($patchs -split "`r`n"))
{
<# not working
$diff = & git config diff.noprefix
& git config diff.noprefix true
#>
& git apply --directory=$gitdir -p $dirlen "$patch"
<# not working
if ([string]::IsNullOrWhiteSpace($diff))
{
& git config --unset diff.noprefix
}
else
{
& git config diff.noprefix $diff
}
#>
$hash = (Get-Content $patch)[0].Substring(5, 40)
$mail = (Get-Content $patch)[1].Substring(6)
$date = (Get-Content $patch)[2].Substring(6)
$text = (Get-Content $patch)[3].Substring(9)
$data = "NOTE: auto-magically re-imported by HAL 9000`r`nHASH: {0} (original)" -f $hash
$text = "[MIGRATED] {0}" -f $text
& git add $target
& git commit -m "$text" -m "$data" --date="$date" --author="$mail"
Remove-Item $patch
}
Write-Host("Removing original file {0}" -f $source) -ForegroundColor Yellow
& git rm $source
& git commit -m $("[MIGRATED] Delete {0}" -f $source) -m "NOTE: file deleted after being migrated with original history" $source
Write-Host("Complete") -ForegroundColor Green
|