From 67a0f1abe52241a887e9a08dae801144af9bb7aa Mon Sep 17 00:00:00 2001 From: Augusto Pascutti Date: Mon, 1 Feb 2016 00:06:18 -0200 Subject: [PATCH] Sorts author names instead of just appending them By keeping pair names sorted, whenever you pair with the same people you should get the same author. Prior, the first author in the list was always the author's machine which could make the same pair yield different author name results depending on the machine being used to produce the commits. The lines to sort authors are a mess, that is why I was delaying this. As a remind to myself, this is what they do: * Echoes final author name with all pairs. * Uses `tr` to change "+" into new lines. * Uses `sed` to remove leading white spaces from author names. * Uses `sed` to remove trailing white spaces from author names. * Sorts all author names available. * Uses `tr` to change new lines back to "+". * Uses `sed` to remove trailing "+" form end of the line. * Uses `sed` to add spaces before and after "+". I am not proud of it but I made sure that this works on Linuxes and BSDs (OSX). --- git-pair | 16 +++++++++++++--- tests/pairing_with_names.bats | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/git-pair b/git-pair index 9eb1a22..e181b27 100755 --- a/git-pair +++ b/git-pair @@ -32,9 +32,19 @@ function gp_add_author() CURRENT_AUTHORS=$(git config user.name) [[ $( echo $CURRENT_AUTHORS | grep -e "$PAIR_NAME" ) != "" ]] && { echo "Author '$PAIR_NAME' already in list!"; exit 102; } - PAIR_NAMES="${CURRENT_AUTHORS} ${PAIR_SEPARATOR} ${PAIR_NAME}" - - git config --local user.name "$PAIR_NAMES" + ORDERED_PAIR_NAMES_ONELINE=$( + echo "${CURRENT_AUTHORS} ${PAIR_SEPARATOR} ${PAIR_NAME}" | + tr ${PAIR_SEPARATOR} '\n' | + sed 's/^ //g' | + sed 's/ $//g' | + sort | + tr '\n' ${PAIR_SEPARATOR} | + sed "s/${PAIR_SEPARATOR}$//g" | + sed "s/${PAIR_SEPARATOR}/ ${PAIR_SEPARATOR} /g" + ) + + + git config --local user.name "$ORDERED_PAIR_NAMES_ONELINE" echo "Local authors name now are:" $(git config --local user.name) } diff --git a/tests/pairing_with_names.bats b/tests/pairing_with_names.bats index d8a8154..b096de7 100644 --- a/tests/pairing_with_names.bats +++ b/tests/pairing_with_names.bats @@ -15,12 +15,12 @@ load git_environment assert_equals "Local authors name now are: Git Pair User + John Doe" "$output" } -@test "Pairing with two other user names" { +@test "Pairing with two other user names order them alphabetically" { run git-pair "John Doe" run git-pair "Jane Doe" assert_success - assert_equals "Local authors name now are: Git Pair User + John Doe + Jane Doe" "$output" + assert_equals "Local authors name now are: Git Pair User + Jane Doe + John Doe" "$output" } @test "Ending a pairing session with 'ended'" {