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" ]]