Skip to content

Instantly share code, notes, and snippets.

@gimmi
Last active June 17, 2022 08:09
Show Gist options
  • Save gimmi/6dc611cc3bd7059a54627bc0667162f4 to your computer and use it in GitHub Desktop.
Save gimmi/6dc611cc3bd7059a54627bc0667162f4 to your computer and use it in GitHub Desktop.
Git helpers
param(
[String]
[ValidateSet('pull', 'clean')]
$Cmd
)
$ErrorActionPreference = "Stop"
function Run-Git() {
$GitExe = (Get-Command -Name git -CommandType Application).Source
$GitArgs = @('--no-pager', '-C') + $Args
& $GitExe $GitArgs
}
$Repos = Get-ChildItem -Directory # -Path $PSScriptRoot
foreach ($Repo in $Repos) {
$Path = $Repo.FullName
$RepoDir = Split-Path $Path -Leaf
$RepoUrl = Run-Git $Path config --get remote.origin.url
$Branch = Run-Git $Path 'rev-parse' '--abbrev-ref' 'HEAD'
Write-Host -ForegroundColor Green '--------------------------------------------------------------------------------'
Write-Host " dir: $RepoDir"
Write-Host " url: $RepoUrl"
Write-Host "branch: $Branch"
Write-Host -ForegroundColor Green '--------------------------------------------------------------------------------'
# Run-Git $Path 'branch' '--list' '--all' # '--format=%(refname:short)|%(upstream:short)|%(HEAD)'
switch ($Cmd) {
pull {
Run-Git $Path 'diff' 'HEAD' '--quiet'
if ($LastExitCode -ne 0) {
Write-Host -ForegroundColor Red "Repo contains changes"
} else {
Run-Git $Path 'fetch' '--all'
Run-Git $Path 'merge' '--ff-only'
}
}
clean {
Run-Git $Path 'clean' '-dxf'
}
}
}
#!/usr/bin/env bash
set -Eeuo pipefail
CYAN='\e[36m'
NC='\e[0m'
PREFIX="${CYAN}git-commpush:${NC}"
if [ -z "$(git status --porcelain)" ]; then
echo -e "${PREFIX} nothing to commit"
exit 0
fi
git add --all
git commit --allow-empty-message --no-edit
git pull --rebase
git push
# git add --all
# git commit --amend --no-edit
# git push --force-with-lease
#!/usr/bin/env bash
set -Eeuo pipefail
CYAN='\e[36m'
NC='\e[0m'
PREFIX="${CYAN}git-each:${NC}"
for dir in */ ; do
if [ -d "${dir}.git" ]; then
echo -e "${PREFIX} git -C $dir $@"
git -C "$dir" $@
else
echo -e "${PREFIX} not a git repo: ${dir}" >&2
fi
done
#!/usr/bin/env bash
set -Eeuo pipefail
NC='\e[0m'
CYAN='\e[36m'
YELLOW='\e[33m'
if [ ! -d .git ]; then
echo "not a GIT repo" >&2
exit 1
fi
co_branch=$(git rev-parse --abbrev-ref HEAD)
git fetch --all --prune
for branch in $(git for-each-ref refs/heads --format="%(refname:short)"); do
rem_name=$(git config --get branch.$branch.remote) || true
rem_branch=$(git config --get branch.$branch.merge | sed 's:refs/heads/::') || true
rem_rev=$(git rev-parse --verify --quiet $rem_name/$rem_branch) || true
header="${CYAN}git-mergeall:${NC} $branch"
# header="${header} ${rem_name:-NONE}/${rem_branch:-NONE} ${rem_rev:-NONE}"
if [ "$co_branch" = "$branch" ]; then
header="${header} (current)"
if [ "$rem_rev" = "" ] && [ "$rem_name" = "" ] && [ "$rem_branch" = "" ]; then
echo -e "${header}: local only"
elif [ "$rem_rev" = "" ]; then
echo -e "${header}: ${YELLOW}remote deleted${NC}"
elif git diff HEAD --quiet; then
echo -e "${header}: fast-forward merge"
git merge --ff-only $rem_name/$rem_branch
else
echo -e "${header}: ${YELLOW}skipping because of uncommitted changes${NC}"
fi
elif [ "$co_branch" != "$branch" ] && [ "$rem_rev" != "" ]; then
echo -e "${header}: updating to remote"
git fetch . $rem_name/$rem_branch:$branch
elif [ "$co_branch" != "$branch" ] && [ "$rem_rev" = "" ] && [ "$rem_name" != "" ] && [ "$rem_branch" != "" ]; then
echo -e "${header}: ${YELLOW}deleting it because $rem_name/$rem_branch does not exists anymore${NC}"
git branch -D $branch
else
echo -e "${header}: nothing to do"
fi
done
exit 0
$ErrorActionPreference = 'Stop'
function Run-Git() {
$GitExe = (Get-Command -Name git -CommandType Application).Source
$GitArgs = @() + $Args
Write-Host "$GitExe $GitArgs"
& $GitExe $GitArgs
if ($LASTEXITCODE -ne 0) { throw "Failed git command with $LASTEXITCODE" }
}
Run-Git 'add' '.'
Run-Git 'commit' '-a' '-m' '.'
Run-Git 'pull' '--rebase'
Run-Git 'push'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment