Skip to content

Commit

Permalink
feat(remote): add origin option to get custom targeted remote
Browse files Browse the repository at this point in the history
Close #1
  • Loading branch information
Maxime Bréhin committed Apr 9, 2020
1 parent 940303a commit d403680
Showing 1 changed file with 15 additions and 10 deletions.
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

# 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

0 comments on commit d403680

Please sign in to comment.