git_handler 0.2.0 → 0.2.1

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.
data/lib/git_handler.rb CHANGED
@@ -7,4 +7,10 @@ require 'git_handler/request'
7
7
  require 'git_handler/git_command'
8
8
  require 'git_handler/session'
9
9
 
10
- module GitHandler ; end
10
+ module GitHandler
11
+ # Shorthand for GitHandler::Session.new
12
+ # @return [Session] new session instance
13
+ def self.new(config)
14
+ GitHandler::Session.new(config)
15
+ end
16
+ end
@@ -1,9 +1,9 @@
1
1
  class Hash
2
- def keys_all?(keys)
2
+ def include_all?(keys)
3
3
  self.keys.include_all?(keys)
4
4
  end
5
5
 
6
- def keys_any?(keys)
6
+ def include_any?(keys)
7
7
  self.keys.include_any?(keys)
8
8
  end
9
9
  end
@@ -1,6 +1,6 @@
1
1
  module GitHandler
2
2
  module GitCommand
3
- GIT_COMMAND = /^(git-upload-pack|git upload-pack|git-upload-archive|git upload-archive|git-receive-pack|git receive-pack) '(.*)'$/
3
+ GIT_COMMAND = /\A(git-upload-pack|git upload-pack|git-upload-archive|git upload-archive|git-receive-pack|git receive-pack) '(.*)'\z/
4
4
 
5
5
  COMMANDS_READONLY = [
6
6
  'git-upload-pack',
@@ -43,4 +43,4 @@ module GitHandler
43
43
  COMMANDS_WRITE.include?(cmd)
44
44
  end
45
45
  end
46
- end
46
+ end
@@ -8,9 +8,7 @@ module GitHandler
8
8
  attr_reader :log
9
9
 
10
10
  # Initialize a new Session
11
- #
12
- # config - GitHandler::Configuration instance
13
- #
11
+ # @param [Configuration] config an existing configuration instance
14
12
  def initialize(config=nil)
15
13
  unless config.kind_of?(GitHandler::Configuration)
16
14
  raise SessionError, 'Configuration required!'
@@ -28,12 +26,11 @@ module GitHandler
28
26
  @log = Logger.new(@config.log_path)
29
27
  end
30
28
 
31
- # Execute session
32
- #
33
- # args - Command arguments
34
- # env - Environment parameters
35
- # run_git - Execute git shell if no block provided#
36
- #
29
+ # Execute session
30
+ #
31
+ # @param [Array] args session arguments
32
+ # @param [Hash] env hash with environment variables, use ENV.to_hash.dup
33
+ # @param [Boolean] run_git execute git command if set to true
37
34
  def execute(args, env, run_git=true)
38
35
  @args = args
39
36
  @env = env
@@ -77,9 +74,12 @@ module GitHandler
77
74
  # exec("ssh", "git@TARGET", "#{args.join(' ')}")
78
75
  end
79
76
 
80
- # Execute session in safe manner, catch all exceptions
81
- # and terminate session
82
- #
77
+ # Execute session with catch-all-exceptions wrapper
78
+ # terminates session on SessionError or Exception
79
+ #
80
+ # @param [Array] args session arguments
81
+ # @param [Hash] env hash with environment variables, use ENV.to_hash.dup
82
+ # @param [Boolean] run_git execute git command if set to true
83
83
  def execute_safe(args, env, run_git=true)
84
84
  begin
85
85
  execute(args, env, run_git)
@@ -94,9 +94,8 @@ module GitHandler
94
94
 
95
95
  # Terminate session execution
96
96
  #
97
- # reason - Process termination reason message
98
- # exit_status - Exit code (default: 1)
99
- #
97
+ # @param [String] reason
98
+ # @param [Fixnum] exit_status
100
99
  def terminate(reason='', exit_status=1)
101
100
  logger.error("Session terminated. Reason: #{reason}")
102
101
  $stderr.puts("Request failed: #{reason}")
@@ -104,15 +103,13 @@ module GitHandler
104
103
  end
105
104
 
106
105
  # Check if session environment is valid
107
- #
108
106
  def valid_environment?
109
107
  env['USER'] == config.user && env['HOME'] == config.home_path
110
108
  end
111
109
 
112
110
  # Check if session request is valid
113
- #
114
111
  def valid_request?
115
- if env.keys_all?(['SSH_CLIENT', 'SSH_CONNECTION', 'SSH_ORIGINAL_COMMAND'])
112
+ if env.include_all?(['SSH_CLIENT', 'SSH_CONNECTION', 'SSH_ORIGINAL_COMMAND'])
116
113
  if valid_command?(env['SSH_ORIGINAL_COMMAND'])
117
114
  return true
118
115
  end
@@ -1,3 +1,3 @@
1
1
  module GitHandler
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/spec/command_spec.rb CHANGED
@@ -5,29 +5,29 @@ class TestInstance
5
5
  end
6
6
 
7
7
  describe GitHandler::GitCommand do
8
- before do
9
- @obj = TestInstance.new
10
- end
8
+ let(:command) { TestInstance.new }
11
9
 
12
10
  it 'detects a valid git command' do
13
- @obj.valid_command?("invalid command").should be_false
14
- @obj.valid_command?("git-receive-pack").should be_false
15
- @obj.valid_command?("git-receive-pack repo.git").should be_false
16
- @obj.valid_command?("git-receive-pack 'repo'").should be_true
17
- @obj.valid_command?("git-receive-pack 'repo.git'").should be_true
11
+ command.valid_command?("invalid command").should be_false
12
+ command.valid_command?("line1\nline2").should be_false
13
+ command.valid_command?("git-receive-pack").should be_false
14
+ command.valid_command?("git-receive-pack repo.git").should be_false
15
+ command.valid_command?("git-receive-pack 'repo'").should be_true
16
+ command.valid_command?("git-receive-pack 'repo.git'").should be_true
17
+ command.valid_command?("git-receive-pack 'repo'\ngit-upload-pack 'repo'").should be_false
18
18
  end
19
19
 
20
20
  context '.parse_command' do
21
21
  it 'raises error on invalid git command' do
22
- proc { @obj.parse_command("invalid command") }.
22
+ proc { command.parse_command("invalid command") }.
23
23
  should raise_error GitHandler::ParseError
24
24
 
25
- proc { @obj.parse_command("git-receive-pack 'repo.git'") }.
25
+ proc { command.parse_command("git-receive-pack 'repo.git'") }.
26
26
  should_not raise_error GitHandler::ParseError
27
27
  end
28
28
 
29
29
  it 'returns a proper action and repo' do
30
- result = @obj.parse_command("git-receive-pack 'repo.git'")
30
+ result = command.parse_command("git-receive-pack 'repo.git'")
31
31
  result.should be_a Hash
32
32
  result.should eql(
33
33
  :action => 'git-receive-pack',
@@ -39,17 +39,17 @@ describe GitHandler::GitCommand do
39
39
  end
40
40
 
41
41
  it 'detects read command' do
42
- @obj.read_command?('git-receive-pack').should be_false
43
- @obj.read_command?('git-upload-pack').should be_true
44
- @obj.read_command?('git upload-pack').should be_true
45
- @obj.read_command?('git-upload-archive').should be_true
46
- @obj.read_command?('git upload-archive').should be_true
42
+ command.read_command?('git-receive-pack').should be_false
43
+ command.read_command?('git-upload-pack').should be_true
44
+ command.read_command?('git upload-pack').should be_true
45
+ command.read_command?('git-upload-archive').should be_true
46
+ command.read_command?('git upload-archive').should be_true
47
47
  end
48
48
 
49
49
  it 'detects write command' do
50
- @obj.write_command?("git-upload-pack").should be_false
51
- @obj.write_command?("git-upload-archive").should be_false
52
- @obj.write_command?("git receive-pack").should be_true
53
- @obj.write_command?("git-receive-pack").should be_true
50
+ command.write_command?("git-upload-pack").should be_false
51
+ command.write_command?("git-upload-archive").should be_false
52
+ command.write_command?("git receive-pack").should be_true
53
+ command.write_command?("git-receive-pack").should be_true
54
54
  end
55
55
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ it { should respond_to :include_all? }
5
+ it { should respond_to :include_any? }
6
+
7
+ it 'should include all elements' do
8
+ [1,2,3].include_all?([1,2,3]).should be_true
9
+ [1,2,3].include_all?([1,2]).should be_true
10
+ [1,2].include_all?([1,2,3]).should be_false
11
+ [1,2].include_all?([4,5,6]).should be_false
12
+ [].include_all?([4,5,6]).should be_false
13
+ [].include_all?([]).should be_true
14
+ end
15
+
16
+ it 'should include any elements' do
17
+ [1,2,3].include_any?([1,2,3]).should be_true
18
+ [1,2,3].include_any?([1,2]).should be_true
19
+ [1,2,3].include_any?([1,4,5]).should be_true
20
+ [1,2,3].include_any?([4,5,6]).should be_false
21
+ [].include_any?([]).should be_false
22
+ end
23
+ end
24
+
25
+ describe Hash do
26
+ it { should respond_to :include_all? }
27
+ it { should respond_to :include_any? }
28
+
29
+ it 'should include all keys' do
30
+ ({'a' => 1, 'b' => 2, 'c' => 3}.include_all?(['a','b','c'])).should be_true
31
+ ({'a' => 1, 'b' => 2, 'c' => 3}.include_all?(['a','b'])).should be_true
32
+ ({'a' => 1, :b => 2}).include_all?(['a', 'b']).should be_false
33
+ ({'a' => 1, 'b' => 2}.include_all?(['c'])).should be_false
34
+ end
35
+
36
+ it 'should include any keys' do
37
+ ({'a' => 1, 'b' => 2}.include_any?(['a', 'b', 'c'])).should be_true
38
+ ({'a' => 1, :b => 2}.include_any?(['a', 'b', 'c'])).should be_true
39
+ ({'a' => 1, 'b' => 2}.include_any?([:a, :b])).should be_false
40
+ ({'a' => 1, 'b' => 2}.include_any?(['d', 'e'])).should be_false
41
+ end
42
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: git_handler
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dan Sosedoff
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-23 00:00:00 Z
13
+ date: 2012-06-22 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -87,6 +87,7 @@ files:
87
87
  - spec/authorized_keys_spec.rb
88
88
  - spec/command_spec.rb
89
89
  - spec/configuration_spec.rb
90
+ - spec/core_ext_spec.rb
90
91
  - spec/fixtures/.gitkeep
91
92
  - spec/public_key_spec.rb
92
93
  - spec/session_spec.rb
@@ -122,6 +123,7 @@ test_files:
122
123
  - spec/authorized_keys_spec.rb
123
124
  - spec/command_spec.rb
124
125
  - spec/configuration_spec.rb
126
+ - spec/core_ext_spec.rb
125
127
  - spec/fixtures/.gitkeep
126
128
  - spec/public_key_spec.rb
127
129
  - spec/session_spec.rb