From e5bb29019d6582f6706494307bb43d7c5fc38624 Mon Sep 17 00:00:00 2001 From: Dmytro Serdiuk Date: Mon, 25 May 2020 16:18:27 +0300 Subject: [PATCH] Improve acquiring logic The first step of the acquiring logic (`acquire-git` and `acquire-repository` commands) is removing obsolete configuration options (keys). Now it consists of 2 steps: 1. removing of `elegant.acquired` key as it was replaced with `elegant-git.acquired` key 2. removing Elegant Git aliases as they should be reconfigured each time acquiring works Also, now the `libexec/plugins/configuration-acquired` plugin encapsulates acquiring logic. --- .workflows/ci-pipeline.bash | 2 +- docs/configuration.md | 4 +++- libexec/git-elegant-acquire-git | 5 +++-- libexec/git-elegant-acquire-repository | 5 +++-- libexec/plugins/configuration | 19 ++++++++++--------- libexec/plugins/configuration-acquired | 14 ++++++++++++++ tests/git-elegant-acquire-git.bats | 6 +++--- tests/git-elegant-acquire-repository.bats | 4 ++-- workflows | 3 ++- 9 files changed, 41 insertions(+), 21 deletions(-) create mode 100755 libexec/plugins/configuration-acquired diff --git a/.workflows/ci-pipeline.bash b/.workflows/ci-pipeline.bash index d025ef4..4a6e484 100755 --- a/.workflows/ci-pipeline.bash +++ b/.workflows/ci-pipeline.bash @@ -18,7 +18,7 @@ pipeline() { git config --global user.name "Elegant Git" git config --global user.email elegant.git@email.com git config --global core.editor vi - git config --global elegant.acquired true + git config --global elegant-git.acquired true ./install.bash /usr/local src echo "'Unknown command' testing..." git elegant unknown-command | grep "Unknown command: git elegant unknown-command" diff --git a/docs/configuration.md b/docs/configuration.md index 084ac46..e4328e4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -53,7 +53,7 @@ Windows with `true` 6. `pull.rebase true` [`i`] uses `rebase` when `git pull` 7. `rebase.autoStash false` [`i`] don't use `autostash` when `git rebase` 8. `credential.helper osxkeychain` [`i`] configures default credentials storage on MacOS only -9. `elegant.acquired true` [`g`] identifies that Elegant Git global configuration is applied +9. `elegant-git.acquired true` [`g`] identifies that Elegant Git global configuration is applied # Level: Aliases @@ -88,3 +88,5 @@ should be separarated with space). By default, `master` branch treats as protect means that Elegant Git commands for a branch state modification (such as `save-work`, `polish-work`, etc.) are prohibited to work if the current branch is protected. Also, the protected branches cannot be removed while running Elegant Git commands for serving a repository. +- `elegant-git.acquired` defines whether a user was applied global configuration or not (see +[approach](#approach) for the details). diff --git a/libexec/git-elegant-acquire-git b/libexec/git-elegant-acquire-git index 9eb37b2..f0bdf05 100755 --- a/libexec/git-elegant-acquire-git +++ b/libexec/git-elegant-acquire-git @@ -31,7 +31,9 @@ MESSAGE default() { source ${BINS}/plugins/configuration source ${BINS}/plugins/configuration-protected-branches - if ! $(is-acquired) ; then + source ${BINS}/plugins/configuration-acquired + obsolete-configurations-removing --global + if ! is-git-acquired ; then info-box "Thank you for installing Elegant Git! Let's configure it..." cat < # it makes available ANSWER available @@ -102,7 +99,7 @@ aliases-removing() { local scope=${1} local aliases=($(git config ${scope} --name-only --get-regexp "^alias\." "^elegant ([-a-z]+)$")) if [[ ${#aliases[@]} -ne 0 ]]; then - info-box "Removing old Elegant Git aliases..." + info-text "Removing old Elegant Git aliases..." local counter=0 for alias in ${aliases[@]}; do git-verbose config ${scope} --unset ${alias} @@ -123,10 +120,14 @@ aliases-configuration() { done } -is-acquired() { - # usage: if $(acquired-state) ; then - if [[ "$(git config --global --get ${acquired[0]} || true)" == ${acquired[1]} ]]; then - return 0 +obsolete-configurations-removing() { + # usage: obsolete-configurations-removing <--global | --local> + local scope=${1} + info-box "Removing obsolete configurations..." + local old_acquired="elegant.acquired" + if git config ${scope} --get-regexp ${old_acquired} >/dev/null; then + info-text "Removing old Elegnat Git configuration keys..." + git-verbose config ${scope} --unset ${old_acquired} fi - return 1 + aliases-removing ${scope} } diff --git a/libexec/plugins/configuration-acquired b/libexec/plugins/configuration-acquired new file mode 100755 index 0000000..3432429 --- /dev/null +++ b/libexec/plugins/configuration-acquired @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# The plugin provides configuration for "acquired" logic that determines +# whether Elegant Git global configuration is applied or not. + +acquired=("elegant-git.acquired" "true") + +is-git-acquired() { + # usage : is-git-acquired + # result: 0 or 1 + if [[ "$(git config --global --get ${acquired[0]} || true)" == ${acquired[1]} ]]; then + return 0 + fi + return 1 +} diff --git a/tests/git-elegant-acquire-git.bats b/tests/git-elegant-acquire-git.bats index d28af0d..b100ca4 100644 --- a/tests/git-elegant-acquire-git.bats +++ b/tests/git-elegant-acquire-git.bats @@ -44,7 +44,7 @@ teardown() { [[ ${lines[@]} =~ "==>> git config --global core.autocrlf true" ]] [[ ${lines[@]} =~ "==>> git config --global pull.rebase true" ]] [[ ${lines[@]} =~ "==>> git config --global rebase.autoStash false" ]] - [[ ${lines[@]} =~ "==>> git config --global elegant.acquired true" ]] + [[ ${lines[@]} =~ "==>> git config --global elegant-git.acquired true" ]] # negative checks are used instead of checking commands size [[ ! ${lines[@]} =~ "==>> git config --global credential.helper osxkeychain" ]] [[ ! ${lines[@]} =~ "==>> git config --global core.autocrlf input" ]] @@ -60,7 +60,7 @@ teardown() { [[ ${lines[@]} =~ "==>> git config --global core.autocrlf input" ]] [[ ${lines[@]} =~ "==>> git config --global pull.rebase true" ]] [[ ${lines[@]} =~ "==>> git config --global rebase.autoStash false" ]] - [[ ${lines[@]} =~ "==>> git config --global elegant.acquired true" ]] + [[ ${lines[@]} =~ "==>> git config --global elegant-git.acquired true" ]] # negative checks are used instead of checking commands size [[ ! ${lines[@]} =~ "==>> git config --global credential.helper osxkeychain" ]] [[ ! ${lines[@]} =~ "==>> git config --global core.autocrlf true" ]] @@ -77,7 +77,7 @@ teardown() { [[ ${lines[@]} =~ "==>> git config --global pull.rebase true" ]] [[ ${lines[@]} =~ "==>> git config --global rebase.autoStash false" ]] [[ ${lines[@]} =~ "==>> git config --global credential.helper osxkeychain" ]] - [[ ${lines[@]} =~ "==>> git config --global elegant.acquired true" ]] + [[ ${lines[@]} =~ "==>> git config --global elegant-git.acquired true" ]] # negative checks are used instead of checking commands size [[ ! ${lines[@]} =~ "==>> git config --global core.autocrlf true" ]] } diff --git a/tests/git-elegant-acquire-repository.bats b/tests/git-elegant-acquire-repository.bats index 91c29d3..183389f 100644 --- a/tests/git-elegant-acquire-repository.bats +++ b/tests/git-elegant-acquire-repository.bats @@ -111,11 +111,11 @@ teardown() { [[ ${lines[@]} =~ "2 Elegant Git aliases were removed." ]] } -@test "'acquire-repository': works properly if 'elegant.acquired' == true " { +@test "'acquire-repository': works properly if 'elegant-git.acquired' == true " { fake-pass "uname -s" Linux repo git config --local "alias.aaa" "\"elegant aaa\"" repo git config --global "alias.bbb" "\"elegant bbb\"" - repo git config --global "elegant.acquired" "true" + repo git config --global "elegant-git.acquired" "true" repo git config --local core.editor my-text-editor check git-elegant acquire-repository [[ ${lines[@]} =~ "What is your user name? {Elegant Git}: " ]] diff --git a/workflows b/workflows index cea56b7..da05a16 100755 --- a/workflows +++ b/workflows @@ -34,7 +34,8 @@ repository() { git config --global user.name \"Elegant Git\" git config --global user.email elegant.git@email.com git config --global core.editor vi - git config --global elegant.acquired true + git config --global elegant-git.protected-branches master + git config --global elegant-git.acquired true ./install.bash /usr/local src " info-text "Ready! Enjoy experiments..."