Skip to content

Commit

Permalink
Improve acquiring logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
extsoft committed May 25, 2020
1 parent 3c13059 commit e5bb290
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .workflows/ci-pipeline.bash
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Expand Up @@ -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

Expand Down Expand Up @@ -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).
5 changes: 3 additions & 2 deletions libexec/git-elegant-acquire-git
Expand Up @@ -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 <<MESSAGE
Elegant Git aims to standardize how a work environment should be configured.
Expand Down Expand Up @@ -89,6 +91,5 @@ MESSAGE
rebase_autoStash \
credential_helper_darwin \
acquired
aliases-removing --global
aliases-configuration --global $(git elegant show-commands)
}
5 changes: 3 additions & 2 deletions libexec/git-elegant-acquire-repository
Expand Up @@ -32,13 +32,14 @@ MESSAGE
default() {
source ${BINS}/plugins/configuration
source ${BINS}/plugins/configuration-protected-branches
aliases-removing --local
source ${BINS}/plugins/configuration-acquired
obsolete-configurations-removing --local
basics-configuration --local \
user_name \
user_email \
core_editor \
protected_branches
if ! is-acquired ; then
if ! is-git-acquired ; then
standards-configuration --local \
core_comment \
apply_whitespace \
Expand Down
19 changes: 10 additions & 9 deletions libexec/plugins/configuration
Expand Up @@ -31,9 +31,6 @@ rebase_autoStash=("rebase.autoStash" "false")
## Credentials, MAC only
credential_helper_darwin=("credential.helper" "osxkeychain")

# Elegant Git
acquired=("elegant.acquired" "true")

__ask_question() {
# usage: __ask_options <prefix>
# it makes available ANSWER available
Expand Down Expand Up @@ -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}
Expand All @@ -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}
}
14 changes: 14 additions & 0 deletions 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
}
6 changes: 3 additions & 3 deletions tests/git-elegant-acquire-git.bats
Expand Up @@ -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" ]]
Expand All @@ -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" ]]
Expand All @@ -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" ]]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/git-elegant-acquire-repository.bats
Expand Up @@ -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}: " ]]
Expand Down
3 changes: 2 additions & 1 deletion workflows
Expand Up @@ -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..."
Expand Down

0 comments on commit e5bb290

Please sign in to comment.