Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remote): add origin option to get custom targeted remote #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions git-clean-stale-local
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
#
# Licensed under the MIT license.

{ [ "$1" = '-n' ] || [ "$1" = '--dry-run' ]; } && dryRun=true || dryRun=false
{ [ "$1" = '-n' ] || [ "$1" = '--dry-run' ] || [ "$2" = '-n' ] || [ "$2" = '--dry-run' ]; } && dryRun=true || dryRun=false
mbrehin marked this conversation as resolved.
Show resolved Hide resolved

# Default remote is `origin` but can be overriden using `origin` option
origin='origin'
{ [ "$1" = '--origin' ]; } && origin="$2"
{ [ "$2" = '--origin' ]; } && origin="$3"

function cleanupStaleLocaleBranch()
{
local branch="$1"
if $dryRun; then
echo "Would clean up local $branch that uses stale origin remote tracking."
echo "Would clean up local $branch that uses stale $origin remote tracking."
else
echo "Cleaning up local $branch that uses stale origin remote tracking…"
echo "Cleaning up local $branch that uses stale $origin remote tracking…"
git branch --delete --quiet "$branch"
fi
}
Expand All @@ -31,18 +36,18 @@ function cleanupStaleLocaleBranch()
# output capture, e.g. `$(getDefaultBranch)`).
function getDefaultBranch()
{
local cachedPath="$(git rev-parse --git-dir)/refs/remotes/origin/HEAD"
local cachedPath="$(git rev-parse --git-dir)/refs/remotes/$origin/HEAD"
if [ -f "$cachedPath" ]; then
sed -n 's@ref: refs/remotes/origin/@@p' "$cachedPath"
sed -n "s@ref: refs/remotes/$origin/@@p" "$cachedPath"
return
fi
local ref=$(git ls-remote --symref origin HEAD | sed -n 's@ref: refs/heads/@@p' | cut -f1)
local ref=$(git ls-remote --symref $origin HEAD | sed -n 's@ref: refs/heads/@@p' | cut -f1)
if [ $? -eq 0 ]; then
echo "ref: refs/remotes/origin/$ref" > "$cachedPath"
echo "ref: refs/remotes/$origin/$ref" > "$cachedPath"
echo $ref
return
fi
echo "Cannot determine default branch: no readable origin/HEAD anywhere." >&2
echo "Cannot determine default branch: no readable $origin/HEAD anywhere." >&2
exit 69 # EX_UNAVAILABLE
}

Expand All @@ -61,10 +66,10 @@ function isBranchTrackingStale()
local branch="$1"
local branchRemote=$(git config --local "branch.$branch.remote")
[ -z "$branchRemote" ] && return 1
[ "$branchRemote" != 'origin' ] && return 1
[ "$branchRemote" != $origin ] && return 1

local remoteBranch=$(git config --local "branch.$branch.merge" | sed 's@refs/heads/@@')
! [ -f "$(git rev-parse --git-dir)/refs/remotes/origin/$remoteBranch" ]
! [ -f "$(git rev-parse --git-dir)/refs/remotes/$origin/$remoteBranch" ]
}

for branch in $(getLocallyMergedBranches); do
Expand Down