gitswitch 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -9
- data/Gemfile +1 -11
- data/README.rdoc +24 -8
- data/Rakefile +3 -33
- data/bin/gitswitch +1 -1
- data/gitswitch.gemspec +19 -40
- data/lib/gitswitch/cli.rb +150 -0
- data/lib/gitswitch/commands.rb +171 -0
- data/lib/gitswitch/version.rb +3 -0
- data/lib/gitswitch.rb +4 -248
- data/spec/gitswitch_spec.rb +56 -4
- metadata +53 -9
- data/Gemfile.lock +0 -31
- data/VERSION +0 -1
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@ Easily set/switch your current git user info for a git repo .git/config or your
|
|
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
|
|
7
|
-
Would you rather type "git config user.email 'me@work.com'", etc. or type "gitswitch
|
7
|
+
Would you rather type "git config user.email 'me@work.com'", etc. or type "gitswitch switch work"? If you answered the latter, check out this gem.
|
8
8
|
|
9
9
|
== Install
|
10
10
|
|
@@ -12,8 +12,8 @@ Would you rather type "git config user.email 'me@work.com'", etc. or type "gitsw
|
|
12
12
|
|
13
13
|
== Usage
|
14
14
|
|
15
|
-
|
16
|
-
# gitswitch
|
15
|
+
First, establish a default ~/.gitswitch file using the git info you already have in your ~/.gitconfig file
|
16
|
+
# gitswitch init
|
17
17
|
|
18
18
|
Gitswitch users file ~/.gitswitch not found. Would you like to create one? (y/n): y
|
19
19
|
Adding your global .gitconfig user info to the "default" tag...
|
@@ -21,22 +21,22 @@ Simply run the script to establish a default ~/.gitswitch file using the git inf
|
|
21
21
|
Name: Joe Alba
|
22
22
|
E-mail: joe@home.com
|
23
23
|
|
24
|
-
|
25
|
-
# gitswitch
|
24
|
+
Now, add a new git user tag
|
25
|
+
# gitswitch add
|
26
26
|
|
27
27
|
Enter a tag to describe this git user entry: work
|
28
28
|
E-mail address: jalba@work.com
|
29
29
|
Name (ENTER to use "Joe Alba"): Joseph M. Alba
|
30
30
|
|
31
31
|
Set specific user info to your current git repository by updating .git/config
|
32
|
-
# gitswitch
|
32
|
+
# gitswitch switch work # Update .git/config with your 'work' git user info
|
33
33
|
Switching git user to "work" tag for the current repository (Joseph M. Alba <jalba@work.com>).
|
34
34
|
|
35
|
-
# gitswitch
|
35
|
+
# gitswitch switch # User info from your 'default' tag is used if you don't specify a tag
|
36
36
|
Switching git user to "default" tag for the current repository (Joe Alba <joe@home.com>).
|
37
37
|
|
38
38
|
List all the gitswitch user tags/info you have stored
|
39
|
-
# gitswitch
|
39
|
+
# gitswitch list
|
40
40
|
|
41
41
|
Current git user options --
|
42
42
|
default:
|
@@ -46,6 +46,22 @@ List all the gitswitch user tags/info you have stored
|
|
46
46
|
Name: Joseph M. Alba
|
47
47
|
E-mail: jalba@work.com
|
48
48
|
|
49
|
+
Update a current entry. [TAG] parameter is optional. If omitted, you'll see a list.
|
50
|
+
# gitswitch update [TAG]
|
51
|
+
|
52
|
+
Delete a current entry. [TAG] parameter is optional. If omitted, you'll see a list.
|
53
|
+
# gitswitch delete [TAG]
|
54
|
+
|
55
|
+
Update your global .git user instead of your current repository
|
56
|
+
# gitswitch global work
|
57
|
+
|
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
|
+
# 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
|
+
|
49
65
|
== Copyright
|
50
66
|
|
51
67
|
Copyright (c) 2010 Joe Alba. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,37 +1,8 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require 'rspec/core'
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec'
|
4
3
|
require 'rspec/core/rake_task'
|
5
4
|
|
6
|
-
|
7
|
-
begin
|
8
|
-
require 'jeweler'
|
9
|
-
Jeweler::Tasks.new do |gem|
|
10
|
-
gem.name = "gitswitch"
|
11
|
-
gem.summary = %Q{Easy git user switching}
|
12
|
-
gem.description = %Q{Easily switch your git name/e-mail user info -- Handy for work vs. personal and for pair programming}
|
13
|
-
gem.email = "joe@joealba.com"
|
14
|
-
gem.homepage = "http://github.com/joealba/gitswitch"
|
15
|
-
gem.authors = ["Joe Alba"]
|
16
|
-
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
17
|
-
# gem.add_development_dependency "rspec", ">= 0"
|
18
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
-
end
|
20
|
-
Jeweler::GemcutterTasks.new
|
21
|
-
rescue LoadError
|
22
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
require 'rake/rdoctask'
|
27
|
-
Rake::RDocTask.new do |rdoc|
|
28
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
29
|
-
|
30
|
-
rdoc.rdoc_dir = 'rdoc'
|
31
|
-
rdoc.title = "gitswitch #{version}"
|
32
|
-
rdoc.rdoc_files.include('README*')
|
33
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
34
|
-
end
|
5
|
+
Bundler::GemHelper.install_tasks
|
35
6
|
|
36
7
|
|
37
8
|
desc 'Default: Run specs'
|
@@ -42,4 +13,3 @@ RSpec::Core::RakeTask.new(:spec) do |t|
|
|
42
13
|
t.pattern = FileList["spec/**/*_spec.rb"]
|
43
14
|
end
|
44
15
|
|
45
|
-
|
data/bin/gitswitch
CHANGED
data/gitswitch.gemspec
CHANGED
@@ -1,58 +1,37 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "gitswitch/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
6
|
s.name = %q{gitswitch}
|
8
|
-
s.version =
|
7
|
+
s.version = Gitswitch::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
9
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
10
|
s.authors = ["Joe Alba"]
|
12
|
-
s.date = %q{2010-09-
|
11
|
+
s.date = %q{2010-09-27}
|
13
12
|
s.default_executable = %q{gitswitch}
|
13
|
+
s.homepage = %q{http://github.com/joealba/gitswitch}
|
14
14
|
s.description = %q{Easily switch your git name/e-mail user info -- Handy for work vs. personal and for pair programming}
|
15
|
+
s.summary = %q{Easy git user switching}
|
15
16
|
s.email = %q{joe@joealba.com}
|
16
|
-
|
17
|
+
|
17
18
|
s.extra_rdoc_files = [
|
18
19
|
"LICENSE",
|
19
|
-
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
".bundle/config",
|
23
|
-
".document",
|
24
|
-
".gitignore",
|
25
|
-
".rspec",
|
26
|
-
"Gemfile",
|
27
|
-
"Gemfile.lock",
|
28
|
-
"LICENSE",
|
29
|
-
"README.rdoc",
|
30
|
-
"Rakefile",
|
31
|
-
"VERSION",
|
32
|
-
"bin/gitswitch",
|
33
|
-
"gitswitch.gemspec",
|
34
|
-
"lib/gitswitch.rb",
|
35
|
-
"spec/gitswitch_spec.rb",
|
36
|
-
"spec/spec_helper.rb"
|
20
|
+
"README.rdoc"
|
37
21
|
]
|
38
|
-
|
39
|
-
s.
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
40
26
|
s.require_paths = ["lib"]
|
27
|
+
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
29
|
s.rubygems_version = %q{1.3.7}
|
42
|
-
s.summary = %q{Easy git user switching}
|
43
|
-
s.test_files = [
|
44
|
-
"spec/gitswitch_spec.rb",
|
45
|
-
"spec/spec_helper.rb"
|
46
|
-
]
|
47
30
|
|
48
|
-
if s.respond_to? :specification_version then
|
49
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
-
s.specification_version = 3
|
51
31
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
32
|
+
s.add_dependency('rake')
|
33
|
+
s.add_dependency('thor')
|
34
|
+
s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
|
35
|
+
|
57
36
|
end
|
58
37
|
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
|
4
|
+
class Gitswitch
|
5
|
+
class CLI < Thor
|
6
|
+
|
7
|
+
|
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
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
######################################################################
|
27
|
+
desc "version", "Show the gitswitch version"
|
28
|
+
map ["-v","--version"] => :version
|
29
|
+
def version
|
30
|
+
puts Gitswitch::VERSION
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
######################################################################
|
35
|
+
desc "info", "Show the current git user"
|
36
|
+
map "-i" => :info
|
37
|
+
method_option :global, :type => :boolean
|
38
|
+
def info
|
39
|
+
puts Gitswitch.current_user_info(options)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
######################################################################
|
44
|
+
desc "list", "Show all the git user tags you have configured"
|
45
|
+
map ["-l","--list"] => :list
|
46
|
+
def list
|
47
|
+
puts Gitswitch.new.list_users
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
######################################################################
|
52
|
+
desc "switch [TAG]", "Switch git user"
|
53
|
+
map "-r" => :switch
|
54
|
+
method_option :global, :type => :boolean, :aliases => ["-s","--global"] ## To support the deprecated behavior
|
55
|
+
method_option :repository, :type => :boolean, :aliases => "-r"
|
56
|
+
def switch(tag = 'default')
|
57
|
+
options[:global] ? switch_global(tag) : Gitswitch.new.switch_repo_user(tag)
|
58
|
+
puts Gitswitch.current_user_info
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
######################################################################
|
63
|
+
desc "global [TAG]", "Switch global git user"
|
64
|
+
map "-s" => :switch_global
|
65
|
+
def switch_global(tag = 'default')
|
66
|
+
Gitswitch.new.switch_global_user(tag)
|
67
|
+
puts Gitswitch.current_user_info
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
######################################################################
|
72
|
+
desc "add [TAG]", "Add a new tagged user entry"
|
73
|
+
map ["-a","--add"] => :add
|
74
|
+
def add(tag = '')
|
75
|
+
gs = Gitswitch.new
|
76
|
+
|
77
|
+
tag.gsub!(/\W+/,'')
|
78
|
+
tag = ask("Enter a tag to describe this git user entry: ").gsub(/\W+/,'') if (tag.nil? || tag.empty?)
|
79
|
+
|
80
|
+
if tag.empty?
|
81
|
+
puts "You must enter a short tag to describe the git user entry you would like to save."
|
82
|
+
exit
|
83
|
+
end
|
84
|
+
|
85
|
+
puts "Adding a new gitswitch user entry for tag '#{tag}'"
|
86
|
+
(email, name) = prompt_for_email_and_name
|
87
|
+
gs.set_gitswitch_entry(tag, email, name)
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
######################################################################
|
92
|
+
desc "update [TAG]", "Update a tagged user entry"
|
93
|
+
map ["-o","--overwrite"] => :add
|
94
|
+
def update(tag = '')
|
95
|
+
gs = Gitswitch.new
|
96
|
+
tag_table = gs.get_tag_display
|
97
|
+
|
98
|
+
tag.gsub!(/\W+/,'')
|
99
|
+
if (tag.nil? || tag.empty?)
|
100
|
+
tag = ask("Which tag would you like to update: \n#{tag_table}").gsub(/\W+/,'')
|
101
|
+
end
|
102
|
+
|
103
|
+
puts "Updating #{tag} entry..."
|
104
|
+
(email, name) = prompt_for_email_and_name
|
105
|
+
Gitswitch.new.set_gitswitch_entry(tag, email, name)
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
######################################################################
|
110
|
+
desc "delete [TAG]", "Delete a tagged user entry"
|
111
|
+
def delete(tag = '')
|
112
|
+
gs = Gitswitch.new
|
113
|
+
|
114
|
+
tag_table = gs.get_tag_display
|
115
|
+
|
116
|
+
tag.gsub!(/\W+/,'')
|
117
|
+
if (tag.nil? || tag.empty?)
|
118
|
+
tag = ask("Which tag would you like to delete: \n#{tag_table}").gsub(/\W+/,'')
|
119
|
+
end
|
120
|
+
|
121
|
+
Gitswitch.new.delete_gitswitch_entry(tag)
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
|
128
|
+
######################################################################
|
129
|
+
def prompt_for_email_and_name
|
130
|
+
email = ask(" E-mail address: ").chomp
|
131
|
+
## TODO: Validate e-mail
|
132
|
+
if email.nil?
|
133
|
+
puts "No name provided"
|
134
|
+
exit
|
135
|
+
end
|
136
|
+
|
137
|
+
name = ask(" Name: (ENTER to use \"" + Gitswitch::get_git_user_info({:global => true})[:name] + "\") ").chomp
|
138
|
+
name = Gitswitch::get_git_user_info({:global => true})[:name] if name.empty?
|
139
|
+
|
140
|
+
if name.nil?
|
141
|
+
puts "No name provided"
|
142
|
+
exit
|
143
|
+
end
|
144
|
+
|
145
|
+
return [email, name]
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,171 @@
|
|
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
|
+
|
11
|
+
|
12
|
+
##############################################################
|
13
|
+
def initialize
|
14
|
+
@users = {}
|
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
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
##############################################################
|
26
|
+
# Create a .gitswitch file with the current user defaults
|
27
|
+
def create_fresh_gitswitch_file
|
28
|
+
@users = {}
|
29
|
+
user = Gitswitch::get_git_user_info({:global => true})
|
30
|
+
if user[:name].empty? && user[:email].empty?
|
31
|
+
puts "ERROR: You must set up a default git user.name and user.email first."
|
32
|
+
else
|
33
|
+
puts "Adding your global .gitconfig user info to the \"default\" tag..."
|
34
|
+
set_gitswitch_entry('default', user[:email], user[:name])
|
35
|
+
save_gitswitch_file
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.gitswitch_file_exists
|
40
|
+
File.exists? GITSWITCH_CONFIG_FILE
|
41
|
+
end
|
42
|
+
|
43
|
+
def save_gitswitch_file
|
44
|
+
if fh = File.open(GITSWITCH_CONFIG_FILE, 'w')
|
45
|
+
fh.write(@users.to_yaml)
|
46
|
+
fh.close
|
47
|
+
else
|
48
|
+
puts "ERROR: Could not open/write the gitswitch config file: #{GITSWITCH_CONFIG_FILE}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
##############################################################
|
54
|
+
# Set git user parameters for a tag
|
55
|
+
# ==== Parameters
|
56
|
+
# * +tag+ - Required. The tag you want to add to your .gitswitch file
|
57
|
+
# * +email+ - Required
|
58
|
+
# * +name+ - Required
|
59
|
+
def set_gitswitch_entry(tag, email, name)
|
60
|
+
@users[tag] = {:name => name, :email => email}
|
61
|
+
save_gitswitch_file
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete_gitswitch_entry(tag)
|
65
|
+
if tag == 'default'
|
66
|
+
puts "Cannot delete the default tag. Use the update command instead"
|
67
|
+
exit
|
68
|
+
end
|
69
|
+
@users.delete(tag)
|
70
|
+
save_gitswitch_file
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_user(tag)
|
74
|
+
if !@users.empty? && @users[tag] && !@users[tag].empty?
|
75
|
+
@users[tag]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_tags
|
80
|
+
@users.keys
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_tag_display
|
84
|
+
max_length = @users.keys.sort{|x,y| y.length <=> x.length }.first.length
|
85
|
+
@users.each_pair.map {|key,value| sprintf(" %#{max_length}s %s\n", key, value[:email]) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def list_users
|
89
|
+
response = ''
|
90
|
+
response << "\nCurrent git user options --\n"
|
91
|
+
@users.each do |key, user|
|
92
|
+
response << "#{key}:\n"
|
93
|
+
response << " Name: #{user[:name]}\n" if !user[:name].to_s.empty?
|
94
|
+
response << " E-mail: #{user[:email]}\n\n"
|
95
|
+
end
|
96
|
+
response
|
97
|
+
end
|
98
|
+
|
99
|
+
## Return active user information.
|
100
|
+
## If you're in a git repo, show that user info. Otherwise, display the global info.
|
101
|
+
def self.current_user_info(options = {})
|
102
|
+
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"
|
103
|
+
current_git_user = Gitswitch::get_git_user_info(options)
|
104
|
+
response << "Name: #{current_git_user[:name]}\n"
|
105
|
+
response << "E-mail: #{current_git_user[:email]}\n"
|
106
|
+
response
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.in_a_git_repo
|
110
|
+
%x(#{GIT_BIN} status 2>&1 | head -n 1).to_s =~ /^fatal/i ? false : true
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
##############################################################
|
115
|
+
def git_config(user, options = {})
|
116
|
+
git_args = 'config --replace-all'
|
117
|
+
git_args += ' --global' if options[:global]
|
118
|
+
|
119
|
+
%x(#{GIT_BIN} #{git_args} user.email #{user[:email].to_s.shellescape})
|
120
|
+
%x(#{GIT_BIN} #{git_args} user.name #{user[:name].to_s.shellescape}) if !user[:name].to_s.empty?
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
##############################################################
|
125
|
+
# Switch git user in your global .gitconfig file
|
126
|
+
# ==== Parameters
|
127
|
+
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
128
|
+
def switch_global_user tag = "default"
|
129
|
+
if user = get_user(tag)
|
130
|
+
puts "Switching your .gitconfig user info to \"#{tag}\" tag (#{user[:name]} <#{user[:email]}>)."
|
131
|
+
git_config(user, {:global => true})
|
132
|
+
else
|
133
|
+
puts "ERROR: Could not find info for tag \"#{tag}\" in your .gitswitch file"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
# Set the git user information for current repository
|
139
|
+
# ==== Parameters
|
140
|
+
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
141
|
+
def switch_repo_user(tag = "default")
|
142
|
+
## TODO: See if we're actually in a git repo
|
143
|
+
if !Gitswitch::in_a_git_repo
|
144
|
+
puts "You do not appear to currently be in a git repository directory"
|
145
|
+
false
|
146
|
+
else
|
147
|
+
if user = get_user(tag)
|
148
|
+
puts "Switching git user to \"#{tag}\" tag for the current repository (#{user[:name]} <#{user[:email]}>)."
|
149
|
+
git_config(user) or raise "Could not change the git user settings to your repository."
|
150
|
+
else
|
151
|
+
puts "ERROR: Could not find info for tag \"#{tag}\" in your .gitswitch file"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
# Show the current git user info
|
161
|
+
def self.get_git_user_info(options = {})
|
162
|
+
git_args = 'config --get'
|
163
|
+
git_args += ' --global' if options[:global]
|
164
|
+
|
165
|
+
{
|
166
|
+
:name => %x(#{GIT_BIN} #{git_args} user.name).to_s.chomp,
|
167
|
+
:email => %x(#{GIT_BIN} #{git_args} user.email).to_s.chomp
|
168
|
+
}
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
data/lib/gitswitch.rb
CHANGED
@@ -1,250 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
require 'gitswitch/version'
|
2
|
+
require 'gitswitch/commands'
|
3
|
+
require 'gitswitch/cli'
|
4
|
+
#Gitswitch::CLI.start ## If we decide to use Thor
|
4
5
|
|
5
6
|
|
6
|
-
class GitSwitch
|
7
|
-
GITSWITCH_CONFIG_FILE = File.join ENV["HOME"], ".gitswitch"
|
8
|
-
GIT_BIN = '/usr/bin/env git'
|
9
|
-
VERSION_FILE = File.join File.dirname(__FILE__), "..", "VERSION"
|
10
|
-
|
11
|
-
|
12
|
-
def self.run args = ARGV
|
13
|
-
gitswitch = GitSwitch.new
|
14
|
-
gitswitch.parse_args args
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
def initialize
|
19
|
-
@users = {}
|
20
|
-
if File.exists? GITSWITCH_CONFIG_FILE
|
21
|
-
@users = YAML::load_file GITSWITCH_CONFIG_FILE
|
22
|
-
if @users.nil?
|
23
|
-
puts "Error loading .gitswitch file"
|
24
|
-
exit
|
25
|
-
end
|
26
|
-
else
|
27
|
-
print "Gitswitch users file ~/.gitswitch not found. Would you like to create one? (y/n): "
|
28
|
-
if gets.chomp =~ /^y/i
|
29
|
-
create_gitswitch_file
|
30
|
-
else
|
31
|
-
puts "Ok, that's fine. Exiting."
|
32
|
-
exit
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
# Read and parse the supplied command line arguments.
|
39
|
-
def parse_args args = ARGV
|
40
|
-
args = ["-h"] if args.empty?
|
41
|
-
|
42
|
-
parser = OptionParser.new do |o|
|
43
|
-
o.banner = "Usage: gitswitch [options]"
|
44
|
-
|
45
|
-
o.on "-l", "--list", "Show all git users you have configured" do
|
46
|
-
list_users
|
47
|
-
exit
|
48
|
-
end
|
49
|
-
|
50
|
-
o.on "-i", "--info", "Show the current git user info." do
|
51
|
-
print_info
|
52
|
-
exit
|
53
|
-
end
|
54
|
-
|
55
|
-
o.on "-s", "--switch [TAG]", String, "Switch git user to the specified tag in your user's global git configuration" do |tag|
|
56
|
-
tag ||= 'default'
|
57
|
-
switch_global_user(tag)
|
58
|
-
print_info
|
59
|
-
exit
|
60
|
-
end
|
61
|
-
|
62
|
-
o.on "-r", "--repo [TAG]", String, "Switch git user to the specified tag for the current directory's git repository" do |tag|
|
63
|
-
tag ||= 'default'
|
64
|
-
switch_repo_user(tag)
|
65
|
-
exit
|
66
|
-
end
|
67
|
-
|
68
|
-
o.on "-h", "--help", "Show this help message." do
|
69
|
-
print_info
|
70
|
-
puts parser
|
71
|
-
exit
|
72
|
-
end
|
73
|
-
|
74
|
-
o.on "-o", "--overwrite", "Overwrite/create a .gitswitch file using your global git user info as default" do
|
75
|
-
create_gitswitch_file
|
76
|
-
print_info
|
77
|
-
exit
|
78
|
-
end
|
79
|
-
|
80
|
-
o.on "-a", "--add [TAG]", "Add a new gitswitch entry" do |tag|
|
81
|
-
add_gitswitch_entry(tag)
|
82
|
-
exit
|
83
|
-
end
|
84
|
-
|
85
|
-
o.on("-v", "--version", "Show the current version.") do
|
86
|
-
print_version
|
87
|
-
exit
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
begin
|
92
|
-
parser.parse! args
|
93
|
-
rescue OptionParser::InvalidOption => error
|
94
|
-
puts error.message.capitalize
|
95
|
-
rescue OptionParser::MissingArgument => error
|
96
|
-
puts error.message.capitalize
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
# Create a .gitswitch file with the current user defaults
|
103
|
-
def create_gitswitch_file
|
104
|
-
user = get_git_user_info({:global => true})
|
105
|
-
if user[:name].empty? && user[:email].empty?
|
106
|
-
puts "ERROR: You must set up a default git user.name and user.email first."
|
107
|
-
else
|
108
|
-
puts "Adding your global .gitconfig user info to the \"default\" tag..."
|
109
|
-
set_gitswitch_entry('default', user[:email], user[:name])
|
110
|
-
save_gitswitch_file
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
|
115
|
-
def save_gitswitch_file
|
116
|
-
if fh = File.open(GITSWITCH_CONFIG_FILE, 'w')
|
117
|
-
fh.write(@users.to_yaml)
|
118
|
-
fh.close
|
119
|
-
else
|
120
|
-
puts "ERROR: Could not open/write the gitswitch config file: #{GITSWITCH_CONFIG_FILE}"
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
|
125
|
-
# Set git user parameters for a tag
|
126
|
-
# ==== Parameters
|
127
|
-
# * +tag+ - Required. The tag you want to add to your .gitswitch file
|
128
|
-
# * +email+ - Required
|
129
|
-
# * +name+ - Required
|
130
|
-
def set_gitswitch_entry(tag, email, name)
|
131
|
-
@users[tag] = {:name => name, :email => email}
|
132
|
-
save_gitswitch_file
|
133
|
-
end
|
134
|
-
|
135
|
-
|
136
|
-
def get_user(tag)
|
137
|
-
if !@users.empty? && @users[tag] && !@users[tag].empty?
|
138
|
-
@users[tag]
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
|
143
|
-
def git_config(user, args = {})
|
144
|
-
git_args = 'config --replace-all'
|
145
|
-
git_args += ' --global' if args[:global]
|
146
|
-
|
147
|
-
%x(#{GIT_BIN} #{git_args} user.email #{user[:email].to_s.shellescape})
|
148
|
-
%x(#{GIT_BIN} #{git_args} user.name #{user[:name].to_s.shellescape}) if !user[:name].to_s.empty?
|
149
|
-
end
|
150
|
-
|
151
|
-
|
152
|
-
# Switch git user in your global .gitconfig file
|
153
|
-
# ==== Parameters
|
154
|
-
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
155
|
-
def switch_global_user tag = "default"
|
156
|
-
if user = get_user(tag)
|
157
|
-
puts "Switching your .gitconfig user info to \"#{tag}\" tag (#{user[:name]} <#{user[:email]}>)."
|
158
|
-
git_config(user, {:global => true})
|
159
|
-
else
|
160
|
-
puts "ERROR: Could not find info for tag \"#{tag}\" in your .gitswitch file"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
|
165
|
-
# Set the git user information for current repository
|
166
|
-
# ==== Parameters
|
167
|
-
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
168
|
-
def switch_repo_user(tag = "default")
|
169
|
-
## TODO: See if we're actually in a git repo
|
170
|
-
if user = get_user(tag)
|
171
|
-
puts "Switching git user to \"#{tag}\" tag for the current repository (#{user[:name]} <#{user[:email]}>)."
|
172
|
-
git_config(user) or raise "Could not change the git user settings to your repository."
|
173
|
-
else
|
174
|
-
puts "ERROR: Could not find info for tag \"#{tag}\" in your .gitswitch file"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
|
179
|
-
# Add a user entry to your .gitswitch file
|
180
|
-
def add_gitswitch_entry(tag = '')
|
181
|
-
if (!tag.nil? && !tag.empty?)
|
182
|
-
tag.gsub!(/\W+/,'')
|
183
|
-
else
|
184
|
-
print "Enter a tag to describe this git user entry: "
|
185
|
-
tag = gets.gsub(/\W+/,'')
|
186
|
-
end
|
187
|
-
|
188
|
-
if tag.empty?
|
189
|
-
puts "You must enter a short tag to describe the git user entry you would like to save."
|
190
|
-
exit
|
191
|
-
end
|
192
|
-
|
193
|
-
puts "Adding a new gitswitch user entry for tag '#{tag}'"
|
194
|
-
print " E-mail address: "
|
195
|
-
email = gets.chomp
|
196
|
-
|
197
|
-
print " Name: (ENTER to use \"" + get_git_user_info({:global => true})[:name] + "\") "
|
198
|
-
name = gets.chomp
|
199
|
-
name = get_git_user_info({:global => true})[:name] if name.empty?
|
200
|
-
|
201
|
-
set_gitswitch_entry(tag, email, name)
|
202
|
-
end
|
203
|
-
|
204
|
-
|
205
|
-
def list_users
|
206
|
-
puts "\nCurrent git user options --"
|
207
|
-
@users.each do |key, user|
|
208
|
-
puts "#{key}:"
|
209
|
-
puts " Name: #{user[:name]}" if !user[:name].to_s.empty?
|
210
|
-
puts " E-mail: #{user[:email]}\n"
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
|
215
|
-
# Print active account information.
|
216
|
-
def print_info
|
217
|
-
current_git_user = get_git_user_info
|
218
|
-
puts "Current git user information:\n"
|
219
|
-
puts "Name: #{current_git_user[:name]}"
|
220
|
-
puts "E-mail: #{current_git_user[:email]}"
|
221
|
-
puts
|
222
|
-
end
|
223
|
-
|
224
|
-
|
225
|
-
# Print version information.
|
226
|
-
def print_version
|
227
|
-
if fh = File.open(VERSION_FILE,'r')
|
228
|
-
puts "GitSwitch " + fh.gets
|
229
|
-
fh.close
|
230
|
-
else
|
231
|
-
puts "Version information not found"
|
232
|
-
end
|
233
|
-
# puts "GitSwitch " + GitSwitch::VERSION.to_s
|
234
|
-
end
|
235
|
-
|
236
|
-
|
237
|
-
private
|
238
|
-
|
239
|
-
# Show the current git user info
|
240
|
-
def get_git_user_info(args = {})
|
241
|
-
git_args = 'config --get'
|
242
|
-
git_args += ' --global' if args[:global]
|
243
|
-
|
244
|
-
{
|
245
|
-
:name => %x(#{GIT_BIN} #{git_args} user.name).to_s.chomp,
|
246
|
-
:email => %x(#{GIT_BIN} #{git_args} user.email).to_s.chomp
|
247
|
-
}
|
248
|
-
end
|
249
|
-
|
250
|
-
end
|
data/spec/gitswitch_spec.rb
CHANGED
@@ -1,16 +1,68 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Gitswitch do
|
4
4
|
|
5
5
|
describe "basics" do
|
6
6
|
it "should have a VERSION" do
|
7
|
-
VERSION.should_not == ''
|
7
|
+
Gitswitch::VERSION.should_not == ''
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should find the git executable" do
|
11
|
-
result = %x[#{
|
11
|
+
result = %x[#{Gitswitch::GIT_BIN} --version]
|
12
12
|
$?.exitstatus.should == 0
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
|
17
|
+
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
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
describe "write methods" do
|
28
|
+
|
29
|
+
it "should allow you to add a new user entry" do
|
30
|
+
pending
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should allow you to update a user entry" do
|
34
|
+
pending
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should allow you to delete a user entry" do
|
38
|
+
pending
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should allow you to overwrite the current .gitswitch file and start fresh" do
|
42
|
+
pending
|
43
|
+
end
|
44
|
+
|
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
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should allow you to change a specific repository's user credentials" do
|
55
|
+
pending
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
describe "weird outlier cases" do
|
61
|
+
|
62
|
+
it "in a git repo directory with no user info specified, show the global config header and user info" do
|
63
|
+
pending
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gitswitch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joe Alba
|
@@ -15,10 +15,53 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-27 00:00:00 -04:00
|
19
19
|
default_executable: gitswitch
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rake
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: thor
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
version: 2.3.0
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
22
65
|
description: Easily switch your git name/e-mail user info -- Handy for work vs. personal and for pair programming
|
23
66
|
email: joe@joealba.com
|
24
67
|
executables:
|
@@ -34,14 +77,15 @@ files:
|
|
34
77
|
- .gitignore
|
35
78
|
- .rspec
|
36
79
|
- Gemfile
|
37
|
-
- Gemfile.lock
|
38
80
|
- LICENSE
|
39
81
|
- README.rdoc
|
40
82
|
- Rakefile
|
41
|
-
- VERSION
|
42
83
|
- bin/gitswitch
|
43
84
|
- gitswitch.gemspec
|
44
85
|
- lib/gitswitch.rb
|
86
|
+
- lib/gitswitch/cli.rb
|
87
|
+
- lib/gitswitch/commands.rb
|
88
|
+
- lib/gitswitch/version.rb
|
45
89
|
- spec/gitswitch_spec.rb
|
46
90
|
- spec/spec_helper.rb
|
47
91
|
has_rdoc: true
|
data/Gemfile.lock
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
diff-lcs (1.1.2)
|
5
|
-
gemcutter (0.6.1)
|
6
|
-
git (1.2.5)
|
7
|
-
jeweler (1.4.0)
|
8
|
-
gemcutter (>= 0.1.0)
|
9
|
-
git (>= 1.2.5)
|
10
|
-
rubyforge (>= 2.0.0)
|
11
|
-
json_pure (1.4.6)
|
12
|
-
rspec (2.0.0.beta.22)
|
13
|
-
rspec-core (= 2.0.0.beta.22)
|
14
|
-
rspec-expectations (= 2.0.0.beta.22)
|
15
|
-
rspec-mocks (= 2.0.0.beta.22)
|
16
|
-
rspec-core (2.0.0.beta.22)
|
17
|
-
rspec-expectations (2.0.0.beta.22)
|
18
|
-
diff-lcs (>= 1.1.2)
|
19
|
-
rspec-mocks (2.0.0.beta.22)
|
20
|
-
rspec-core (= 2.0.0.beta.22)
|
21
|
-
rspec-expectations (= 2.0.0.beta.22)
|
22
|
-
rubyforge (2.0.4)
|
23
|
-
json_pure (>= 1.1.7)
|
24
|
-
|
25
|
-
PLATFORMS
|
26
|
-
ruby
|
27
|
-
|
28
|
-
DEPENDENCIES
|
29
|
-
bundler (~> 1.0.0)
|
30
|
-
jeweler (~> 1.4.0)
|
31
|
-
rspec (~> 2.0.0.beta.22)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.2.5
|