Skip to content

Instantly share code, notes, and snippets.

@kbilsted
Last active June 11, 2022 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbilsted/9b0e74a8e77d67a2edef39c5d38c5e12 to your computer and use it in GitHub Desktop.
Save kbilsted/9b0e74a8e77d67a2edef39c5d38c5e12 to your computer and use it in GitHub Desktop.
Clone or pull all repo's of a user - only repos with no changes and on "master" branch are updated
# remember to change the $gitHubApiKey
# remember to change the $uri username
$git="C:\Program Files\Git\bin\git.exe"
$gitHubApiKey = convertto-securestring "ghp_sdafsdfsdaf..." -asplaintext -force
function PullOrCloneAll($path="c:\src")
{
cd $path
$repositories = FetchRepoList
$i = 0
Foreach($repo IN $repositories)
{
$i += 1
Write-Host("{0,3} {1,-40} {2,-40}" -f ` $i, $repo.Name, $repo.Clone_Url)
if(test-path $repo.Name)
{
Pull($repo);
}
else
{
Clone($repo);
}
}
}
function FetchRepoList()
{
$uri = "https://api.github.com:443/users/kbilsted/repos?page=&per_page=100"
$all = @()
$page = 0
$any = $TRUE
while($any)
{
$any = $FALSE
$page += 1
$urin = $uri.replace("?page=","?page="+$page)
$repositories = (Invoke-RestMethod -Uri $urin -Token $gitHubApiKey)
Foreach($repo IN $repositories)
{
$all += $repo
$any = $TRUE
}
}
Write-Host("Found " + $All.Count + " repositories")
return $all | Sort-Object -property Name
}
function MayPullFromRepo()
{
$status = & $git status --porcelain
if($status.Length -gt 0)
{
Write-Host -ForegroundColor red "!! LOCAL CHANGES - giving up"
Write-Host("'"+$status+"'" + $status.Length)
return $FALSE
}
$branch = & $git rev-parse --abbrev-ref HEAD
if($branch -ne "master")
{
Write-Host -ForegroundColor red "!! NOT ON MASTER - giving up"
return $FALSE
}
return $TRUE
}
function Pull($repo)
{
cd $repo.Name
if(MayPullFromRepo)
{
$pullresult = & $git pull --ff-only --no-stat
if($pullresult -ne "Already up-to-date.")
{
Write-Host -ForegroundColor green "" + $pullresult
}
}
cd $path
}
function Clone($repo)
{
Write-Host("Clonning...");
& $git clone -v --recurse-submodules --progress $repo.Clone_Url $repo.Name # alternatively replace "Clone_Url" with "ssh_url"
}
(Get-Host).Version
pullorcloneall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment