Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Set config based on where current config originates from
Browse files Browse the repository at this point in the history
This is a breaking change.
  • Loading branch information
spinningarrow committed Dec 11, 2017
1 parent 993d7d4 commit 9c5a646
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
53 changes: 24 additions & 29 deletions git-pair
Expand Up @@ -83,35 +83,12 @@ _construct_names_and_emails () {
echo "$email_ids@$email_host"
}

_set_local () {
echo "Updated ${bold}local${normal} git config:"
echo
git config user.name "$1"
git config user.email "$2"
}

_set_global () {
git config --unset user.name &> /dev/null
git config --unset user.email &> /dev/null

echo "Updated ${bold}global${normal} git config (use --local to set locally instead):"
echo
git config --global user.name "$names"
git config --global user.email "$emails"
}

_set_pairs () {
if [ -z $1 ]; then
exit 1
fi

if [ $1 = '--local' ]; then
num_args_to_drop=2
is_local=true
else
num_args_to_drop=1
is_local=false
fi
local num_args_to_drop=1

local names_and_emails=$(_construct_names_and_emails "${@:$num_args_to_drop}")
local names=$(echo "$names_and_emails" | head -1)
Expand All @@ -120,10 +97,20 @@ _set_pairs () {
[ -z "$names" ] && exit 1
[ -z "$emails" ] && exit 1

if $is_local; then
_set_local "$names" "$emails"
local name_origin email_origin origins=$(_get_origins)
if [ $(echo "$origins" | wc -l | tr -d ' ') -eq 2 ]; then
name_origin=$(echo "$origins" | head -1)
email_origin=$(echo "$origins" | tail -1)
else
_set_global "$names" "$emails"
name_origin=".git/config"
email_origin=".git/config"
fi

git config --file $name_origin user.name "$names"
git config --file $email_origin user.email "$emails"

if [ $? -ne 0 ]; then
echo Unable to set git config. Are you in a git directory?
fi
}

Expand All @@ -138,8 +125,13 @@ git_pair_reset () {
}

git_pair_show () {
git config --get user.name
git config --get user.email
git config user.name
git config user.email
}

_get_origins () {
git config --show-origin user.name | grep -e '^file:' | cut -f1 | cut -d':' -f2
git config --show-origin user.email | grep -e '^file:' | cut -f1 | cut -d':' -f2
}

case "$1" in
Expand All @@ -161,6 +153,9 @@ case "$1" in
show)
git_pair_show ;;

test)
_get_origins ;;

*)
git_pair_help
exit 1
Expand Down
47 changes: 44 additions & 3 deletions tests/test.bats
@@ -1,8 +1,15 @@
#!/usr/bin/env bats

setup () {
run mkdir -p $BATS_TMPDIR/$BATS_TEST_NAME
run git init $BATS_TMPDIR/$BATS_TEST_NAME
cd $BATS_TMPDIR/$BATS_TEST_NAME
}

teardown() {
git config --global --unset user.name
git config --global --unset user.email
cd /
if [ -e ~/.gitpairables ]; then rm ~/.gitpairables; fi
}

Expand Down Expand Up @@ -73,7 +80,9 @@ teardown() {
}

# set
@test "'set' changes global config to provided pairs" {
@test "'set' changes global config to provided pairs when user is set globally" {
git config --global user.name something
git config --global user.email something@something.com
run expect /tests/expect_add_one.exp
run expect /tests/expect_add_two.exp
run git pair set one two
Expand All @@ -82,13 +91,45 @@ teardown() {
[ "$(git config --global --get user.email)" = 'one+two@one.com' ]
}

@test "'set' changes local config to provided pairs when user is set locally" {
run git config user.name something
run git config user.email something@something.com
run expect /tests/expect_add_one.exp
run expect /tests/expect_add_two.exp
run git pair set one two

[ "$(git config --local user.name)" = 'One and Two' ]
[ "$(git config --local user.email)" = 'one+two@one.com' ]
}

@test "set: changes config based on location of original config" {
run git config --global user.name something
run git config user.email something@something.com
run expect /tests/expect_add_one.exp
run expect /tests/expect_add_two.exp
run git pair set one two

[ "$(git config --global user.name)" = 'One and Two' ]
[ "$(git config --local user.email)" = 'one+two@one.com' ]
}

@test "set: changes local config when either name or email are not set at all" {
run git config --global user.name something
run expect /tests/expect_add_one.exp
run expect /tests/expect_add_two.exp
run git pair set one two

[ "$(git config --local user.name)" = 'One and Two' ]
[ "$(git config --local user.email)" = 'one+two@one.com' ]
}

@test "'set' ignores the case of pair nicknames" {
run expect /tests/expect_add_one.exp
run expect /tests/expect_add_two.exp
run git pair set OnE tWo

[ "$(git config --global --get user.name)" = 'One and Two' ]
[ "$(git config --global --get user.email)" = 'one+two@one.com' ]
[ "$(git config user.name)" = 'One and Two' ]
[ "$(git config user.email)" = 'one+two@one.com' ]
}

@test "'set' does nothing if the provided nicknames don't exist" {
Expand Down

0 comments on commit 9c5a646

Please sign in to comment.