124 lines
5.2 KiB
PowerShell
124 lines
5.2 KiB
PowerShell
# Brings the main folder up to the branch the work is done on (TASK-062).
|
|
# Run it through sync-main-folder.bat.
|
|
#
|
|
# It only ever moves forward (fast-forward), and it never deletes: a file that is in the way is
|
|
# moved into .git\sync-backup\<time>\ and the place is named on screen.
|
|
#
|
|
# This file is saved as UTF-8 with a byte order mark. Windows PowerShell reads a script without
|
|
# one in the system's own code page, and the Korean messages would come out garbled.
|
|
param(
|
|
[string]$Branch = 'claude/app-features-purpose-c45235',
|
|
[string]$Main = 'D:\Android\UBGitea'
|
|
)
|
|
|
|
$ErrorActionPreference = 'Continue'
|
|
# File names come from git as UTF-8; without this a Korean file name is not found again.
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
function Stop-Sync([string]$message) {
|
|
Write-Host ''
|
|
Write-Host "[중단] $message" -ForegroundColor Yellow
|
|
Write-Host ''
|
|
exit 1
|
|
}
|
|
|
|
function Invoke-Git {
|
|
# Quietly: what matters to the caller is whether it worked.
|
|
& git -C $Main -c core.quotepath=false @args 2>$null
|
|
}
|
|
|
|
Write-Host ''
|
|
Write-Host ' 메인 폴더 동기화'
|
|
Write-Host " 대상 폴더 : $Main"
|
|
Write-Host " 가져올 작업 : $Branch"
|
|
Write-Host ''
|
|
|
|
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { Stop-Sync 'git 을 찾을 수 없습니다. Git 이 설치되어 있는지 확인해 주세요.' }
|
|
if (-not (Test-Path (Join-Path $Main '.git'))) { Stop-Sync "$Main 은 Git 저장소가 아닙니다." }
|
|
Invoke-Git rev-parse --verify --quiet "$Branch^{commit}" | Out-Null
|
|
if ($LASTEXITCODE -ne 0) { Stop-Sync "$Branch 브랜치가 없습니다." }
|
|
|
|
# A change to a tracked file is someone's work in progress. It is not touched.
|
|
Invoke-Git diff --quiet
|
|
$changed = $LASTEXITCODE -ne 0
|
|
Invoke-Git diff --cached --quiet
|
|
if ($changed -or $LASTEXITCODE -ne 0) {
|
|
Write-Host '[중단] 메인 폴더에 저장하지 않은 수정이 있습니다. 아무것도 바꾸지 않았습니다.' -ForegroundColor Yellow
|
|
Write-Host ' 아래 파일을 커밋하거나 되돌린 뒤 다시 실행해 주세요.'
|
|
Write-Host ''
|
|
Invoke-Git status --short --untracked-files=no
|
|
Write-Host ''
|
|
exit 1
|
|
}
|
|
|
|
# Moving forward is only possible from a commit the branch already contains.
|
|
Invoke-Git merge-base --is-ancestor HEAD $Branch
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Stop-Sync '메인 폴더에 이 작업에 없는 커밋이 있어, 앞으로만 이동하는 방식으로는 합칠 수 없습니다. 아무것도 바뀌지 않았습니다.'
|
|
}
|
|
|
|
# A file lying in the folder, unknown to git, that the branch has a file of its own for. Git will
|
|
# not write over it, rightly; so it is moved aside first.
|
|
$blocking = @(Invoke-Git ls-files --others --exclude-standard | Where-Object {
|
|
Invoke-Git cat-file -e "${Branch}:$_"
|
|
$LASTEXITCODE -eq 0
|
|
})
|
|
$backup = $null
|
|
if ($blocking.Count -gt 0) {
|
|
$backup = Join-Path $Main (Join-Path '.git\sync-backup' (Get-Date -Format 'yyyyMMdd-HHmmss'))
|
|
Write-Host "[1/3] 길을 막는 파일 $($blocking.Count)개를 옮겨 둡니다."
|
|
$moved = @()
|
|
foreach ($path in $blocking) {
|
|
$from = Join-Path $Main $path
|
|
$to = Join-Path $backup $path
|
|
try {
|
|
New-Item -ItemType Directory -Force -Path (Split-Path $to) -ErrorAction Stop | Out-Null
|
|
Move-Item -LiteralPath $from -Destination $to -ErrorAction Stop
|
|
$moved += $path
|
|
Write-Host " $path"
|
|
} catch {
|
|
# All or nothing: put back what was already moved.
|
|
foreach ($done in $moved) {
|
|
Move-Item -LiteralPath (Join-Path $backup $done) -Destination (Join-Path $Main $done) -ErrorAction SilentlyContinue
|
|
}
|
|
Stop-Sync "$path 을(를) 옮길 수 없습니다. 이 파일을 연 프로그램(PowerPoint, Android Studio 등)을 닫고 다시 실행해 주세요. 아무것도 바뀌지 않았습니다."
|
|
}
|
|
}
|
|
} else {
|
|
Write-Host '[1/3] 길을 막는 파일이 없습니다.'
|
|
}
|
|
|
|
Write-Host '[2/3] 최신 작업으로 이동합니다.'
|
|
$output = & git -C $Main merge --ff-only $Branch 2>&1
|
|
if ($LASTEXITCODE -ne 0) {
|
|
foreach ($done in $blocking) {
|
|
Move-Item -LiteralPath (Join-Path $backup $done) -Destination (Join-Path $Main $done) -ErrorAction SilentlyContinue
|
|
}
|
|
$output | ForEach-Object { Write-Host " $_" }
|
|
Stop-Sync '이동하지 못했습니다. 옮겨 둔 파일은 제자리로 돌려놓았습니다.'
|
|
}
|
|
|
|
$current = Invoke-Git branch --show-current
|
|
if ($current -eq 'master') {
|
|
Write-Host ' 지금 master 에 있으므로 master 도 함께 이동했습니다.'
|
|
} else {
|
|
Invoke-Git branch -f master $Branch | Out-Null
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host ' [주의] master 를 옮기지 못했습니다. 다른 폴더에서 master 를 열어 둔 경우입니다.' -ForegroundColor Yellow
|
|
} else {
|
|
Write-Host ' master 도 같은 위치로 맞췄습니다.'
|
|
}
|
|
}
|
|
|
|
Write-Host '[3/3] 결과'
|
|
Invoke-Git log --oneline -3 | ForEach-Object { Write-Host " $_" }
|
|
Write-Host ''
|
|
if ($backup) {
|
|
Write-Host " 옮겨 둔 예전 파일: $backup"
|
|
Write-Host ' 같은 파일의 최신 버전이 들어왔으므로 보통은 필요 없습니다.'
|
|
Write-Host ''
|
|
}
|
|
Write-Host ' 완료되었습니다.' -ForegroundColor Green
|
|
Write-Host ''
|
|
exit 0
|