From a598aece805228fcc404af7597fd5cd9d98f6d44 Mon Sep 17 00:00:00 2001 From: Sam Gleske Date: Wed, 5 Jun 2024 01:23:31 -0400 Subject: [PATCH] Release 0.11 Bugfix on git setup When setting up Git for the first time an out of order configuration can cause issues for the user. fixes #6 --- CHANGELOG.md | 8 ++++++++ README.md | 4 +++- git-idm | 22 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 722a80a..e1e2dd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# git-idm v0.11 + +- Bug fix: since initial setup assumes a certain order of `.gitconfig` sections, + then attempt to assure initial setup every time a write operation would occur + to `.gitconfig`. See issue [#6][#6]. + +[#6]: https://github.com/samrocketman/git-identity-manager/issues/6 + # git-idm v0.10 Important Notes: diff --git a/README.md b/README.md index afa67b3..14fb4d0 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,10 @@ attempts to make it less painful. # Requirements -- Git 2.13 or later. +- Git 2.18 or later. - [`git 2.10` or later because of `core.sshCommand`][git-2.10]. - [`git 2.13` or later because of `includeIf..path`][git-2.13]. + - [`git 2.18` or later because of git-config bug][git-2.18]. - GNU bash - awk (BSD or GNU awk recommended) - sed (BSD or GNU sed recommended) @@ -94,4 +95,5 @@ Verify the identity has switched with `git config user.email`. [build-status]: https://travis-ci.org/samrocketman/git-identity-manager [git-2.10]: https://github.com/git/git/blob/v2.10.0/Documentation/RelNotes/2.10.0.txt#L83-L84 [git-2.13]: https://github.com/git/git/blob/v2.13.0/Documentation/RelNotes/2.13.0.txt#L127-L130 +[git-2.18]: https://github.com/git/git/blob/53f9a3e157dbbc901a02ac2c73346d375e24978c/Documentation/RelNotes/2.18.0.txt#L379-L384 [pbs]: https://www.pbs.org/newshour/health/back-pain-industry-taking-patients-unhealthy-ride diff --git a/git-idm b/git-idm index cd34864..7e06752 100755 --- a/git-idm +++ b/git-idm @@ -3,12 +3,24 @@ #License: MIT #Project URL: https://github.com/samrocketman/git-identity-manager -version='0.10' +version='0.11' -if ! git --version | awk 'BEGIN { FS="." }; $2 < 13 { exit(1) }'; then - echo 'WARNING: "git --version" is older than Git 2.13. git-idm will have unexpected behavior.' >&2 +if ! git --version | awk 'BEGIN { FS="." }; $2 < 18 { exit(1) }'; then + echo 'WARNING: "git --version" is older than Git 2.18. git-idm will have unexpected behavior.' >&2 fi +function initial_setup() { + if [ ! -f ~/.gitconfig ]; then + touch ~/.gitconfig + fi + if ! { git config --global -l | grep '^user\.' || grep -F '[user]' ~/.gitconfig; } > /dev/null; then + echo '[user]' >> ~/.gitconfig + fi + if ! { git config --global -l | grep '^include\.' || grep -F '[include]' ~/.gitconfig; } > /dev/null; then + echo '[include]' >> ~/.gitconfig + fi +} + function echo_version() { echo "git idm v${version}" >&2 } @@ -421,6 +433,7 @@ EOF echo 'ERROR: options --name, --email, and --key or --ssh-command are required when adding an identity for the first time.' >&2 exit 1 fi + initial_setup if [ -n "${NAME}" ]; then run_command git config --global gitidm."${id}".name "${NAME}" fi @@ -444,6 +457,7 @@ EOF no_ssh_key_warning "${id}" ;; copy-key-to) + initial_setup SSH_KEY="$(git config --global --get gitidm."${FROM_ID}".sshKey)" SSH_COMMAND="$(git config --global --get gitidm."${FROM_ID}".sshCommand)" if [ -z "${SSH_KEY:-}" ] || [ -z "${SSH_COMMAND:-}" ]; then @@ -490,6 +504,7 @@ EOF echo 'ERROR: git idm track --directory requires --directory option to be passed. See "git idm help".' >&2 exit 1 fi + initial_setup add_identity_file "${id}" run_command git config --global includeIf."gitdir:${DIRECTORY}".idm "${id}" run_command git config --global includeIf."gitdir:${DIRECTORY}".path ~/".gitconfig_idm_${id}" @@ -508,6 +523,7 @@ EOF echo "ERROR: Identity ${id} does not exist. See 'git idm list' or 'git idm help'." >&2 exit 1 fi + initial_setup NAME="$(git config --global --get gitidm."${id}".name)" EMAIL="$(git config --global --get gitidm."${id}".email)" if [ -n "${NAME}" ]; then