-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove useless branches in a repository
The purpose of `clear-local` command is to remove branches that have unexisting upstreams. And it starts asking for deleting branches if they aren't merged. But this behavior is overengineering and, usually, all these branches have to be removed. As the solution, the `prune-repository` command is introduced which makes safe automatic cleanup of all useless branches. Also, it doesn't try to save your working state by using pipes. This is because that if `prune-repository` is executed, then the repository should not have uncommitted changes, otherwise, it will fail.
- Loading branch information
Showing
7 changed files
with
139 additions
and
142 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
command-purpose() { | ||
cat <<MESSAGE | ||
Removes useless local branches. | ||
MESSAGE | ||
} | ||
|
||
command-synopsis() { | ||
cat <<MESSAGE | ||
usage: git elegant prune-repository | ||
MESSAGE | ||
} | ||
|
||
command-description() { | ||
cat<<MESSAGE | ||
Identifies useless branches within the current repository and removes them. A | ||
branch is useless if it either has configured an unavailable upstream branch (1) | ||
or does not have new commits comparing to \`master\` branch (2). | ||
1 - Usually, a local branch has this state when an appropriate remote branch was | ||
merged to a remote target branch and was removed. Since these manipulations were | ||
made on server side, the local branch is still present, but useless. | ||
2 - This kind of branches appears when a branch is created for some purposes but | ||
does not have any commits nowadays. So, it is useless. | ||
Approximate commands flow is | ||
\`\`\`bash | ||
==>> git elegant prune-repository | ||
git checkout master | ||
git fetch --all | ||
git branch --delete --force task-24 | ||
git branch --delete --force 2349 | ||
git branch --delete --force task-1 | ||
\`\`\` | ||
MESSAGE | ||
} | ||
|
||
default() { | ||
git-verbose checkout ${MASTER} | ||
git-verbose fetch --all | ||
for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/**); do | ||
if [[ ${branch} == ${MASTER} ]]; then continue; fi | ||
if [[ -n $(git config --get branch.${branch}.merge) ]]; then | ||
if git rev-parse --abbrev-ref ${branch}@{upstream} >/dev/null 2>&1; then | ||
# the branch has existing upstream; keep it | ||
continue | ||
fi | ||
else | ||
if [[ ! $(git merge-base ${MASTER} ${branch}) == $(git rev-parse ${branch}) ]]; then | ||
# the branch has new commits; keep it | ||
continue | ||
fi | ||
fi | ||
git-verbose branch --delete --force ${branch} | ||
done | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env bats -ex | ||
|
||
load addons-common | ||
load addons-read | ||
load addons-fake | ||
load addons-repo | ||
|
||
setup() { | ||
repo-new | ||
} | ||
|
||
teardown() { | ||
fake-clean | ||
repo-clean | ||
} | ||
|
||
@test "'prune-repository': a branch is removed when it doesn't have an upstream and new commits" { | ||
repo "git checkout -b equal-to-master" | ||
repo "git checkout master" | ||
check git-elegant prune-repository | ||
[[ ${status} -eq 0 ]] | ||
[[ ${lines[@]} =~ "git branch --delete --force equal-to-master" ]] | ||
} | ||
|
||
@test "'prune-repository': a branch is alive when it doesn't have an upstream and has a new commit" { | ||
repo "git checkout -b commit" | ||
repo-commit-file "commit" | ||
check git-elegant prune-repository | ||
[[ ${status} -eq 0 ]] | ||
[[ ! ${lines[@]} =~ "git branch --delete --force commit" ]] | ||
} | ||
|
||
@test "'prune-repository': a branch is removed when it has a gone upstream" { | ||
repo "git checkout -b upstream" | ||
repo "git checkout master" | ||
fake-pass "git config --get branch.upstream.merge" "upstream" | ||
fake-fail "git rev-parse --abbrev-ref upstream@{upstream}" | ||
check git-elegant prune-repository | ||
[[ ${status} -eq 0 ]] | ||
[[ ${lines[@]} =~ "git branch --delete --force upstream" ]] | ||
} | ||
|
||
@test "'prune-repository': a branch is alive when it has an active upstream" { | ||
repo "git checkout -b upstream" | ||
fake-pass "git config --get branch.upstream.merge" "upstream" | ||
fake-pass "git rev-parse --abbrev-ref upstream@{upstream}" | ||
check git-elegant prune-repository | ||
[[ ${status} -eq 0 ]] | ||
[[ ! ${lines[@]} =~ "git branch --delete --force upstream" ]] | ||
} |
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