Skip to content

Commit

Permalink
Introduce acquire-git command
Browse files Browse the repository at this point in the history
The command allows applying the required configuration to a Git
installation. Since there are some interceptionsbetween the
`acquire-git` and `acquire-repository`, there are some changes in the
overall configuration workflow. These changes are desribed on the
documentation.

Also, to prevent unexpected tests failures, the `~/.gitconfig` is
removed after each test where a real Git repository is used.

#177
  • Loading branch information
extsoft committed Nov 3, 2019
1 parent 4e36cf0 commit f9b7606
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 29 deletions.
13 changes: 13 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ There are commands used in various situations such as

and others
commands Prints available Elegant Git commands.
acquire-git Configures a Git installation.


Please visit https://elegant-git.bees-hive.org to find out more.
Expand Down Expand Up @@ -72,6 +73,18 @@ git branch --delete --force __eg
git push origin --delete task-123
```

# `acquire-git`

```bash
usage: git elegant acquire-repository
```

Applies the "basics", "standards", and "aliases" configurations to the current
Git installation using `git config --global`.

To find out what will be configured, please visit
https://elegant-git.bees-hive.org/en/latest/configuration/

# `acquire-repository`

```bash
Expand Down
45 changes: 31 additions & 14 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# Approach
Elegant Git aims to standardize how a Git repository should be configured. It operates 3 levels of
configurations (basics, standards, and aliases - see below) which are applied only within a
current Git repository by using `git config --local <key> <value>`.
configurations (basics, standards, and aliases - see below) which can be applied to a repository
(local configuration) or to a Git globally (global configuration).

The local configuration applies by running
[`git elegant acquire-repository`](commands.md#acquire-repository) and configures current Git
repository by using `git config --local <key> <value>`.

The global configuration invokes by [`git elegant acquire-git`](commands.md#acquire-git) and uses
`git config --global <key> <value>` for Git configuration.

If you've applied a global configuration, there is no sense to repeat some options for a local one.
That's why the following markers explain how each particular option will be configured:

- `b` - configures for both configurations
- `l` - configures only for a local configuration
- `g` - configures only for a global configuration
- `i` - if a global configuration is applied, an option isn't used in local configuration;
otherwise, uses in local configuration

# Basics
The basics configuration configures the following options `git config` options:

1. `user.name`
2. `user.email`
3. `core.editor`
1. `user.name` (`b`)
2. `user.email` (`b`)
3. `core.editor` (`i`)

During the configuration, you will be asked to provide appropriate values.

Expand All @@ -17,21 +33,22 @@ The standards configuration configures a set of the `git config` options which b
OS-specific configurations and add specific options for the correct execution of Elegant Git
commands. It consists of

1. setting `core.commentChar` to `|` enables commit messages starting from `#`
2. setting `apply.whitespace` to `fix` removes whitespaces when applying a patch
3. setting `fetch.prune` to `true` keeps remote-tracking references up-to-date
4. setting `fetch.pruneTags` to `false` does not remove tags until you specify it explicitly
1. setting `core.commentChar` (`i`) to `|` enables commit messages starting from `#`
2. setting `apply.whitespace` (`i`) to `fix` removes whitespaces when applying a patch
3. setting `fetch.prune` (`i`) to `true` keeps remote-tracking references up-to-date
4. setting `fetch.pruneTags` (`i`) to `false` does not remove tags until you specify it explicitly
(`git fetch --tags`)
5. setting `core.autocrlf` to either `input` on MacOS/Linux or `true` on Windows solves issues with
5. setting `core.autocrlf` (`i`) to either `input` on MacOS/Linux or `true` on Windows solves issues with
line endings
6. setting `pull.rebase` to `true` uses `rebase` when `git pull`
7. setting `rebase.autoStash` to `false` uses `autostash` never when `git rebase`
8. setting `credential.helper` to `osxkeychain` on MacOS configures default credentials storage
6. setting `pull.rebase` (`i`) to `true` uses `rebase` when `git pull`
7. setting `rebase.autoStash` (`i`) to `false` uses `autostash` never when `git rebase`
8. setting `credential.helper` (`i`) to `osxkeychain` on MacOS configures default credentials storage
9. setting `elegant.acquired` (`g`) to `true` identifies that Elegant Git global configuration is applied

# Aliases
In order to make Elegant Git command like a native Git command, each Elegant Git command will have
an appropriate alias like `git elegant save-work` will become `git save-work`. This should
significantly improve user experience.

The configuration is a call of `git config "alias.<command>" "elegant <command>"` for each Elegant
The configuration is a call of `git config "alias.<command>" "elegant <command>"` (`i`) for each Elegant
Git command.
8 changes: 5 additions & 3 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ main() {
echo "Elegant Git is installed to '${INSTALL_PATH}/bin/git-elegant'."
echo "
The final step after installation is to run
git elegant acquire-repository
for each git repository where Elegant Git is going to be used.
git elegant acquire-git
to apply the latest Elegant Git's configuration.
If this step is not completed, Elegant Git may behave unexpectedly
since the desired Git configuration (relevant for the current version)
is not applied.
is not applied. Please read more on
https://elegant-git.bees-hive.org/en/latest/configuration/
"
command -v git-elegant 1>/dev/null 2>&1 || next-steps ${INSTALL_PATH}
}
Expand Down
1 change: 1 addition & 0 deletions libexec/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ $(--print-command-in-usage release-work)
and others
$(--print-command-in-usage commands)
$(--print-command-in-usage acquire-git)
Please visit ${__site} to find out more.
Expand Down
41 changes: 41 additions & 0 deletions libexec/git-elegant-acquire-git
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -e

command-purpose() {
cat <<MESSAGE
Configures a Git installation.
MESSAGE
}

command-synopsis() {
cat <<MESSAGE
usage: git elegant acquire-repository
MESSAGE
}

command-description() {
cat<<MESSAGE
Applies the "basics", "standards", and "aliases" configurations to the current
Git installation using \`git config --global\`.
To find out what will be configured, please visit
${__site}/en/latest/configuration/
MESSAGE
}

default() {
source ${BINS}/plugins/configuration
basics-configuration --global user_name user_email core_editor
standards-configuration --global \
core_comment \
apply_whitespace \
fetch_prune \
fetch_pruneTags \
core_autocrlf_darwinlinux \
core_autocrlf_windows pull_rebase \
rebase_autoStash \
credential_helper_darwin \
acquired
aliases-removing --global
aliases-configuration --global $(git elegant commands)
}
29 changes: 17 additions & 12 deletions libexec/git-elegant-acquire-repository
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ MESSAGE

default() {
source ${BINS}/plugins/configuration
basics-configuration --local user_name user_email core_editor
standards-configuration --local \
core_comment \
apply_whitespace \
fetch_prune \
fetch_pruneTags \
core_autocrlf_darwinlinux \
core_autocrlf_windows pull_rebase \
rebase_autoStash \
credential_helper_darwin
aliases-removing --local
aliases-configuration --local $(git elegant commands)
if [[ $(git config ${acquired[0]}) == ${acquired[1]} ]]; then
basics-configuration --local user_name user_email
aliases-removing --local
else
basics-configuration --local user_name user_email core_editor
standards-configuration --local \
core_comment \
apply_whitespace \
fetch_prune \
fetch_pruneTags \
core_autocrlf_darwinlinux \
core_autocrlf_windows pull_rebase \
rebase_autoStash \
credential_helper_darwin
aliases-removing --local
aliases-configuration --local $(git elegant commands)
fi
}
3 changes: 3 additions & 0 deletions libexec/plugins/configuration
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ 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
3 changes: 3 additions & 0 deletions tests/addons-repo.bash
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ repo-clean() {
if [[ -d "${GIT_REPO_DIR}" ]]; then
rm -rf "${GIT_REPO_DIR}"
fi
if [[ -f ~/.gitconfig ]]; then
rm -rf ~/.gitconfig
fi
}
95 changes: 95 additions & 0 deletions tests/git-elegant-acquire-git.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env bats

load addons-common
load addons-read
load addons-fake
load addons-repo

setup() {
repo-new
}

teardown() {
fake-clean
repo-clean
}

@test "'acquire-git': all configurations work as expected" {
check git-elegant acquire-git
[[ "${status}" -eq 0 ]]
}

@test "'acquire-git': basics are configured as expected" {
check git-elegant acquire-git
[[ "${lines[@]}" =~ "What is your user name? {Elegant Git}: " ]]
[[ "${lines[@]}" =~ "==>> git config --global user.name Elegant Git" ]]
[[ "${lines[@]}" =~ "What is your user email? {[email protected]}: " ]]
[[ "${lines[@]}" =~ "==>> git config --global user.email [email protected]" ]]
[[ "${lines[@]}" =~ "Please specify a command to start the editor. {edi}: " ]]
[[ "${lines[@]}" =~ "==>> git config --global core.editor edi" ]]
}

@test "'acquire-git': standards are configured as expected on Windows" {
fake-pass "uname -s" Windows
check git-elegant acquire-git
[[ "${lines[@]}" =~ "==>> git config --global core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --global apply.whitespace fix" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.prune true" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.pruneTags false" ]]
[[ "${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" ]]
# negative checks are used instead of checking commands size
[[ ! "${lines[@]}" =~ "==>> git config --global credential.helper osxkeychain" ]]
[[ ! "${lines[@]}" =~ "==>> git config --global core.autocrlf input" ]]
}

@test "'acquire-git': standards are configured as expected on Linux" {
fake-pass "uname -s" Linux
check git-elegant acquire-git
[[ "${lines[@]}" =~ "==>> git config --global core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --global apply.whitespace fix" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.prune true" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.pruneTags false" ]]
[[ "${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" ]]
# negative checks are used instead of checking commands size
[[ ! "${lines[@]}" =~ "==>> git config --global credential.helper osxkeychain" ]]
[[ ! "${lines[@]}" =~ "==>> git config --global core.autocrlf true" ]]
}

@test "'acquire-git': standards are configured as expected on Darwin" {
fake-pass "uname -s" Darwin
check git-elegant acquire-git
[[ "${lines[@]}" =~ "==>> git config --global core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --global apply.whitespace fix" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.prune true" ]]
[[ "${lines[@]}" =~ "==>> git config --global fetch.pruneTags false" ]]
[[ "${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 credential.helper osxkeychain" ]]
[[ "${lines[@]}" =~ "==>> git config --global elegant.acquired true" ]]
# negative checks are used instead of checking commands size
[[ ! "${lines[@]}" =~ "==>> git config --global core.autocrlf true" ]]
}

@test "'acquire-git': new aliases are configured as expected" {
check git-elegant acquire-git
for next in $(git-elegant commands); do
echo "Test aliasing of '${next}' command"
[[ "${lines[@]}" =~ "==>> git config --global alias.${next} elegant ${next}" ]]
echo "Tested successfully!"
done
}

@test "'acquire-git': old aliases remove correctly if they are present" {
repo git config --global "alias.aaa" "\"elegant aaa\""
repo git config --global "alias.bbb" "\"elegant bbb\""
check git-elegant acquire-git
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "2 Elegant Git aliases were removed." ]]
}
17 changes: 17 additions & 0 deletions tests/git-elegant-acquire-repository.bats
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ teardown() {
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "2 Elegant Git aliases were removed." ]]
}

@test "'acquire-repository': 'elegant.acquired' affects configuration correctly" {
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"
check git-elegant acquire-repository
[[ "${lines[@]}" =~ "What is your user name? {Elegant Git}: " ]]
[[ "${lines[@]}" =~ "==>> git config --local user.name Elegant Git" ]]
[[ "${lines[@]}" =~ "What is your user email? {[email protected]}: " ]]
[[ "${lines[@]}" =~ "==>> git config --local user.email [email protected]" ]]
[[ ! "${lines[@]}" =~ "Please specify a command to start the editor. {edi}: " ]]
[[ ! "${lines[@]}" =~ "==>> git config --local core.editor edi" ]]
[[ ! "${lines[@]}" =~ "==>> git config --local core.commentChar |" ]]
[[ "${lines[@]}" =~ "1 Elegant Git aliases were removed." ]]
[[ ! "${lines[@]}" =~ "==>> git config --local alias.acquire-repository elegant acquire-repository" ]]
}
1 change: 1 addition & 0 deletions tests/git-elegant-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ teardown() {
@test "'commands': print all available commands" {
COMMANDS=(
"acquire-repository"
"acquire-git"
"clone-repository"
"init-repository"
"start-work"
Expand Down

0 comments on commit f9b7606

Please sign in to comment.