Skip to content

Instantly share code, notes, and snippets.

@jeanfrancoislarente
Last active June 15, 2019 04:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanfrancoislarente/61629d5b5348e432ac842a931302b808 to your computer and use it in GitHub Desktop.
Save jeanfrancoislarente/61629d5b5348e432ac842a931302b808 to your computer and use it in GitHub Desktop.
Copy contents of a git repo (branch) to another similar (not connected) repo
<#
.SYNOPSIS
Simple but useful script to copy "exported" git content from one repo (specific branch) to another repo
.DESCRIPTION
Script allows copying from two different repos (different providers, between public and private repos, etc) by creating an
archive (git archive) of a particular branch in one repo and extracting it into a cleaned destination folder
which is linked to another related but disconnected repo.
This is particularly useful when you can't fork a public repo and make it private
(when dealing perhaps with repos that leverage both public and pre-release / unpublished software)
ASSUMPTIONS
The script assumes that the project name will be either no suffix (for a GitHub repo) or have the suffix .VSTS for the VSTS repo
RESULTS
Use your favourite source control tool (Team Explorer, SourceTree, etc) to see the result, review the changes
and commit to the destination repo
.PARAMETER Branch
Name of branch you would like to copy from (Source Repository)
.PARAMETER Projects
Array of local folder / projects to process.
.PARAMETER ProjectsFolder
Name of the root folder where source control projects are stored.
.PARAMETER Source
Source repository (in this example it's either GitHub or VSTS)
.OUTPUTS
git archive created will be extracted into the projectDestinationPath folder.
.NOTES
Version: 1.0
Author: J.F. Larente
Creation Date: 6/13/2019
Purpose/Change: Initial script development
.EXAMPLE
.\copy-repo.ps1 -Branch master -Projects @("Project.Name") -Source GitHub
This will git archive the content of the master branch for local project (folder) Project.Name and copy it to Project.Name.VSTS
into the currently selected branch.
You can ensure the destination is on the correct branch by creating / switching branches before running the copy command.
To perform the reverse operation:
.\copy-repo.ps1 -Branch master -Projects @("Project.Name") -Source VSTS
This will git archive the content of the master branch for local project (folder) Project.Name and copy it to Project.Name.VSTS
For multiple projects / repos:
.\copy-repo.ps1 -Branch master -Projects @("Project.Name", "Project1.Name") -Source GitHub
Same assumptions apply with naming convention. Script will iterate through all projects specified.
#>
Param(
[string]$Branch = "develop",
[string[]]$Projects = @("Demo.Platform"),
[string]$ProjectsFolder = "C:\projects",
[ValidateSet('GitHub', 'VSTS')]
[Parameter(Mandatory = $true)]
[string]$Source
)
set-alias sz "$env:ProgramFiles\7-zip\7z.exe"
# Script assumes GitHub <-> VSTS although this can be any two repos.
# .VSTS could be changed to .INTERNAL for example.
if ($Source -eq "VSTS") {
$sourcePostFix = ".VSTS"
}
else {
$destinationPostFix = ".VSTS"
}
foreach ($project in $Projects) {
# Set the source and destination paths
$projectSourcePath = (Join-Path $ProjectsFolder ( $project + $sourcePostFix))
$projectDestinationPath = (Join-Path $ProjectsFolder ( $project + $destinationPostFix))
# Clean existing folder first
Set-Location $projectDestinationPath
$exclude = @(".*", "other-file-pattern-to-exclude") #Excludes anything that starts with a . (important .git, .gitignore, etc)
Get-ChildItem -Exclude $exclude | Remove-item -Recurse -Force #deletes all items except the exclude list from destination repo
# Create an archive of the desired branch from the source repository
Set-Location $projectSourcePath
Write-Host ("Archiving {0} - {1} branch" -f $project, $Branch) -ForegroundColor Green
$zipFileName = ($projectSourcePath + ".zip")
git fetch
git checkout $Branch
git pull
git archive --format=zip -o $zipFileName $Branch
# Extract the contents of the archive to the destination directory
Write-Host ("Extracting {0} to {1}" -f $zipFileName, $projectDestinationPath) -ForegroundColor Green
sz x $zipFileName -o"$($projectDestinationPath)" -r -y -aoa -x!".gitignore"
# Cleanup
Remove-item $zipFileName
Write-Host ("Copy of {0} Completed" -f $project)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment