Skip to content

Commit

Permalink
Use the latest changes of the master branch during pruning repository
Browse files Browse the repository at this point in the history
There are 2 states of the repository:

1. There is a remote configured. This means that the `master` branchi
has an upstream (usually `origin/master`). And after `git fetch --all`,
the `git rebase` has to be executed in order to rebase (merge) fetched
changes to сurrent local branch. Having the latest version is critical
as the `master` branch is used to seeking for useless local branches.

2. There is no remote configured. This means that the `master` branch
is up to date and seeking for useless local branches should be
performed.
  • Loading branch information
extsoft committed Dec 6, 2019
1 parent b813ebf commit b619fbd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/commands.md
Expand Up @@ -250,6 +250,7 @@ Approximate commands flow is
==>> git elegant prune-repository
git checkout master
git fetch --all
git rebase
git branch --delete --force task-24
git branch --delete --force 2349
git branch --delete --force task-1
Expand Down
12 changes: 10 additions & 2 deletions libexec/git-elegant-prune-repository
Expand Up @@ -31,6 +31,7 @@ Approximate commands flow is
==>> git elegant prune-repository
git checkout master
git fetch --all
git rebase
git branch --delete --force task-24
git branch --delete --force 2349
git branch --delete --force task-1
Expand All @@ -40,13 +41,20 @@ The command works even if the remotes are unavailable.
MESSAGE
}

--is-there-upstream-for() {
git rev-parse --abbrev-ref ${1}@{upstream} >/dev/null 2>&1
}

default() {
git-verbose checkout ${MASTER}
git-verbose fetch --all || info-text "As the remotes can't be fetched, the current local version is used."
if --is-there-upstream-for ${MASTER}; then
git-verbose fetch --all || info-text "As the remotes can't be fetched, the current local version is used."
git-verbose rebase
fi
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
if --is-there-upstream-for ${branch}; then
# the branch has existing upstream; keep it
continue
fi
Expand Down
14 changes: 13 additions & 1 deletion tests/git-elegant-prune-repository.bats
Expand Up @@ -22,10 +22,22 @@ teardown() {
[[ ${lines[@]} =~ "git branch --delete --force equal-to-master" ]]
}

@test "'prune-repository': updates current master branch when there is a remote upstream" {
repo "git checkout -b equal-to-master"
fake-pass "git rev-parse --abbrev-ref master@{upstream}"
fake-pass "git rebase"
check git-elegant prune-repository
[[ ${status} -eq 0 ]]
[[ ${lines[@]} =~ "git fetch --all" ]]
[[ ${lines[@]} =~ "git rebase" ]]
[[ ${lines[@]} =~ "git branch --delete --force equal-to-master" ]]
}

@test "'prune-repository': works when the remote repository is unavailable" {
repo "git checkout -b equal-to-master"
repo "git checkout master"
fake-pass "git rev-parse --abbrev-ref master@{upstream}"
fake-fail "git fetch --all" "Manual fetch fail"
fake-pass "git rebase"
check git-elegant prune-repository
[[ ${status} -eq 0 ]]
[[ ${lines[@]} =~ "Manual fetch fail" ]]
Expand Down

0 comments on commit b619fbd

Please sign in to comment.