From b619fbde08d835f962622c2c15e3c03f3af6e699 Mon Sep 17 00:00:00 2001 From: Dmytro Serdiuk Date: Fri, 6 Dec 2019 16:45:54 +0200 Subject: [PATCH] Use the latest changes of the `master` branch during pruning repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- docs/commands.md | 1 + libexec/git-elegant-prune-repository | 12 ++++++++++-- tests/git-elegant-prune-repository.bats | 14 +++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 86b508f..994c581 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -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 diff --git a/libexec/git-elegant-prune-repository b/libexec/git-elegant-prune-repository index 7d7b497..ac927f1 100644 --- a/libexec/git-elegant-prune-repository +++ b/libexec/git-elegant-prune-repository @@ -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 @@ -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 diff --git a/tests/git-elegant-prune-repository.bats b/tests/git-elegant-prune-repository.bats index dd179d5..ecfb51b 100644 --- a/tests/git-elegant-prune-repository.bats +++ b/tests/git-elegant-prune-repository.bats @@ -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" ]]