gitswitch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +34 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/bin/gitswitch +4 -0
- data/lib/gitswitch.rb +212 -0
- data/test/helper.rb +10 -0
- data/test/test_gitswitch.rb +9 -0
- metadata +91 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Joe Alba
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
= gitswitch
|
2
|
+
|
3
|
+
Easily switch your current git user info. Handy for work and personal repositories. Also might come in handy for switching between users while pair programming.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install gitswitch
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
Simply run the script to establish a default ~/.gitswitch file using the git info you already have in your ~/.gitconfig file
|
12
|
+
gitswitch
|
13
|
+
|
14
|
+
Add a new git user tag
|
15
|
+
gitswitch -a
|
16
|
+
|
17
|
+
Set specific user info to your current git repository
|
18
|
+
gitswitch -r work
|
19
|
+
gitswitch -r # Sets your 'default' tag if you don't specify a tag
|
20
|
+
|
21
|
+
List all the gitswitch user tags/info you have stored
|
22
|
+
gitswitch -l
|
23
|
+
|
24
|
+
Current git user options --
|
25
|
+
default:
|
26
|
+
Name: Joe Alba
|
27
|
+
E-mail: joe@home.com
|
28
|
+
work:
|
29
|
+
Name: Joe Alba
|
30
|
+
E-mail: jalba@work.com
|
31
|
+
|
32
|
+
== Copyright
|
33
|
+
|
34
|
+
Copyright (c) 2010 Joe Alba. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "gitswitch"
|
8
|
+
gem.summary = %Q{Easy git user switching}
|
9
|
+
gem.description = %Q{Do you write code for your employer and for yourself? Want to easily change the e-mail address associated with your commits for work and home repos? This gem might help you out.}
|
10
|
+
gem.email = "joe@joealba.com"
|
11
|
+
gem.homepage = "http://github.com/joealba/gitswitch"
|
12
|
+
gem.authors = ["Joe Alba"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "gitswitch #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/gitswitch
ADDED
data/lib/gitswitch.rb
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
class GitSwitch
|
5
|
+
VERSION_FILE = File.join File.dirname(__FILE__), "..", "VERSION.yml"
|
6
|
+
GITSWITCH_CONFIG_FILE = File.join ENV["HOME"], ".gitswitch"
|
7
|
+
GIT_BIN = '/usr/bin/env git'
|
8
|
+
|
9
|
+
|
10
|
+
def self.run args = ARGV
|
11
|
+
gitswitch = GitSwitch.new
|
12
|
+
gitswitch.parse_args args
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@users = {}
|
18
|
+
if File.exists? GITSWITCH_CONFIG_FILE
|
19
|
+
@users = YAML::load_file GITSWITCH_CONFIG_FILE
|
20
|
+
raise "Error loading .gitswitch file" if @users.nil?
|
21
|
+
else
|
22
|
+
print "Gitswitch users file ~/.gitswitch not found. Would you like to create one? (y/n): "
|
23
|
+
if gets.chomp =~ /^y/i
|
24
|
+
create_gitswitch_file
|
25
|
+
else
|
26
|
+
puts "Ok, that's fine. Exiting." and exit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# Read and parse the supplied command line arguments.
|
34
|
+
def parse_args args = ARGV
|
35
|
+
args = ["-h"] if args.empty?
|
36
|
+
|
37
|
+
parser = OptionParser.new do |o|
|
38
|
+
o.banner = "Usage: gitswitch [options]"
|
39
|
+
|
40
|
+
o.on "-l", "--list", "Show all git users you have configured" do
|
41
|
+
list_users and exit
|
42
|
+
end
|
43
|
+
|
44
|
+
o.on "-i", "--info", "Show the current git user info." do
|
45
|
+
print_info and exit
|
46
|
+
end
|
47
|
+
|
48
|
+
o.on "-s", "--switch [TAG]", String, "Switch git user to the specified tag" do |tag|
|
49
|
+
tag ||= 'default'
|
50
|
+
switch_user tag
|
51
|
+
print_info
|
52
|
+
exit
|
53
|
+
end
|
54
|
+
|
55
|
+
o.on "-r", "--repo [TAG]", String, "Switch git user to the specified tag for the current directory's git repository" do |tag|
|
56
|
+
tag ||= 'default'
|
57
|
+
switch_repo_user tag
|
58
|
+
exit
|
59
|
+
end
|
60
|
+
|
61
|
+
o.on "-h", "--help", "Show this help message." do
|
62
|
+
print_info
|
63
|
+
puts parser and exit
|
64
|
+
end
|
65
|
+
|
66
|
+
o.on "-o", "--overwrite", "Overwrite/create a .gitswitch file using your current git user info as default" do
|
67
|
+
create_gitswitch_file
|
68
|
+
print_info and exit
|
69
|
+
end
|
70
|
+
|
71
|
+
o.on "-a", "--add", "Add a new gitswitch entry" do
|
72
|
+
add_gitswitch_entry and exit
|
73
|
+
end
|
74
|
+
|
75
|
+
o.on("-v", "--version", "Show the current version.") do
|
76
|
+
print_version and exit
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
begin
|
82
|
+
parser.parse! args
|
83
|
+
rescue OptionParser::InvalidOption => error
|
84
|
+
puts error.message.capitalize
|
85
|
+
rescue OptionParser::MissingArgument => error
|
86
|
+
puts error.message.capitalize
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
# Create a .gitswitch file with the current user defaults
|
93
|
+
def create_gitswitch_file
|
94
|
+
current_git_user = get_current_git_user
|
95
|
+
if current_git_user[:name].empty? && current_git_user[:email].empty?
|
96
|
+
puts "ERROR: You must set up a default git user.name and user.email first."
|
97
|
+
else
|
98
|
+
puts "Adding your current git user info to the \"default\" tag..."
|
99
|
+
set_gitswitch_entry('default', current_git_user[:email], current_git_user[:name])
|
100
|
+
save_gitswitch_file
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
def save_gitswitch_file
|
106
|
+
if fh = File.open(GITSWITCH_CONFIG_FILE, 'w')
|
107
|
+
fh.write(@users.to_yaml)
|
108
|
+
fh.close
|
109
|
+
else
|
110
|
+
puts "ERROR: Could not open/write the gitswitch config file: #{GITSWITCH_CONFIG_FILE}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
# Set git user parameters for a tag
|
116
|
+
# ==== Parameters
|
117
|
+
# * +tag+ - Required. The tag you want to add to your .gitswitch file
|
118
|
+
# * +email+ - Required
|
119
|
+
# * +name+ - Required
|
120
|
+
def set_gitswitch_entry(tag, email, name)
|
121
|
+
@users[tag] = {:name => name, :email => email}
|
122
|
+
save_gitswitch_file
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
# Switch git user in your global .gitconfig file
|
127
|
+
# ==== Parameters
|
128
|
+
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
129
|
+
def switch_global_user tag = "default"
|
130
|
+
puts "Switching git user to \"#{tag}\" tag..."
|
131
|
+
if !@users[tag].empty? && !@users[tag][:email].to_s.empty?
|
132
|
+
%x(#{GIT_BIN} config --replace-all --global user.name '#{@users[tag][:name]}') if !@users[tag][:name].to_s.empty?
|
133
|
+
%x(#{GIT_BIN} config --replace-all --global user.email '#{@users[tag][:email]}')
|
134
|
+
else
|
135
|
+
puts "ERROR: Could not find info for tag #{tag} in your .gitswitch file"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
# Set the git user information for current repository
|
141
|
+
# ==== Parameters
|
142
|
+
# * +tag+ - The tag associated with your desired git info in .gitswitch. Defaults to "default".
|
143
|
+
def switch_repo_user tag = "default"
|
144
|
+
puts "Switching git user to \"#{tag}\" tag..."
|
145
|
+
if !@users[tag].empty? && !@users[tag][:email].to_s.empty?
|
146
|
+
%x(#{GIT_BIN} config --replace-all user.name '#{@users[tag][:name]}') if !@users[tag][:name].to_s.empty?
|
147
|
+
%x(#{GIT_BIN} config --replace-all user.email '#{@users[tag][:email]}')
|
148
|
+
else
|
149
|
+
puts "ERROR: Could not find info for tag #{tag} in your .gitswitch file"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
# Add a user entry to your .gitswitch file
|
155
|
+
def add_gitswitch_entry
|
156
|
+
print "What tag would you like to set to this git user? "
|
157
|
+
tag = gets.chomp
|
158
|
+
|
159
|
+
print "E-mail address: "
|
160
|
+
email = gets.chomp
|
161
|
+
|
162
|
+
print "Name: (ENTER to use \"" + get_current_git_user()[:name] + "\") "
|
163
|
+
name = gets.chomp
|
164
|
+
if name.empty?
|
165
|
+
name = get_current_git_user()[:name]
|
166
|
+
end
|
167
|
+
set_gitswitch_entry(tag, email, name)
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
def list_users
|
172
|
+
puts "\nCurrent git user options --"
|
173
|
+
@users.each do |key, user|
|
174
|
+
puts "#{key}:"
|
175
|
+
puts " Name: #{user[:name]}" if !user[:name].to_s.empty?
|
176
|
+
puts " E-mail: #{user[:email]}\n"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
# Print active account information.
|
182
|
+
def print_info
|
183
|
+
current_git_user = get_current_git_user
|
184
|
+
puts "Current git user information:\n"
|
185
|
+
puts "Name: #{current_git_user[:name]}"
|
186
|
+
puts "E-mail: #{current_git_user[:email]}"
|
187
|
+
puts
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
# Print version information.
|
192
|
+
def print_version
|
193
|
+
if fh = File.open('VERSION','r')
|
194
|
+
puts "GitSwitch " + fh.gets
|
195
|
+
fh.close
|
196
|
+
else
|
197
|
+
puts "Version information not found"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
private
|
203
|
+
|
204
|
+
# Show the current git user info
|
205
|
+
def get_current_git_user
|
206
|
+
user = {
|
207
|
+
:name => %x(#{GIT_BIN} config --get user.name).to_s.chomp,
|
208
|
+
:email => %x(#{GIT_BIN} config --get user.email).to_s.chomp
|
209
|
+
}
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gitswitch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Joe Alba
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-04 00:00:00 -04:00
|
19
|
+
default_executable: gitswitch
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: thoughtbot-shoulda
|
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: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Do you write code for your employer and for yourself? Want to easily change the e-mail address associated with your commits for work and home repos? This gem might help you out.
|
36
|
+
email: joe@joealba.com
|
37
|
+
executables:
|
38
|
+
- gitswitch
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/gitswitch.rb
|
52
|
+
- test/helper.rb
|
53
|
+
- test/test_gitswitch.rb
|
54
|
+
- bin/gitswitch
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/joealba/gitswitch
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Easy git user switching
|
89
|
+
test_files:
|
90
|
+
- test/helper.rb
|
91
|
+
- test/test_gitswitch.rb
|