gitswitch 0.3.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e495045a498536c929c7ee81a01d0d4c2d2dde1622d9df5c4a09bf374fcee99e
4
+ data.tar.gz: 4f59cc61dec0c72ad4e0561743cd218b1e27af1d72750b97a1f533497cdeeeb8
5
+ SHA512:
6
+ metadata.gz: 1bcc6bed6d628b752034312cfbf58a30c9b7b8e0bc373b5d596538b7930de38f1f13e423171a9aeb2cf838da1430349797a93fed5c15c916fa0f5e835e7e6a0b
7
+ data.tar.gz: 6c0393c27ad23ddabc7d51e75616b63ef523006494303fab6176026c65acd0dbe5923fc741de46a8318ea047bb34a9cd9bb771485736958792abd43dc3484eb9
@@ -0,0 +1,32 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ paths-ignore:
8
+ - 'README.rdoc'
9
+ pull_request:
10
+ branches:
11
+ - main
12
+ paths-ignore:
13
+ - 'README.rdoc'
14
+ jobs:
15
+ test:
16
+ runs-on: ubuntu-latest
17
+ if: "contains(github.event.commits[0].message, '[ci skip]') == false"
18
+
19
+ strategy:
20
+ matrix:
21
+ ruby: [3.0.2, 2.7.4, 2.6.8, 2.5.9]
22
+
23
+ steps:
24
+ - name: Checkout
25
+ uses: actions/checkout@v2
26
+ - name: Setup Ruby
27
+ uses: ruby/setup-ruby@v1
28
+ with:
29
+ ruby-version: ${{ matrix.ruby }}
30
+ bundler-cache: true
31
+ - name: Test
32
+ run: bundle exec rake spec
data/.gitignore CHANGED
@@ -1,10 +1,6 @@
1
1
  ## MAC OS
2
2
  .DS_Store
3
3
 
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
4
  ## PROJECT::GENERAL
9
5
  coverage
10
6
  rdoc
@@ -14,3 +10,4 @@ pkg/*
14
10
  Gemfile.lock
15
11
 
16
12
  ## PROJECT::SPECIFIC
13
+ spec/tmp/.gitswitch
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
  gemspec
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = gitswitch
2
2
 
3
- Easily set/switch your current git user info for a git repo .git/config or your global ~/.gitconfig file.
3
+ Easily set/switch your current git user info for a git repo .git/config or your global ~/.gitconfig file.
4
4
 
5
5
  This gem should come in handy if you have work and personal repositories. It also might help with switching between users while pair programming.
6
6
 
@@ -53,15 +53,7 @@ Delete a current entry. [TAG] parameter is optional. If omitted, you'll see a
53
53
  # gitswitch delete [TAG]
54
54
 
55
55
  Update your global .git user instead of your current repository
56
- # gitswitch global work
56
+ # gitswitch switch work --global
57
57
 
58
58
  Show your current git user info. If you are in a git repo directory, it will show you the info associated with the current repository. Outside a repo (or if you use the --global option), it will show you the info from your global .git config.
59
59
  # gitswitch info
60
-
61
- == What's with all the aliases?
62
-
63
- I initially wrote the gem using optparse and used the '-r' style params to keep the commands as short as possible. Now, I've decided to lean toward a rake-style descriptive method of passing commands -- which meshes well with the way Thor likes to do things. But the old command line options still work fine in case you got used to it.
64
-
65
- == Copyright
66
-
67
- Copyright (c) 2010 Joe Alba. See LICENSE for details.
data/Rakefile CHANGED
@@ -4,7 +4,6 @@ require 'rspec/core/rake_task'
4
4
 
5
5
  Bundler::GemHelper.install_tasks
6
6
 
7
-
8
7
  desc 'Default: Run specs'
9
8
  task :default => :spec
10
9
 
data/bin/gitswitch CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "gitswitch"
4
- Gitswitch::CLI.start
4
+
5
+ Dry::CLI.new(Gitswitch::CLI::Commands).call
data/gitswitch.gemspec CHANGED
@@ -8,30 +8,34 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
 
10
10
  s.authors = ["Joe Alba"]
11
- s.date = %q{2010-09-27}
12
- s.default_executable = %q{gitswitch}
13
- s.homepage = %q{http://github.com/joealba/gitswitch}
11
+ s.date = %q{2012-04-29}
12
+ s.homepage = %q{https://github.com/joealba/gitswitch}
14
13
  s.description = %q{Easily switch your git name/e-mail user info -- Handy for work vs. personal and for pair programming}
15
14
  s.summary = %q{Easy git user switching}
16
15
  s.email = %q{joe@joealba.com}
17
-
16
+ s.metadata = {
17
+ "bug_tracker_uri" => "#{s.homepage}/issues",
18
+ "changelog_uri" => "#{s.homepage}/blob/main/CHANGELOG.md",
19
+ "documentation_uri" => s.homepage.to_s,
20
+ "homepage_uri" => s.homepage.to_s,
21
+ "source_code_uri" => s.homepage.to_s
22
+ }
23
+
18
24
  s.extra_rdoc_files = [
19
25
  "LICENSE",
20
26
  "README.rdoc"
21
27
  ]
22
-
28
+
23
29
  s.files = `git ls-files`.split("\n")
24
30
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
31
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
32
  s.require_paths = ["lib"]
27
33
 
28
34
  s.rdoc_options = ["--charset=UTF-8"]
29
- s.rubygems_version = %q{1.3.7}
30
-
31
-
32
- s.add_dependency('rake')
33
- s.add_dependency('thor')
34
- s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
35
35
 
36
+ s.add_dependency('rake')
37
+ s.add_dependency('dry-cli')
38
+ s.add_dependency('tty-prompt')
39
+ s.add_development_dependency(%q<rspec>, [">= 3.3.0"])
40
+ s.add_development_dependency 'simplecov'
36
41
  end
37
-
data/lib/gitswitch.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'gitswitch/version'
2
2
  require 'gitswitch/commands'
3
+ require 'gitswitch/git'
3
4
  require 'gitswitch/cli'
4
- #Gitswitch::CLI.start ## If we decide to use Thor
5
-
6
5
 
6
+ module Gitswitch
7
+ EXIT_OK = 0
8
+ EXIT_MISSING_INFO = 1
9
+ end
data/lib/gitswitch/cli.rb CHANGED
@@ -1,160 +1,207 @@
1
- require 'thor'
1
+ require "dry/cli"
2
+ require "tty-prompt"
3
+
4
+ module Gitswitch
5
+ module CLI
6
+ module Helpers
7
+ def prompt_for_email_and_name
8
+ prompt = TTY::Prompt.new
9
+ email = prompt.ask(" E-mail address: ").to_s.chomp
10
+ if email.empty?
11
+ puts "No e-mail address provided"
12
+ exit Gitswitch::EXIT_MISSING_INFO
13
+ end
2
14
 
15
+ default_name = Gitswitch::get_git_user_info({global: true})[:name]
16
+ name = prompt.ask(" Name: (ENTER to use \"" + default_name + "\") ").to_s.chomp
17
+ name = default_name if name.empty?
3
18
 
4
- class Gitswitch
5
- class CLI < Thor
19
+ if name.empty?
20
+ puts "No name provided"
21
+ end
6
22
 
23
+ return [email, name]
24
+ end
25
+ end
7
26
 
8
- ######################################################################
9
- desc "init", "Initialize your .gitswitch file"
10
- def init
11
- if !Gitswitch::gitswitch_file_exists
12
- if yes?("Gitswitch users file ~/.gitswitch not found. Would you like to create one? (y/n): ")
13
- Gitswitch::new.create_fresh_gitswitch_file
14
- else
15
- puts "Ok, that's fine. Exiting."
16
- exit
17
- end
18
- else
19
- if yes?("Gitswitch users file ~/.gitswitch already exists. Would you like to wipe it out and create a fresh one? (y/n): ")
20
- Gitswitch::new.create_fresh_gitswitch_file
27
+ module Commands
28
+ extend Dry::CLI::Registry
29
+
30
+ class Init < Dry::CLI::Command
31
+ desc "Initialize your .gitswitch file"
32
+
33
+ def call(*)
34
+ prompt = TTY::Prompt.new
35
+
36
+ if !Gitswitch::gitswitch_file_exists
37
+ if prompt.yes?("Gitswitch users file ~/.gitswitch not found. Would you like to create one? (y/n):")
38
+ Gitswitch.create_fresh_gitswitch_file
39
+ else
40
+ puts "Ok, that's fine. Exiting."
41
+ exit Gitswitch::EXIT_OK
42
+ end
43
+ else
44
+ if prompt.yes?("Gitswitch users file ~/.gitswitch already exists. Would you like to wipe it out and create a fresh one? (y/n):")
45
+ Gitswitch.create_fresh_gitswitch_file
46
+ end
47
+ end
48
+
49
+ # Grab the current global info to drop into the default slot -- if available
50
+ user = Gitswitch.get_git_user_info({global: true})
51
+ if user[:name].empty? && user[:email].empty?
52
+ puts "No global git user.name and user.email configurations were found. Set up a default now."
53
+ add('default')
54
+ else
55
+ puts "Adding your global .gitconfig user info to the \"default\" tag..."
56
+ Gitswitch.set_gitswitch_entry('default', user[:email], user[:name])
57
+ end
58
+
59
+ exit Gitswitch::EXIT_OK
21
60
  end
22
61
  end
23
62
 
24
- # Grab the current global info to drop into the default slot -- if available
25
- user = Gitswitch::get_git_user_info({:global => true})
26
- if user[:name].empty? && user[:email].empty?
27
- puts "No global git user.name and user.email configurations were found. Set up a default now."
28
- add('default')
29
- else
30
- puts "Adding your global .gitconfig user info to the \"default\" tag..."
31
- Gitswitch::new.set_gitswitch_entry('default', user[:email], user[:name])
63
+ class Info < Dry::CLI::Command
64
+ desc "Show the current git user"
65
+ option :global, type: :boolean, default: false, desc: "Show global config"
66
+
67
+ def call(**options)
68
+ puts Gitswitch.current_user_info(options)
69
+
70
+ exit Gitswitch::EXIT_OK
71
+ end
32
72
  end
33
73
 
34
- end
74
+ class List < Dry::CLI::Command
75
+ desc "Show all the git user tags you have configured"
35
76
 
36
-
37
- ######################################################################
38
- desc "version", "Show the gitswitch version"
39
- map ["-v","--version"] => :version
40
- def version
41
- puts Gitswitch::VERSION
42
- end
77
+ def call(*)
78
+ puts Gitswitch.list_users
43
79
 
80
+ exit Gitswitch::EXIT_OK
81
+ end
82
+ end
44
83
 
45
- ######################################################################
46
- desc "info", "Show the current git user"
47
- map "-i" => :info
48
- method_option :global, :type => :boolean
49
- def info
50
- puts Gitswitch.current_user_info(options)
51
- end
84
+ class Switch < Dry::CLI::Command
85
+ desc "Switch git user"
86
+ argument :tag, desc: "Tag name"
87
+ option :global, type: :boolean, default: false, desc: "Set global config"
52
88
 
89
+ def call(tag: "default", **options)
90
+ options[:global] ? Gitswitch.switch_global_user(tag) : Gitswitch.switch_repo_user(tag)
91
+ puts Gitswitch.current_user_info(options)
53
92
 
54
- ######################################################################
55
- desc "list", "Show all the git user tags you have configured"
56
- map ["-l","--list"] => :list
57
- def list
58
- puts Gitswitch.new.list_users
59
- end
93
+ exit Gitswitch::EXIT_OK
94
+ end
95
+ end
60
96
 
97
+ class Global < Dry::CLI::Command
98
+ desc "Switch the global git user info (your ~/.gitconfig file)"
99
+ argument :tag, desc: "Tag name"
61
100
 
62
- ######################################################################
63
- desc "switch [TAG]", "Switch git user"
64
- map "-r" => :switch
65
- method_option :global, :type => :boolean, :aliases => ["-s","--global"] ## To support the deprecated behavior
66
- method_option :repository, :type => :boolean, :aliases => "-r"
67
- def switch(tag = 'default')
68
- options[:global] ? global(tag) : Gitswitch.new.switch_repo_user(tag)
69
- puts Gitswitch.current_user_info(options)
70
- end
101
+ def call(tag: "default", **)
102
+ Gitswitch.switch_global_user(tag)
71
103
 
104
+ exit Gitswitch::EXIT_OK
105
+ end
106
+ end
72
107
 
73
- ######################################################################
74
- desc "global [TAG]", "Switch global git user (your ~/.gitconfig file)"
75
- map "-s" => :global
76
- def global(tag = 'default')
77
- Gitswitch.new.switch_global_user(tag)
78
- end
108
+ class Add < Dry::CLI::Command
109
+ include ::Gitswitch::CLI::Helpers
110
+
111
+ desc "Add a new tagged user entry"
112
+ argument :tag, desc: "Tag name"
79
113
 
114
+ def call(tag: "", **)
115
+ tag = tag.gsub(/\W+/,'')
116
+ if (tag.nil? || tag.empty?)
117
+ prompt = TTY::Prompt.new
118
+ tag = prompt.ask("Enter a tag to describe this git user entry: ").to_s.gsub(/\W+/,'')
119
+ end
80
120
 
81
- ######################################################################
82
- desc "add [TAG]", "Add a new tagged user entry"
83
- map ["-a","--add"] => :add
84
- def add(tag = '')
85
- gs = Gitswitch.new
121
+ if tag.empty?
122
+ puts "You must enter a short tag to describe the git user entry you would like to save."
123
+ exit Gitswitch::EXIT_MISSING_INFO
124
+ end
86
125
 
87
- tag.gsub!(/\W+/,'')
88
- tag = ask("Enter a tag to describe this git user entry: ").gsub(/\W+/,'') if (tag.nil? || tag.empty?)
89
-
90
- if tag.empty?
91
- puts "You must enter a short tag to describe the git user entry you would like to save."
92
- exit
126
+ puts "Adding a new gitswitch user entry for tag '#{tag}'"
127
+ (email, name) = prompt_for_email_and_name
128
+ Gitswitch.set_gitswitch_entry(tag, email, name)
129
+ exit Gitswitch::EXIT_OK
130
+ end
93
131
  end
94
132
 
95
- puts "Adding a new gitswitch user entry for tag '#{tag}'"
96
- (email, name) = prompt_for_email_and_name
97
- gs.set_gitswitch_entry(tag, email, name)
98
- end
133
+ class Delete < Dry::CLI::Command
134
+ desc "Delete a tagged user entry"
99
135
 
136
+ def call(tag: "")
137
+ tag_table = Gitswitch.get_tag_display
100
138
 
101
- ######################################################################
102
- desc "update [TAG]", "Update a tagged user entry"
103
- map ["-o","--overwrite"] => :add
104
- def update(tag = '')
105
- gs = Gitswitch.new
106
- tag_table = gs.get_tag_display
107
-
108
- tag.gsub!(/\W+/,'')
109
- if (tag.nil? || tag.empty?)
110
- tag = ask("Which tag would you like to update: \n#{tag_table}").gsub(/\W+/,'')
111
- end
112
-
113
- puts "Updating #{tag} entry..."
114
- (email, name) = prompt_for_email_and_name
115
- Gitswitch.new.set_gitswitch_entry(tag, email, name)
116
- end
139
+ tag = tag.gsub(/\W+/, '')
140
+ if (tag.empty?)
141
+ puts tag_table
142
+ prompt = TTY::Prompt.new
143
+ tag = prompt.ask("\nWhich tag would you like to delete: ").to_s.gsub(/\W+/,'')
144
+ end
145
+
146
+ if tag.empty?
147
+ puts "No tag chosen"
148
+ exit Gitswitch::EXIT_MISSING_INFO
149
+ end
117
150
 
151
+ Gitswitch.delete_gitswitch_entry(tag)
118
152
 
119
- ######################################################################
120
- desc "delete [TAG]", "Delete a tagged user entry"
121
- def delete(tag = '')
122
- gs = Gitswitch.new
153
+ puts Gitswitch.get_tag_display
123
154
 
124
- tag_table = gs.get_tag_display
125
-
126
- tag.gsub!(/\W+/,'')
127
- if (tag.nil? || tag.empty?)
128
- tag = ask("Which tag would you like to delete: \n#{tag_table}").gsub(/\W+/,'')
155
+ exit Gitswitch::EXIT_OK
156
+ end
129
157
  end
130
158
 
131
- Gitswitch.new.delete_gitswitch_entry(tag)
132
- end
159
+ class Update < Dry::CLI::Command
160
+ include ::Gitswitch::CLI::Helpers
133
161
 
162
+ desc "Update a tagged user entry"
163
+ argument :tag, desc: "Tag name"
134
164
 
135
- private
136
-
165
+ def call(tag: "", **)
166
+ tag_table = Gitswitch.get_tag_display
137
167
 
138
- ######################################################################
139
- def prompt_for_email_and_name
140
- email = ask(" E-mail address: ").chomp
141
- ## TODO: Validate e-mail
142
- if email.nil?
143
- puts "No name provided"
144
- exit
168
+ tag = tag.gsub(/\W+/, '')
169
+ if (tag.empty?)
170
+ prompt = TTY::Prompt.new
171
+ tag = prompt.ask("Which tag would you like to update: \n#{tag_table}\nTag: ").to_s.gsub(/\W+/,'')
172
+ end
173
+
174
+ if tag.empty?
175
+ puts "No tag chosen"
176
+ exit Gitswitch::EXIT_MISSING_INFO
177
+ end
178
+
179
+ puts "Updating #{tag} entry..."
180
+ (email, name) = prompt_for_email_and_name
181
+ Gitswitch.set_gitswitch_entry(tag, email, name)
182
+
183
+ exit Gitswitch::EXIT_OK
184
+ end
145
185
  end
146
-
147
- name = ask(" Name: (ENTER to use \"" + Gitswitch::get_git_user_info({:global => true})[:name] + "\") ").chomp
148
- name = Gitswitch::get_git_user_info({:global => true})[:name] if name.empty?
149
-
150
- if name.nil?
151
- puts "No name provided"
152
- exit
186
+
187
+ class Version < Dry::CLI::Command
188
+ desc "Show the gitswitch version"
189
+
190
+ def call(*)
191
+ puts Gitswitch::VERSION
192
+
193
+ exit Gitswitch::EXIT_OK
194
+ end
153
195
  end
154
196
 
155
- return [email, name]
197
+ register "add", Add, aliases: ["-a", "--add"]
198
+ register "delete", Delete, aliases: ["remove"]
199
+ register "info", Info, aliases: ["-i", "--info"]
200
+ register "init", Init
201
+ register "list", List, aliases: ["-l", "--list"]
202
+ register "switch", Switch, aliases: ["-r"]
203
+ register "update", Update, aliases: ["-o","--overwrite"]
204
+ register "version", Version, aliases: ["-v", "--version"]
156
205
  end
157
-
158
-
159
206
  end
160
- end
207
+ end
@@ -1,45 +1,42 @@
1
1
  require 'yaml'
2
- require 'shellwords' if !String.new.methods.include?('shellescape')
3
-
4
-
5
- class Gitswitch
6
- GITSWITCH_CONFIG_FILE = File.join ENV["HOME"], ".gitswitch"
7
- GIT_BIN = '/usr/bin/env git'
8
-
9
- attr_reader :users
10
2
 
3
+ module Gitswitch
4
+ def self.gitswitch_config_file
5
+ ENV['GITSWITCH_CONFIG_FILE'] || File.join(ENV["HOME"], ".gitswitch")
6
+ end
11
7
 
12
8
  ##############################################################
13
- def initialize
14
- @users = {}
9
+ def self.users
10
+ @users ||= load_users
11
+ end
12
+
13
+ def self.load_users
14
+ user_hash = {}
15
15
  if Gitswitch::gitswitch_file_exists
16
- @users = YAML::load_file GITSWITCH_CONFIG_FILE
17
- if @users.nil?
18
- puts "Error loading .gitswitch file. Delete the file and start fresh."
19
- exit
20
- end
16
+ user_hash = YAML::load_file gitswitch_config_file
21
17
  end
18
+ user_hash
22
19
  end
23
-
24
20
 
25
- ##############################################################
26
- # Create a .gitswitch file with the current user defaults
27
- def create_fresh_gitswitch_file
28
- @users = {}
29
- save_gitswitch_file
21
+ ## Create a .gitswitch file with the current user defaults
22
+ def self.create_fresh_gitswitch_file
23
+ save_gitswitch_file({})
30
24
  end
31
25
 
32
26
  def self.gitswitch_file_exists
33
- File.exists? GITSWITCH_CONFIG_FILE
27
+ File.exists? gitswitch_config_file
34
28
  end
35
29
 
36
- def save_gitswitch_file
37
- if fh = File.open(GITSWITCH_CONFIG_FILE, 'w')
38
- fh.write(@users.to_yaml)
39
- fh.close
40
- else
41
- puts "ERROR: Could not open/write the gitswitch config file: #{GITSWITCH_CONFIG_FILE}"
30
+ def self.save_gitswitch_file(users_hash)
31
+ begin
32
+ File.open(gitswitch_config_file, 'w') do |fh|
33
+ fh.write(users_hash.to_yaml)
34
+ end
35
+ rescue
36
+ warn "ERROR: Could not open/write the gitswitch config file: #{gitswitch_config_file}: #{$!}"
37
+ exit
42
38
  end
39
+ @users = nil
43
40
  end
44
41
 
45
42
 
@@ -49,68 +46,57 @@ class Gitswitch
49
46
  # * +tag+ - Required. The tag you want to add to your .gitswitch file
50
47
  # * +email+ - Required
51
48
  # * +name+ - Required
52
- def set_gitswitch_entry(tag, email, name)
53
- @users[tag] = {:name => name, :email => email}
54
- save_gitswitch_file
49
+ def self.set_gitswitch_entry(tag, email, name)
50
+ users[tag] = {name: name, email: email}
51
+ save_gitswitch_file(users)
55
52
  end
56
53
 
57
- def delete_gitswitch_entry(tag)
54
+ def self.delete_gitswitch_entry(tag)
58
55
  if tag == 'default'
59
56
  puts "Cannot delete the default tag. Use the update command instead"
60
57
  exit
61
58
  end
62
- @users.delete(tag)
63
- save_gitswitch_file
59
+ users.delete(tag)
60
+ save_gitswitch_file(users)
64
61
  end
65
62
 
66
- def get_user(tag)
67
- if !@users.empty? && @users[tag] && !@users[tag].empty?
68
- @users[tag]
63
+ def self.get_user(tag)
64
+ ## TODO: Stop coding so defensively.
65
+ if !users.empty? && users[tag] && !users[tag].empty?
66
+ users[tag]
69
67
  end
70
68
  end
71
69
 
72
- def get_tags
73
- @users.keys
70
+ def self.get_tags
71
+ users.keys
74
72
  end
75
73
 
76
- def get_tag_display
77
- max_length = @users.keys.sort{|x,y| y.length <=> x.length }.first.length
78
- @users.each_pair.map {|key,value| sprintf(" %#{max_length}s %s\n", key, value[:email]) }
74
+ def self.get_tag_display
75
+ users.map do |key, user|
76
+ item = "#{key}:\n"
77
+ item << " Name: #{user[:name]}\n" if !user[:name].to_s.empty?
78
+ item << " E-mail: #{user[:email]}\n"
79
+ item
80
+ end.join("\n")
79
81
  end
80
82
 
81
- def list_users
82
- response = ''
83
- response << "\nCurrent git user options --\n"
84
- @users.each do |key, user|
85
- response << "#{key}:\n"
86
- response << " Name: #{user[:name]}\n" if !user[:name].to_s.empty?
87
- response << " E-mail: #{user[:email]}\n\n"
88
- end
83
+ def self.list_users
84
+ response = "\nCurrent git user options --\n"
85
+ response << get_tag_display
86
+
89
87
  response
90
88
  end
91
89
 
92
90
  ## Return active user information.
93
91
  ## If you're in a git repo, show that user info. Otherwise, display the global info.
94
92
  def self.current_user_info(options = {})
95
- response = !Gitswitch::in_a_git_repo || options[:global] ? "Your git user information from your global config:\n" : "Your git user information from the current repository:\n"
96
93
  current_git_user = Gitswitch::get_git_user_info(options)
97
- response << "Name: #{current_git_user[:name]}\n"
98
- response << "E-mail: #{current_git_user[:email]}\n"
99
- response
100
- end
101
-
102
- def self.in_a_git_repo
103
- %x(#{GIT_BIN} status 2>&1 | head -n 1).to_s =~ /^fatal/i ? false : true
104
- end
105
94
 
95
+ response = !Gitswitch::in_a_git_repo || options[:global] ? "Your git user information from your global config:\n" : "Your git user information from the current repository:\n"
96
+ response << "Name: #{current_git_user[:name]}\n"
97
+ response << "E-mail: #{current_git_user[:email]}\n"
106
98
 
107
- ##############################################################
108
- def git_config(user, options = {})
109
- git_args = 'config --replace-all'
110
- git_args += ' --global' if options[:global]
111
-
112
- %x(#{GIT_BIN} #{git_args} user.email #{user[:email].to_s.shellescape})
113
- %x(#{GIT_BIN} #{git_args} user.name #{user[:name].to_s.shellescape}) if !user[:name].to_s.empty?
99
+ response
114
100
  end
115
101
 
116
102
 
@@ -118,21 +104,21 @@ class Gitswitch
118
104
  # Switch git user in your global .gitconfig file
119
105
  # ==== Parameters
120
106
  # * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
121
- def switch_global_user tag = "default"
107
+ def self.switch_global_user(tag = "default")
122
108
  if user = get_user(tag)
123
109
  puts "Switching your .gitconfig user info to \"#{tag}\" tag (#{user[:name]} <#{user[:email]}>)."
124
- git_config(user, {:global => true})
110
+ git_config(user, {global: true})
125
111
  else
126
112
  puts "ERROR: Could not find info for tag \"#{tag}\" in your .gitswitch file"
127
113
  end
128
114
  end
129
115
 
130
116
 
117
+ ##############################################################
131
118
  # Set the git user information for current repository
132
119
  # ==== Parameters
133
120
  # * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
134
- def switch_repo_user(tag = "default")
135
- ## TODO: See if we're actually in a git repo
121
+ def self.switch_repo_user(tag = "default")
136
122
  if !Gitswitch::in_a_git_repo
137
123
  puts "You do not appear to currently be in a git repository directory"
138
124
  false
@@ -146,19 +132,17 @@ class Gitswitch
146
132
  end
147
133
  end
148
134
 
135
+ ##############################################################
136
+ # TODO: Straight delegation through to Gitswitch::Git
137
+ def self.in_a_git_repo
138
+ Gitswitch::Git.in_a_git_repo
139
+ end
149
140
 
150
-
151
- private
141
+ def self.git_config(user, options = {})
142
+ Gitswitch::Git.git_config(user, options)
143
+ end
152
144
 
153
- # Show the current git user info
154
145
  def self.get_git_user_info(options = {})
155
- git_args = 'config --get'
156
- git_args += ' --global' if options[:global]
157
-
158
- {
159
- :name => %x(#{GIT_BIN} #{git_args} user.name).to_s.chomp,
160
- :email => %x(#{GIT_BIN} #{git_args} user.email).to_s.chomp
161
- }
146
+ Gitswitch::Git.get_git_user_info(options)
162
147
  end
163
-
164
148
  end
@@ -0,0 +1,33 @@
1
+ require 'shellwords' if !String.new.methods.include?('shellescape')
2
+
3
+ module Gitswitch
4
+ module Git
5
+ GIT_BIN = '/usr/bin/env git'
6
+
7
+ def self.version
8
+ %x(#{GIT_BIN} --version).to_s.gsub(/^git version\s*/, '')
9
+ end
10
+
11
+ def self.in_a_git_repo
12
+ %x(#{GIT_BIN} status 2>&1 | head -n 1).to_s =~ /^fatal/i ? false : true
13
+ end
14
+
15
+ def self.git_config(user, options = {})
16
+ git_args = 'config --replace-all'
17
+ git_args += ' --global' if options[:global]
18
+
19
+ %x(#{GIT_BIN} #{git_args} user.email #{user[:email].to_s.shellescape})
20
+ %x(#{GIT_BIN} #{git_args} user.name #{user[:name].to_s.shellescape}) if !user[:name].to_s.empty?
21
+ end
22
+
23
+ def self.get_git_user_info(options = {})
24
+ git_args = 'config --get'
25
+ git_args += ' --global' if options[:global]
26
+
27
+ {
28
+ name: %x(#{GIT_BIN} #{git_args} user.name).to_s.chomp,
29
+ email: %x(#{GIT_BIN} #{git_args} user.email).to_s.chomp
30
+ }
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
- class Gitswitch
2
- VERSION = "0.3.2"
3
- end
1
+ module Gitswitch
2
+ VERSION = "0.5.0"
3
+ end
data/spec/git_spec.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gitswitch::Git do
4
+ it "finds the git executable" do
5
+ result = %x[#{Gitswitch::Git::GIT_BIN} --version]
6
+ expect($?.exitstatus).to eq 0
7
+ end
8
+
9
+ it "grabs the local git version" do
10
+ expect(Gitswitch::Git.version).to match /^\d+\.+\d+/ # Should start off looking like a version number
11
+ end
12
+ end
@@ -1,68 +1,57 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gitswitch do
4
-
5
4
  describe "basics" do
6
5
  it "should have a VERSION" do
7
- Gitswitch::VERSION.should_not == ''
6
+ expect(Gitswitch::VERSION).not_to eq ''
8
7
  end
9
-
10
- it "should find the git executable" do
11
- result = %x[#{Gitswitch::GIT_BIN} --version]
12
- $?.exitstatus.should == 0
13
- end
14
8
  end
15
9
 
16
-
17
10
  describe "read-only" do
18
- it "should show the current git user credentials" do
19
- Gitswitch.current_user_info.should =~ /^Your git user/
20
- end
21
- it "should show the current list of available gitswitch tags" do
22
- pending
11
+ it "shows the current list of available gitswitch tags" do
12
+ skip
23
13
  end
24
14
  end
25
15
 
26
-
27
16
  describe "write methods" do
28
-
29
- it "should allow you to add a new user entry" do
30
- pending
17
+ before :each do
18
+ Gitswitch.create_fresh_gitswitch_file
31
19
  end
32
20
 
33
- it "should allow you to update a user entry" do
34
- pending
21
+ it "allows you to add a new user entry" do
22
+ initial_user_count = Gitswitch.users.keys.count
23
+ set_test_entry
24
+ expect(Gitswitch.users.keys.count > initial_user_count).to eq true
35
25
  end
36
26
 
37
- it "should allow you to delete a user entry" do
38
- pending
27
+ it "allows you to update a user entry" do
28
+ set_test_entry
29
+ test_entry = get_test_entry
30
+ Gitswitch.set_gitswitch_entry(test_entry[0], 'testing@test.com', test_entry[2])
31
+ expect(Gitswitch.get_user(test_entry[0])[:email]).to eq 'testing@test.com'
32
+ expect(Gitswitch.get_user(test_entry[0])[:name]).to eq test_entry[2]
39
33
  end
40
34
 
41
- it "should allow you to overwrite the current .gitswitch file and start fresh" do
42
- pending
35
+ it "allows you to delete a user entry" do
36
+ set_test_entry
37
+ Gitswitch.delete_gitswitch_entry(get_test_entry[0])
38
+ expect(Gitswitch.users.keys.count).to eq 0
43
39
  end
44
40
 
45
- end
46
-
47
-
48
- describe "git proxy methods" do
49
-
50
- it "should allow you to change the global git user credentials" do
51
- pending
41
+ it "allows you to overwrite the current .gitswitch file and start fresh" do
42
+ set_test_entry
43
+ Gitswitch.create_fresh_gitswitch_file
44
+ expect(Gitswitch.users.keys.count).to eq 0
52
45
  end
53
-
54
- it "should allow you to change a specific repository's user credentials" do
55
- pending
56
- end
57
46
  end
58
47
 
59
-
60
48
  describe "weird outlier cases" do
61
-
62
49
  it "in a git repo directory with no user info specified, show the global config header and user info" do
63
- pending
50
+ skip
64
51
  end
65
-
66
52
  end
67
53
 
54
+ it "shows the current git user credentials" do
55
+ expect(Gitswitch.current_user_info).to match /^Your git user/
56
+ end
68
57
  end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,20 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'rubygems'
6
+
4
7
  require 'gitswitch'
5
8
 
6
- require 'bundler'
7
- Bundler.setup
9
+ RSpec.configure do |c|
10
+ end
8
11
 
12
+ ENV['GITSWITCH_CONFIG_FILE'] = File.join(File.dirname(__FILE__), 'tmp', '.gitswitch')
9
13
 
10
- RSpec.configure do |c|
14
+ def get_test_entry
15
+ ['test','test@null.com', 'A. Tester']
16
+ end
11
17
 
18
+ def set_test_entry
19
+ Gitswitch.set_gitswitch_entry(*get_test_entry)
12
20
  end
data/spec/tmp/.gitkeep ADDED
File without changes
metadata CHANGED
@@ -1,82 +1,101 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gitswitch
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Joe Alba
14
- autorequire:
8
+ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2010-09-27 00:00:00 -04:00
19
- default_executable: gitswitch
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2012-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
22
14
  name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
23
21
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-cli
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
27
31
  - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
33
34
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: thor
37
35
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
41
38
  - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-prompt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
47
48
  type: :runtime
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
50
56
  name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ type: :development
51
63
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
55
73
  - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 27
58
- segments:
59
- - 2
60
- - 5
61
- - 0
62
- version: 2.5.0
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
63
76
  type: :development
64
- version_requirements: *id003
65
- description: Easily switch your git name/e-mail user info -- Handy for work vs. personal and for pair programming
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Easily switch your git name/e-mail user info -- Handy for work vs. personal
84
+ and for pair programming
66
85
  email: joe@joealba.com
67
- executables:
86
+ executables:
68
87
  - gitswitch
69
88
  extensions: []
70
-
71
- extra_rdoc_files:
89
+ extra_rdoc_files:
72
90
  - LICENSE
73
91
  - README.rdoc
74
- files:
75
- - .bundle/config
76
- - .document
77
- - .gemtest
78
- - .gitignore
79
- - .rspec
92
+ files:
93
+ - ".bundle/config"
94
+ - ".document"
95
+ - ".gemtest"
96
+ - ".github/workflows/tests.yml"
97
+ - ".gitignore"
98
+ - ".rspec"
80
99
  - Gemfile
81
100
  - LICENSE
82
101
  - README.rdoc
@@ -86,43 +105,42 @@ files:
86
105
  - lib/gitswitch.rb
87
106
  - lib/gitswitch/cli.rb
88
107
  - lib/gitswitch/commands.rb
108
+ - lib/gitswitch/git.rb
89
109
  - lib/gitswitch/version.rb
110
+ - spec/git_spec.rb
90
111
  - spec/gitswitch_spec.rb
91
112
  - spec/spec_helper.rb
92
- has_rdoc: true
93
- homepage: http://github.com/joealba/gitswitch
113
+ - spec/tmp/.gitkeep
114
+ homepage: https://github.com/joealba/gitswitch
94
115
  licenses: []
95
-
96
- post_install_message:
97
- rdoc_options:
98
- - --charset=UTF-8
99
- require_paths:
116
+ metadata:
117
+ bug_tracker_uri: https://github.com/joealba/gitswitch/issues
118
+ changelog_uri: https://github.com/joealba/gitswitch/blob/main/CHANGELOG.md
119
+ documentation_uri: https://github.com/joealba/gitswitch
120
+ homepage_uri: https://github.com/joealba/gitswitch
121
+ source_code_uri: https://github.com/joealba/gitswitch
122
+ post_install_message:
123
+ rdoc_options:
124
+ - "--charset=UTF-8"
125
+ require_paths:
100
126
  - lib
101
- required_ruby_version: !ruby/object:Gem::Requirement
102
- none: false
103
- requirements:
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
104
129
  - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: 3
107
- segments:
108
- - 0
109
- version: "0"
110
- required_rubygems_version: !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
113
134
  - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
119
137
  requirements: []
120
-
121
- rubyforge_project:
122
- rubygems_version: 1.5.2
123
- signing_key:
124
- specification_version: 3
138
+ rubygems_version: 3.2.22
139
+ signing_key:
140
+ specification_version: 4
125
141
  summary: Easy git user switching
126
- test_files:
142
+ test_files:
143
+ - spec/git_spec.rb
127
144
  - spec/gitswitch_spec.rb
128
145
  - spec/spec_helper.rb
146
+ - spec/tmp/.gitkeep