GitとSVNの並行運用のため、Gitで作業 → まとまりをSVNへ反映 という手順をpowershellで書いた。
#最終バージョンを取得する
$lastUpdate = Get-Content -Path ($folderPath+"lastGitVersion.txt")
#更新ファイルリストを取得し、svnのフォルダファイルを上書きする
git diff head $lastUpdate --name-only | %{ CopyWithDirectory -sourcePath ($gitRoot+"/"+$_) -destinationPath ($folderPath+"/"+$_) }
#最終バージョンを上書きする
$a = (git log --oneline | Select-Object -first 1).Split("");
$a[0]
set-content -path ($folderPath+"lastGitVersion.txt") -value $a[0]
SVNフォルダのルート直下にlastGitVersion.txtファイルを置いておき、ここに最後に反映させたコミット番号を書き込んでおく。
ここで、フォルダごとコピペにてこずってしまう。そのままCopy-Itemでできればいいのだが、これだと新規で追加したファイルを追加することができない。
間違いなく同じ問題にあたった人がいるはずなのだが、うまくみつからなかったので車輪の発明をしてしまう。
方法としては、ファイルが存在しない場合、親ディレクトリをたどって存在するまでディレクトリ名を配列に入れておき、そのあと、配列にある分ディレクトリを追加するというもの。
ここで、ファイルパスを制御する Split-Pathコマンドが上手く使えればいいのだが、戻り値を文字列として取得して云々が上手くできずに、結局、パスから親ディレクトリ名を取得する関数も必要になり、時間がかかってしまった。
パイプラインをちゃんと理解できていないのが根本問題。
#ファイルパスの最後(ファイル名またはフォルダ名)を取得すする
function GetParentDirectory{
param(
[string]$path
)
$retArr = $path.Split("\")
return $retArr[$retArr.Length-1]
}
function CopyWithDirectory {
param (
[string]$sourcePath,
[string]$destinationPath
)
$bufPath1 = $destinationPath
$arr = @()
$destinationPath.Replace("/","\")
$parentDirectory = GetParentDirectory -path $bufPath1
$bufPath2 = $bufPath1.Replace("\"+$parentDirectory, "")
$ret = Test-Path $bufPath2
if ( $ret -eq $false ){
while( $ret -eq $false ){
$parentDirectory = GetParentDirectory -path $bufPath2
$bufPath1 = $bufPath2.Replace("\"+$parentDirectory, "")
$arr += $parentDirectory
$parentDirectory = GetParentDirectory -path $bufPath1
$bufPath2 = $bufPath1.Replace("\"+$parentDirectory, "")
$ret = Test-Path $bufPath2
}
#echo $arr
for($i = $arr.Length - 1; $i -gt -1;$i--){
New-Item -path $bufPath1 -name $arr[$i] -type directory
$bufPath1 = $bufPath1 + $arr[$i]
}
}
Copy-Item -Path $sourcePath -Destination (Split-Path ($destinationPath) -Parent) -Force
}