Skip to content

Instantly share code, notes, and snippets.

@chadbaldwin
Last active February 20, 2024 23:16
Show Gist options
  • Save chadbaldwin/18c4aa39e770138651241a2b9f80455a to your computer and use it in GitHub Desktop.
Save chadbaldwin/18c4aa39e770138651241a2b9f80455a to your computer and use it in GitHub Desktop.
# Run this script from within the git repo you want to copy data OUT of
$copyTo = 'C:\MyTestFolder'
$ErrorActionPreference = 'Stop'
$currBranch = git branch --show-current
$repoRoot = git rev-parse --show-toplevel
if ($LASTEXITCODE -gt 0) { return }
$decision = $Host.UI.PromptForChoice($null, "Current branch is [${currBranch}], is that correct?", ('&Yes','&No'), 0); ''
if ($decision -eq 1) {
Write-Host 'Exiting script'
return
}
git fetch -q
if (git rev-parse --symbolic-full-name '@{u}') {
if (git status -s) {
$decision = $Host.UI.PromptForChoice($null, 'Current branch has uncommited changes, okay to proceed?', ('&Yes','&No'), 0); ''
if ($decision -eq 1) {
Write-Host 'Exiting script'
return
}
}
if (git cherry) {
$decision = $Host.UI.PromptForChoice($null, 'Current branch is ahead (unpushed commits), okay to proceed?', ('&Yes','&No'), 0); ''
if ($decision -eq 1) {
Write-Host 'Exiting script'
return
}
}
if (git log HEAD..'@{u}') {
$decision = $Host.UI.PromptForChoice($null, 'Current branch is behind (remote has new commits), okay to proceed?', ('&Yes','&No'), 0); ''
if ($decision -eq 1) {
Write-Host 'Exiting script'
return
}
}
}
$copyToBranch = mkdir (Join-Path $copyTo $currBranch) -Force
if (gci -Path $copyToBranch) {
$decision = $Host.UI.PromptForChoice($null, 'Branch folder already exists, okay to clear out?', ('&Yes','&No'), 0); ''
if ($decision -eq 0) {
Write-Host 'Clearing out folder...'
rm -LiteralPath $copyToBranch -Recurse -Force
} else {
Write-Host 'Exiting script'
return
}
}
Write-Host 'Copying files to destination...'
(git diff --name-only master...) | ? { Test-Path -LiteralPath (Join-Path $repoRoot $_) } |
% {
# Create absolute file destination path
$dp = Join-Path $copyToBranch $_
# Create absolute file source path
$sp = Join-Path $repoRoot $_
# Get the destination directory path
$dir = Split-Path $dp -Parent
# Create directory path if it doesn't exist
$null = mkdir $dir -Force
# Copy file to destination
cp -LiteralPath $sp -Destination $dir
}
''; $decision = $Host.UI.PromptForChoice($null, 'Open folder?', ('&Yes','&No'), 0); ''
if ($decision -eq 0) {
Write-Host 'Clearing out folder...'
ii $copyToBranch
}
Write-Host 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment