Created
July 6, 2017 03:48
-
-
Save jfmherokiller/4a3accb6fdfd77ec6ebee20bed211731 to your computer and use it in GitHub Desktop.
ipfs git rehost script powershell edition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Bool]$unpack = $false, | |
[string]$existing = "", | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$GitRepo, | |
[string]$name = "" | |
) | |
function New-TemporaryDirectory { | |
$parent = [System.IO.Path]::GetTempPath() | |
[string] $name = [System.Guid]::NewGuid() | |
New-Item -ItemType Directory -Path (Join-Path $parent $name) | |
} | |
function Unpack-GitObjects | |
{ | |
if(Test-Path ".\objects\pack\*.pack") | |
{ | |
echo "Unpacking..." | |
Move-Item ".\objects\pack\*.pack" "." | |
Remove-Item ".\objects\pack\*.idx" -Force | |
$files = @(Get-ChildItem *.pack) | |
foreach ($file in $files) { | |
#workarround because powershell and piping dont want to work for me | |
cmd.exe "/c git unpack-objects < $file" | |
Remove-Item $file -Force | |
} | |
} | |
} | |
function Pack-GitObjects | |
{ | |
echo "Packing..." | |
git repack -a -d --depth=250 --window=250 | |
$files = @(Get-ChildItem ".\objects\pack\*.pack") | |
foreach ($file in $files) { | |
$newfile = (join-path $File.DirectoryName $file.BaseName) + ".keep" | |
echo $null >> $newfile | |
} | |
} | |
function Get-Existing-Ipfs-Git | |
{ | |
echo "Getting packs from $existing..." | |
$existing_str = $existing + "/objects/pack/" | |
$ipfs_pack_directory_contents = (ipfs ls "$existing_str") -split '[\n ]' | |
$pack_files = $ipfs_pack_directory_contents -imatch ".pack" | |
$idx_files = $ipfs_pack_directory_contents -imatch ".idx" | |
$keep_files = $ipfs_pack_directory_contents -imatch ".keep" | |
$files_array = $pack_files + $idx_files + $keep_files | |
foreach($file in $files_array) | |
{ | |
$full_file_path = $existing_str + $file | |
$full_repo_path = $ipfs_repo.FullName + "/objects/pack/" + $file | |
echo "writing $full_file_path to $full_repo_path" | |
cmd.exe "/c ipfs cat $full_file_path > $full_repo_path" | |
} | |
} | |
if($name.CompareTo("") -eq 0) | |
{ | |
$name = (Get-Item $GitRepo).BaseName | |
} | |
$newdirectory = New-TemporaryDirectory | |
$ipfs_tohost = $newdirectory.FullName + "\tohost" | |
$ipfs_repo = New-Item -ItemType Directory -Path ($ipfs_tohost + "\$name") | |
echo "" | |
echo "Rehosting: $name" | |
echo "" | |
#clone repo into bare repo | |
git clone --bare "$GitRepo" "$ipfs_repo" | |
#cd into repo | |
pushd $ipfs_repo | |
if ($LastExitCode -ne 0) { | |
throw "Could not clone '$GitRepo'" | |
} | |
if($existing.CompareTo("") -ne 0) | |
{ | |
if($existing.Contains("/ipfs/") -eq $true) | |
{ | |
Get-Existing-Ipfs-Git | |
} | |
if($existing.Contains("/ipfs/") -eq $false) | |
{ | |
throw "Bad --existing argument '$existing'\nIt should start with '/ipfs/'" | |
} | |
} | |
if($unpack -eq $false) | |
{ | |
Pack-GitObjects | |
} | |
if($unpack -eq $true) | |
{ | |
Unpack-GitObjects | |
} | |
echo "Updating server info..." | |
git update-server-info | |
echo "Adding to IPFS..." | |
$ipfs_hash =(ipfs add -Q -r --pin=false "$ipfs_tohost") | |
popd | |
Remove-Item $newdirectory -Force -Recurse | |
echo "done." | |
echo "" | |
echo "Repo rehosted at:" | |
echo "" | |
echo " https://ipfs.io/ipfs/$ipfs_hash/$name" | |
echo "" | |
echo "You can clone it with:" | |
echo "" | |
echo " git clone https://ipfs.io/ipfs/$ipfs_hash/$name" | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment