ghqでプロジェクト管理(整理)する
· 約6分
ある程度、自分なりにクローン先を指定して管理していたのですが、数が多くなって管理が難しくなってきたので、ghq を使ってみました. ここでは、その備忘録を残しておきます.
ghq はリポジトリをクローンするときに、ルールにもとづいて自動的にフォルダを生成して、そこにクローンしてくれるツールです.一貫した構成になるので、管理しやすくなりますし、ghqコマンドを使ってリストを取得することもできます.fzfと組み合わせることで、プロジェクトの行き来が楽になります.
すでにある程度、自分なりにクローン先を分別してやっていたのですが、いざ不要なものを削除するときにわかりづらいものとなっていました.
たとえば、GitHubで管理しているものとローカルに作成したベアリポジトリ(バックアップ用)の区別がつきませんでした.
そこで、ghqを導入して整理することにしました.100個以上のクローンしたプロジェクトがあったので、なるべく一括でできるようにPowerShellでスクリプトを作成しました.
前述したように GitHubで管理しているものとローカルに作成したものがあったのですが、ghqではローカルに作成したもの(ssh
やhttps
以外)は上手くいかなかったので、そこはいい感じの構成にしておきました(repo
ディレクトリの中に xxx.git
ディレクトリが置いてあるので、<ghq.root>/repo/<project>
という名前にしました).
あとは、すでにあるプロジェクトを移行します.migrate2ghq
という関数を作成してghqのフォルダ構成になるように移動するものです.
function migrate2ghq() {
param(
[Parameter(Mandatory)]
[string]$InputPath,
[switch]$DryRun
)
Push-Location
Set-Location $InputPath
Write-Host "Location: $(Get-Location)"
$url = git remote get-url origin
Write-Host "Origin : ${url}"
Pop-Location
$root = git config --global ghq.root
Write-Host "ghq.root: ${root}"
$repo = @{}
if ($url -match "^git@(?<host>.*?):(?<user>.*?)/(?<project>.*?)\.git$") {
$repo = @{
Host = $Matches.host
User = $Matches.user
project = $Matches.project
}
} elseif ($url -match "^https://(?<host>.*?)/(?<user>.*?)/(?<project>.*?)\.git$") {
$repo = @{
Host = $Matches.host
User = $Matches.user
project = $Matches.project
}
} elseif ($url -match "^(?<drive>\w+?):[/\\].*?(?<host>.*)[/\\](?<project>.+)\.git$") {
$repo = @{
Host = $Matches.host
User = ""
project = $Matches.project
}
} else {
Write-Error "Not found repository information. Couldn't migration."
return
}
# <ghq.root>/<host>/<user>/<project>
$outputPath = Join-Path $root $repo.host $repo.user $repo.project
$outputParentPath = Join-Path $root $repo.host $repo.user
if (-not(Test-Path $outputParentPath -Type Container)) {
if ($DryRun) {
Write-Host "MKDIR(DryRun): ${outputParentPath}"
} else {
New-Item -Path $outputParentPath -ItemType Directory
}
} else {
if (Test-Path $outputPath -Type Container) {
Write-Error "[${outputPath}] is Already exists."
return
}
}
if ($DryRun) {
Write-Host "Move(DryRun): ${InputPath} -> ${outputPath}"
} else {
Move-Item -LiteralPath $InputPath -Destination $outputPath
Write-Host "Move: ${InputPath} -> ${outputPath}"
}
}
使い方は次のようになっています:
PS > migrate2ghq hoge -DryRun
PS > migrate2ghq hoge