From 62b534ea67d68a462edce3e22059ea755881f261 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 22 Jan 2019 13:08:13 -0500 Subject: [PATCH 1/3] Use sshCommand to switch ssh keys The original implementation used `ssh-add -D` to remove all ssh keys from the ssh-agent and then re-add the one associated with the selected profile. This new approach is much more elegant, using the `core.sshCommand` config introduced in git 2.10 to adjust the ssh command, specifying which ssh key to use, at the git config level. Like other git config settings, this may be applied locally or globally and really fits with how git_switch handles the rest of the settings. Fixes #17 --- lib/git_switch/config.rb | 12 ++++++---- lib/git_switch/switcher.rb | 26 +++++++++++---------- spec/lib/git_switch/config_spec.rb | 34 ++++++++++++++++++++++++++++ spec/lib/git_switch/switcher_spec.rb | 17 +++----------- 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/lib/git_switch/config.rb b/lib/git_switch/config.rb index 8d6196b..3c3f8f7 100644 --- a/lib/git_switch/config.rb +++ b/lib/git_switch/config.rb @@ -13,10 +13,6 @@ def get_profile(args) args.detect {|a| !a.start_with? '-'} end - def name - profiles[selected_profile]["name"] - end - def username profiles[selected_profile]["username"] end @@ -25,10 +21,18 @@ def email profiles[selected_profile]["email"] end + def name + profiles[selected_profile]["name"] + end + def ssh profiles[selected_profile]["ssh"] end + def ssh_command + "ssh -i #{ssh}" + end + def profile @selected_profile end diff --git a/lib/git_switch/switcher.rb b/lib/git_switch/switcher.rb index cf5a921..2b37cd3 100644 --- a/lib/git_switch/switcher.rb +++ b/lib/git_switch/switcher.rb @@ -5,7 +5,7 @@ module GitSwitch class Switcher attr_reader :config, :options delegate :usage?, :config?, :list?, :global?, to: :options - delegate :profile, :name, :username, :email, :ssh, :print_list, :configure!, :valid_profile?, to: :config + delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config def initialize(args) raise ArgumentError unless args.is_a? Array @@ -38,21 +38,19 @@ def git_repo? def set! return unless valid_profile? && git_repo? - flag = global? ? '--global' : '' - - set_git_config(flag) + set_git_config set_ssh - print_settings(flag) + print_settings end def print_usage puts usage end - def print_settings(flag = '') + def print_settings if options.verbose? puts "\nGit Config:" - puts `git config #{flag} -l --show-origin | grep user` + puts `git config #{git_config_flag} -l --show-origin | grep user` puts "\nSSH:" puts `ssh-add -l` else @@ -62,14 +60,18 @@ def print_settings(flag = '') private - def set_git_config(flag) - `git config #{flag} user.name "#{name}"` - `git config #{flag} user.username "#{username}"` - `git config #{flag} user.email "#{email}"` + def git_config_flag + @git_config_flag ||= global? ? '--global' : '' + end + + def set_git_config + `git config #{git_config_flag} user.name "#{name}"` + `git config #{git_config_flag} user.username "#{username}"` + `git config #{git_config_flag} user.email "#{email}"` end def set_ssh - `ssh-add -D` + `git config #{git_config_flag} core.sshCommand "#{ssh_command}"` `ssh-add #{ssh}` end diff --git a/spec/lib/git_switch/config_spec.rb b/spec/lib/git_switch/config_spec.rb index c204775..a84e1fc 100644 --- a/spec/lib/git_switch/config_spec.rb +++ b/spec/lib/git_switch/config_spec.rb @@ -1,6 +1,40 @@ require 'spec_helper' RSpec.describe GitSwitch::Config do + describe 'profile attributes' do + let(:config) { GitSwitch::Config.new(['personal']) } + + describe '#username' do + it 'returns the username for the selected profile' do + expect(config.username).to eq 'johnnyfive' + end + end + + describe '#email' do + it 'returns the email for the selected profile' do + expect(config.email).to eq 'me@johnsmith.com' + end + end + + describe '#name' do + it 'returns the name for the selected profile' do + expect(config.name).to eq 'Johnny Smith' + end + end + + describe '#ssh' do + it 'returns the ssh path for the selected profile' do + expect(config.ssh).to eq '~/.ssh/id_rsa' + end + end + + describe '#ssh_command' do + it 'returns the ssh command for the selected profile' do + expect(config.ssh_command).to eq 'ssh -i ~/.ssh/id_rsa' + end + end + end + describe 'valid_profile?' do context 'when profile is configured' do let(:config) { GitSwitch::Config.new(['personal']) } diff --git a/spec/lib/git_switch/switcher_spec.rb b/spec/lib/git_switch/switcher_spec.rb index c473b3e..4d01178 100644 --- a/spec/lib/git_switch/switcher_spec.rb +++ b/spec/lib/git_switch/switcher_spec.rb @@ -210,20 +210,9 @@ let(:switcher) { GitSwitch::Switcher.new(['personal']) } - context 'when run with -g flag' do - let(:switcher) { GitSwitch::Switcher.new(['personal', '-g']) } - it 'calls set_git_config with global flag' do - expect(switcher).to receive(:set_git_config).with('--global') - switcher.run - end - end - - context 'when run with --global flag' do - let(:switcher) { GitSwitch::Switcher.new(['personal', '--global']) } - it 'calls set_git_config with global flag' do - expect(switcher).to receive(:set_git_config).with('--global') - switcher.run - end + it 'calls set_git_config' do + expect(switcher).to receive(:set_git_config) + switcher.run end it 'calls set_ssh' do From a9a902d0c990d9de14fac8d4693261bf95b560b7 Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 22 Jan 2019 14:31:30 -0500 Subject: [PATCH 2/3] Update README with prerequisites Fixes #11 and #47 --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index f9f25ca..1fa9f20 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,12 @@ Git Switch is a command line utility to easily switch between multiple git profiles. It uses a `.gitswitch` YAML file to configure each profile (name, username, and email) and ssh key. +## Prerequisites + +This gem has been developed for, and only tested on, Mac OS. + +Additionally, it requires git version 2.10+. Run `git --version` to check your currently install version. If you are using homebrew, you can install a newer version of git with `brew upgrade git`. + ## Installation This gem is not intended to be installed via a Gemfile. Instead, install it yourself: From d76c2a610d57e8a5454fffa19f8eb26d0d7230eb Mon Sep 17 00:00:00 2001 From: "Randall Reed, Jr" Date: Tue, 22 Jan 2019 14:19:19 -0500 Subject: [PATCH 3/3] Bump version --- lib/git_switch/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git_switch/version.rb b/lib/git_switch/version.rb index 26ac620..61b4eb7 100644 --- a/lib/git_switch/version.rb +++ b/lib/git_switch/version.rb @@ -1,3 +1,3 @@ module GitSwitch - VERSION = "0.4.0" + VERSION = "0.4.1" end