gitsu 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +676 -0
- data/README.md +38 -0
- data/Rakefile +3 -0
- data/TODO +10 -0
- data/bin/git-su +13 -0
- data/bin/git-whoami +13 -0
- data/features/add_user.feature +26 -0
- data/features/change_user_in_different_scopes.feature +24 -0
- data/features/clear_user.feature +28 -0
- data/features/configure_default_scope.feature +9 -0
- data/features/edit_config.feature +8 -0
- data/features/list_users.feature +16 -0
- data/features/print_current_user.feature +60 -0
- data/features/print_options.feature +8 -0
- data/features/step_definitions/gitsu_steps.rb +193 -0
- data/features/support/env.rb +2 -0
- data/features/switch_to_fully_qualified_user.feature +10 -0
- data/features/switch_to_stored_user.feature +49 -0
- data/gitsu.gemspec +19 -0
- data/lib/gitsu.rb +9 -0
- data/lib/gitsu/factory.rb +23 -0
- data/lib/gitsu/git.rb +136 -0
- data/lib/gitsu/gitsu.rb +94 -0
- data/lib/gitsu/runner.rb +18 -0
- data/lib/gitsu/shell.rb +16 -0
- data/lib/gitsu/switcher.rb +94 -0
- data/lib/gitsu/user.rb +55 -0
- data/lib/gitsu/user_file.rb +32 -0
- data/lib/gitsu/user_list.rb +47 -0
- data/lib/gitsu/version.rb +3 -0
- data/man/git-su.1.ronn +120 -0
- data/spec/gitsu/git_spec.rb +189 -0
- data/spec/gitsu/gitsu_spec.rb +177 -0
- data/spec/gitsu/runner_spec.rb +27 -0
- data/spec/gitsu/switcher_spec.rb +196 -0
- data/spec/gitsu/user_list_spec.rb +82 -0
- data/spec/gitsu/user_spec.rb +33 -0
- data/spec/spec_helper.rb +1 -0
- metadata +106 -0
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# GitSu
|
2
|
+
|
3
|
+
GitSu helps to manage your Git users, by making it easy to switch
|
4
|
+
between users.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
$ gem install gitsu
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
GitSu is intended to be used from the command line, through Git.
|
13
|
+
|
14
|
+
To switch the configured Git user:
|
15
|
+
|
16
|
+
$ git su "John Galt <jgalt@tcmc.com>"
|
17
|
+
|
18
|
+
Switched local user to John Galt <jgalt@tcmc.com>
|
19
|
+
|
20
|
+
To make it easier to switch users, tell GitSu about some users.
|
21
|
+
|
22
|
+
$ git su --add "John Galt <jgalt@tcmc.com>"
|
23
|
+
$ git su --add "Raphe Rackstraw <rack@hp.royalnavy.mod.uk>"
|
24
|
+
$ git su jg
|
25
|
+
|
26
|
+
Switched to user John Galt <jgalt@tcmc.com>
|
27
|
+
|
28
|
+
$ git su raphe
|
29
|
+
|
30
|
+
Switched to user Raphe Rackstraw <rack@hp.royalnavy.mod.uk>
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/TODO
ADDED
data/bin/git-su
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib_path = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include? lib_path
|
5
|
+
require 'gitsu'
|
6
|
+
|
7
|
+
factory = GitSu::Factory.new(STDOUT)
|
8
|
+
git_su = factory.git_su
|
9
|
+
runner = factory.runner
|
10
|
+
|
11
|
+
runner.run do
|
12
|
+
git_su.go ARGV
|
13
|
+
end
|
data/bin/git-whoami
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib_path = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include? lib_path
|
5
|
+
require 'gitsu'
|
6
|
+
|
7
|
+
factory = GitSu::Factory.new(STDOUT)
|
8
|
+
switcher = factory.switcher
|
9
|
+
runner = factory.runner
|
10
|
+
|
11
|
+
runner.run do
|
12
|
+
switcher.print_current :derived
|
13
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: Add user
|
2
|
+
As a new user
|
3
|
+
I want to add myself to the GitSu config
|
4
|
+
So that I can quickly switch back to myself
|
5
|
+
|
6
|
+
Scenario: Add user
|
7
|
+
Given no user is selected
|
8
|
+
When I type "git su --add 'John Galt <jgalt@example.com>'"
|
9
|
+
Then I should see "User 'John Galt <jgalt@example.com>' added to users"
|
10
|
+
|
11
|
+
Scenario: Try to add existing user
|
12
|
+
Given user list is
|
13
|
+
"""
|
14
|
+
jgalt@example.com: John Galt
|
15
|
+
"""
|
16
|
+
When I type "git su --add 'John Galt <jgalt@example.com>'"
|
17
|
+
Then I should see "User 'John Galt <jgalt@example.com>' already in user list"
|
18
|
+
And user list should be
|
19
|
+
"""
|
20
|
+
jgalt@example.com: John Galt
|
21
|
+
"""
|
22
|
+
|
23
|
+
Scenario: Try to add invalid user
|
24
|
+
Given no user is selected
|
25
|
+
When I type "git su --add xxx"
|
26
|
+
Then I should see "Couldn't parse 'xxx' as user (expected user in format: 'John Smith <jsmith@example.com>')"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Feature: Change user in different scopes
|
2
|
+
As a user with multiple projects
|
3
|
+
I want to change my user in different scopes
|
4
|
+
So that I have greater control over repositories
|
5
|
+
|
6
|
+
Scenario Outline: change user in scope
|
7
|
+
Given no user is selected in any scope
|
8
|
+
When I type "git su 'John Galt <jgalt@example.com>' --<selected>"
|
9
|
+
Then user "John Galt <jgalt@example.com>" should be selected in "<selected>" scope
|
10
|
+
And no user should be selected in "<not_selected>" scope
|
11
|
+
And no user should be selected in "<also_not_selected>" scope
|
12
|
+
|
13
|
+
Scenarios:
|
14
|
+
| selected | not_selected | also_not_selected |
|
15
|
+
| local | global | system |
|
16
|
+
| global | local | system |
|
17
|
+
| system | local | global |
|
18
|
+
|
19
|
+
Scenario: change user in multiple scopes
|
20
|
+
Given no user is selected in any scope
|
21
|
+
When I type "git su 'John Galt <jgalt@example.com>' --local --global"
|
22
|
+
Then user "John Galt <jgalt@example.com>" should be selected in "local" scope
|
23
|
+
And user "John Galt <jgalt@example.com>" should be selected in "global" scope
|
24
|
+
And no user should be selected in "system" scope
|
@@ -0,0 +1,28 @@
|
|
1
|
+
Feature: Clear user
|
2
|
+
As a user
|
3
|
+
I want to clear my Git configured user
|
4
|
+
So that I can make sure others don't commit code as me
|
5
|
+
|
6
|
+
Scenario: Clear user in specified scope
|
7
|
+
Given user "John Galt <jgalt@example.com>" is selected in "local" scope
|
8
|
+
When I type "git su --clear --local"
|
9
|
+
Then I should see "Clearing Git user in local scope"
|
10
|
+
And no user should be selected in "local" scope
|
11
|
+
|
12
|
+
Scenario: Clear user in multiple specified scopes
|
13
|
+
Given user "John Galt <jgalt@example.com>" is selected in "local" scope
|
14
|
+
And user "John Galt <jgalt@example.com>" is selected in "global" scope
|
15
|
+
When I type "git su --clear --local --global"
|
16
|
+
Then I should see "Clearing Git users in local and global scopes"
|
17
|
+
And no user should be selected in "local" scope
|
18
|
+
And no user should be selected in "global" scope
|
19
|
+
|
20
|
+
Scenario: Clear all users
|
21
|
+
Given user "Joe Local <jlocal@example.com>" is selected in "local" scope
|
22
|
+
And user "Joe Global <jglobal@example.com>" is selected in "global" scope
|
23
|
+
And user "Joe System <jsystem@example.com>" is selected in "system" scope
|
24
|
+
When I type "git su --clear"
|
25
|
+
Then I should see "Clearing Git users in all scopes"
|
26
|
+
And no user should be selected in "local" scope
|
27
|
+
And no user should be selected in "global" scope
|
28
|
+
And no user should be selected in "system" scope
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Feature: Configure default scope
|
2
|
+
As a user of a shared environment
|
3
|
+
I want to configure the default scope
|
4
|
+
So that I can change the user for all projects at once
|
5
|
+
|
6
|
+
Scenario: Default to global scope
|
7
|
+
Given the Git configuration has "git-su.defaultSelectScope" set to "global"
|
8
|
+
When I type "git su 'John Galt <jgalt@example.com>'"
|
9
|
+
Then I should see "Switched global user to John Galt <jgalt@example.com>"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Feature: List users
|
2
|
+
As a user
|
3
|
+
I want to list all users
|
4
|
+
So that I can see if the user I want is in the config
|
5
|
+
|
6
|
+
Scenario: list users
|
7
|
+
Given user list contains user "John Galt" with email "jgalt@example.com"
|
8
|
+
And user list contains user "Raphe Rackstraw" with email "rrack@github.com"
|
9
|
+
When I type "git su --list"
|
10
|
+
Then I should see "John Galt <jgalt@example.com>"
|
11
|
+
And I should see "Raphe Rackstraw <rrack@github.com>"
|
12
|
+
|
13
|
+
Scenario: no users configured yet
|
14
|
+
Given user list is empty
|
15
|
+
When I type "git su --list"
|
16
|
+
Then I shouldn't see anything
|
@@ -0,0 +1,60 @@
|
|
1
|
+
Feature: Print the current user
|
2
|
+
As a user
|
3
|
+
I want to see who is currently logged in
|
4
|
+
So that I know if I need to switch users
|
5
|
+
|
6
|
+
Scenario Outline: Print current user in different scopes
|
7
|
+
Given user "Johnny System <jsystem@gitsu.com>" is selected in "system" scope
|
8
|
+
And user "Johnny Global <jglobal@gitsu.com>" is selected in "global" scope
|
9
|
+
And user "Johnny Local <jlocal@gitsu.com>" is selected in "local" scope
|
10
|
+
When I type "git su --<scope>"
|
11
|
+
Then I should see "<output>"
|
12
|
+
|
13
|
+
Scenarios: Selected Users
|
14
|
+
| scope | output |
|
15
|
+
| system | Johnny System <jsystem@gitsu.com> |
|
16
|
+
| global | Johnny Global <jglobal@gitsu.com> |
|
17
|
+
| local | Johnny Local <jlocal@gitsu.com> |
|
18
|
+
|
19
|
+
Scenario: Print all users
|
20
|
+
Given user "Johnny System <jsystem@gitsu.com>" is selected in "system" scope
|
21
|
+
And user "Johnny Global <jglobal@gitsu.com>" is selected in "global" scope
|
22
|
+
And user "Johnny Local <jlocal@gitsu.com>" is selected in "local" scope
|
23
|
+
When I type "git su"
|
24
|
+
Then I should see
|
25
|
+
"""
|
26
|
+
Current user: Johnny Local <jlocal@gitsu.com>
|
27
|
+
|
28
|
+
Local: Johnny Local <jlocal@gitsu.com>
|
29
|
+
Global: Johnny Global <jglobal@gitsu.com>
|
30
|
+
System: Johnny System <jsystem@gitsu.com>
|
31
|
+
"""
|
32
|
+
|
33
|
+
Scenario: No user is selected
|
34
|
+
Given no user is selected in any scope
|
35
|
+
When I type "git su"
|
36
|
+
Then I should see "Current user: (none)"
|
37
|
+
|
38
|
+
Scenario: No user is selected in the specified scope
|
39
|
+
Given no user is selected in any scope
|
40
|
+
When I type "git su --local"
|
41
|
+
Then I shouldn't see anything
|
42
|
+
|
43
|
+
Scenario: No user is selected in a specified scope
|
44
|
+
Given user "Johnny System <jsystem@gitsu.com>" is selected in "system" scope
|
45
|
+
When I type "git su --local --system"
|
46
|
+
Then I should see
|
47
|
+
"""
|
48
|
+
Johnny System <jsystem@gitsu.com>
|
49
|
+
"""
|
50
|
+
|
51
|
+
Scenario: Multiple scopes specified
|
52
|
+
Given user "Johnny System <jsystem@gitsu.com>" is selected in "system" scope
|
53
|
+
And user "Johnny Global <jglobal@gitsu.com>" is selected in "global" scope
|
54
|
+
And user "Johnny Local <jlocal@gitsu.com>" is selected in "local" scope
|
55
|
+
When I type "git su --local --system"
|
56
|
+
Then I should see
|
57
|
+
"""
|
58
|
+
Johnny Local <jlocal@gitsu.com>
|
59
|
+
Johnny System <jsystem@gitsu.com>
|
60
|
+
"""
|
@@ -0,0 +1,193 @@
|
|
1
|
+
Given /^no user is selected$/ do
|
2
|
+
step "no user is selected in any scope"
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^no user is selected in any scope$/ do
|
6
|
+
git.clear_users
|
7
|
+
end
|
8
|
+
|
9
|
+
Given /^user "(.*?)" is selected$/ do |user|
|
10
|
+
git.select_user(GitSu::User.parse(user), :global)
|
11
|
+
end
|
12
|
+
|
13
|
+
Given /^user "(.*?)" is selected in "(.*?)" scope$/ do |user, scope|
|
14
|
+
git.select_user(GitSu::User.parse(user), scope.to_sym)
|
15
|
+
end
|
16
|
+
|
17
|
+
Given /^the Git configuration has "(.*?)" set to "(.*?)"$/ do |key, value|
|
18
|
+
git.set_config(:global, key, value)
|
19
|
+
end
|
20
|
+
|
21
|
+
Given /^user list is empty$/ do
|
22
|
+
write_user_list("\n")
|
23
|
+
end
|
24
|
+
|
25
|
+
Given /^user list is$/ do |user_list|
|
26
|
+
write_user_list user_list
|
27
|
+
end
|
28
|
+
|
29
|
+
Then /^user list should be$/ do |user_list|
|
30
|
+
read_user_list.should eq user_list
|
31
|
+
end
|
32
|
+
|
33
|
+
And /^user list contains user "(.*?)" with email "(.*?)"$/ do |name, email|
|
34
|
+
user_list.add GitSu::User.new(name, email)
|
35
|
+
end
|
36
|
+
|
37
|
+
def split_args_with_shell(arg_line)
|
38
|
+
`for arg in #{arg_line}; do echo $arg; done`.strip.split("\n")
|
39
|
+
end
|
40
|
+
|
41
|
+
When /^I type "(.*?)"$/ do |command_line|
|
42
|
+
arg_line = command_line.gsub(/^git su/, "")
|
43
|
+
args = split_args_with_shell(arg_line)
|
44
|
+
gitsu.go args
|
45
|
+
end
|
46
|
+
|
47
|
+
Then /^I should see$/ do |expected_output|
|
48
|
+
output.messages.join("\n").should eq expected_output
|
49
|
+
end
|
50
|
+
|
51
|
+
Then /^I should see "(.*?)"$/ do |expected_output|
|
52
|
+
matching_messages = output.messages.select {|e| e.include? expected_output}
|
53
|
+
if matching_messages.empty?
|
54
|
+
fail "Expected [#{output.messages}] to contain '#{expected_output}'"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Then /^I shouldn't see anything$/ do
|
59
|
+
output.messages.should be_empty
|
60
|
+
end
|
61
|
+
|
62
|
+
Then /^user "(.*?)" should be selected$/ do |user|
|
63
|
+
step %[user "#{user}" should be selected in "global" scope]
|
64
|
+
end
|
65
|
+
|
66
|
+
Then /^user "(.*?)" should be selected in "(.*?)" scope$/ do |user, scope|
|
67
|
+
git.selected_user(scope.to_sym).should == GitSu::User.parse(user)
|
68
|
+
end
|
69
|
+
|
70
|
+
Then /^no user should be selected$/ do
|
71
|
+
step 'no user should be selected in "global" scope'
|
72
|
+
end
|
73
|
+
|
74
|
+
Then /^no user should be selected in "(.*?)" scope$/ do |scope|
|
75
|
+
git.selected_user(scope.to_sym).should be GitSu::User::NONE
|
76
|
+
end
|
77
|
+
|
78
|
+
Then /^the config file should be open in an editor$/ do
|
79
|
+
git.editing?.should be true
|
80
|
+
end
|
81
|
+
|
82
|
+
class Output
|
83
|
+
def messages
|
84
|
+
@messages ||= []
|
85
|
+
end
|
86
|
+
|
87
|
+
def puts(message = nil)
|
88
|
+
messages << "#{message}"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class StubGit < GitSu::Git
|
93
|
+
|
94
|
+
attr_accessor :users
|
95
|
+
|
96
|
+
def initialize
|
97
|
+
clear_users
|
98
|
+
@editing = false
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_config(scope, key)
|
102
|
+
if scope == :derived
|
103
|
+
config(:local)[key] || config(:global)[key] || config(:system)[key] || ""
|
104
|
+
else
|
105
|
+
config(scope)[key] || ""
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def set_config(scope, key, value)
|
110
|
+
config(scope)[key] = value
|
111
|
+
end
|
112
|
+
|
113
|
+
def unset_config(scope, key)
|
114
|
+
config(scope).delete key
|
115
|
+
end
|
116
|
+
|
117
|
+
def list_config(scope)
|
118
|
+
config(scope).map {|k,v| "#{k}=#{v}"}
|
119
|
+
end
|
120
|
+
|
121
|
+
def remove_config_section(scope, section)
|
122
|
+
config(scope).reject! {|k,v| k =~ /^#{section}\./ }
|
123
|
+
end
|
124
|
+
|
125
|
+
def clear_users
|
126
|
+
[:local, :global, :system].each do |scope|
|
127
|
+
clear_user(scope)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def config(scope)
|
132
|
+
@config ||= {}
|
133
|
+
@config[scope] ||= {}
|
134
|
+
end
|
135
|
+
|
136
|
+
def edit_gitsu_config
|
137
|
+
@editing = true
|
138
|
+
end
|
139
|
+
|
140
|
+
def color_output?
|
141
|
+
false
|
142
|
+
end
|
143
|
+
|
144
|
+
def render(user)
|
145
|
+
user.to_s
|
146
|
+
end
|
147
|
+
|
148
|
+
def render_user(scope)
|
149
|
+
render selected_user(scope)
|
150
|
+
end
|
151
|
+
|
152
|
+
def editing?
|
153
|
+
@editing
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def output
|
158
|
+
@output ||= Output.new
|
159
|
+
end
|
160
|
+
|
161
|
+
def git
|
162
|
+
@git ||= StubGit.new
|
163
|
+
end
|
164
|
+
|
165
|
+
def gitsu
|
166
|
+
@gitsu ||= GitSu::Gitsu.new(switcher, output)
|
167
|
+
end
|
168
|
+
|
169
|
+
def switcher
|
170
|
+
@switcher ||= GitSu::Switcher.new(git, user_list, output)
|
171
|
+
end
|
172
|
+
|
173
|
+
def user_list
|
174
|
+
@user_list ||= GitSu::UserList.new(user_list_file)
|
175
|
+
end
|
176
|
+
|
177
|
+
def user_list_file
|
178
|
+
@user_list_file ||= "/tmp/#{rand}"
|
179
|
+
end
|
180
|
+
|
181
|
+
def write_user_list(content)
|
182
|
+
File.open(user_list_file, "w") do |f|
|
183
|
+
f.write content
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def read_user_list
|
188
|
+
File.read user_list_file
|
189
|
+
end
|
190
|
+
|
191
|
+
After do
|
192
|
+
File.delete user_list_file if File.exist? user_list_file
|
193
|
+
end
|