Created
February 10, 2024 13:56
-
-
Save book000/57c59eff64f5ff14facffce423838e2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GitHubリポジトリをクローンするPowerShellスクリプト | |
# 関数: GitHubリポジトリのURLが有効かどうかを確認する | |
function Test-GitHubRepositoryURL { | |
param ( | |
[string]$URL | |
) | |
if ($URL -notmatch '^https:\/\/github\.com\/.+\/.+') { | |
Write-Host "エラー: 正しいGitHubリポジトリのURLを入力してください。" -ForegroundColor Red | |
exit 1 | |
} | |
return $true | |
} | |
# 関数: リポジトリのフォーク元を設定する | |
function Set-UpstreamRemote { | |
param ( | |
[string]$Folder, | |
[string]$UpstreamURL | |
) | |
$currentDirectory = Get-Location | |
Set-Location -Path $Folder | |
git remote add upstream $UpstreamURL | Out-Null | |
Set-Location -Path $currentDirectory | |
} | |
# 引数があるかどうかを確認 | |
if ($args.Count -eq 0) { | |
# 引数がない場合はユーザーに入力を求める | |
do { | |
$repoURL = Read-Host "GitHubリポジトリのURLを入力してください" | |
} until (Test-GitHubRepositoryURL $repoURL) | |
} | |
else { | |
# 引数がある場合はその値を利用する | |
$repoURL = $args[0] | |
Test-GitHubRepositoryURL $repoURL | Out-Null | |
} | |
# リポジトリのオーナー名とリポジトリ名を取得 | |
$repoURLComponents = $repoURL -split '/' | |
$owner = $repoURLComponents[-2] | |
$repoName = $repoURLComponents[-1] | |
Write-Host "オーナー: $owner" | |
Write-Host "リポジトリ名: $repoName" | |
# リポジトリがフォークであるかどうかを確認 | |
$isFork = (gh api repos/$owner/$repoName | ConvertFrom-Json).fork | |
if ($isFork) { | |
Write-Host "-> フォークリポジトリです。" -ForegroundColor Yellow | |
} | |
# オーナーフォルダの名前を決定する | |
$parentFolderName = $owner | |
$upstreamCloneURL = $null | |
if ($isFork) { | |
$originalOwner = (gh api repos/$owner/$repoName | ConvertFrom-Json).parent.owner.login | |
Write-Host "フォーク元のリポジトリのオーナー: $originalOwner" | |
if (Test-Path -Path "@$originalOwner") { | |
$parentFolderName = $originalOwner | |
} | |
$upstreamCloneURL = "git@github.com:$originalOwner/$repoName.git" | |
} | |
# オーナーフォルダが存在しない場合は作成する。フォルダ名は "@" + owner とする | |
$ownerFolder = Join-Path -Path $PWD -ChildPath "@$parentFolderName" | |
if (-not (Test-Path -Path $ownerFolder)) { | |
Write-Host "フォルダ $ownerFolder が存在しないため、作成します。" -ForegroundColor Yellow | |
New-Item -Path $ownerFolder -ItemType Directory | Out-Null | |
} | |
$repoFolder = Join-Path -Path $ownerFolder -ChildPath $repoName | |
$cloneURL = "git@github.com:$owner/$repoName.git" | |
Write-Host "クローンURL: $cloneURL" | |
Write-Host "クローン先フォルダ: $repoFolder" | |
# クローン先フォルダが既に存在する場合はエラーを表示して終了する | |
if (Test-Path -Path $repoFolder) { | |
Write-Host "エラー: クローン先フォルダ $repoFolder は既に存在します。" -ForegroundColor Red | |
exit 1 | |
} | |
git clone $cloneURL $repoFolder | |
# フォークの場合は、Upstreamリモートを設定する | |
if ($null -ne $upstreamCloneURL) { | |
Set-UpstreamRemote -Folder $repoFolder -UpstreamURL $upstreamCloneURL | |
Write-Host "Upstream リモートを設定しました。" -ForegroundColor Green | |
} | |
# Visual Studio Codeでフォルダを開く | |
Write-Host "Visual Studio Codeでフォルダを開きます。" | |
code $repoFolder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment