Skip to content

Commit

Permalink
Extract git mixin into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed May 21, 2014
1 parent dc57620 commit 4bb47b2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion features/commands/install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ Feature: berks install
"""
Repository not found.
"""
And the exit status should be "GitLocation::GitError"
And the exit status should be "GitError"

Scenario: transitive dependencies in metadata
Given the cookbook store contains a cookbook "fake" "1.0.0" with dependencies:
Expand Down
1 change: 1 addition & 0 deletions lib/berkshelf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Berkshelf

module Mixin
autoload :DSLEval, 'berkshelf/mixin/dsl_eval'
autoload :Git, 'berkshelf/mixin/git'
autoload :Logging, 'berkshelf/mixin/logging'
end

Expand Down
28 changes: 28 additions & 0 deletions lib/berkshelf/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,32 @@ def to_s
"is the standard error from the command:\n\n#{@output}"
end
end

# Git errors
# ------------------------------
class GitError < BerkshelfError; status_code(400); end

class GitNotInstalled < GitError
def initialize
super 'You need to install Git before you can download ' \
'cookbooks from git repositories. For more information, please ' \
'see the Git docs: http://git-scm.org. If you have git installed, ' \
'please make sure it is in your $PATH and accessible by the user ' \
'running this command.'
end
end

class GitCommandError < GitError
def initialize(command, path, stderr = nil)
out = "Git error: command `git #{command}` failed. If this error "
out << "persists, try removing the cache directory at '#{path}'."

if stderr
out << "Output from the command:\n\n"
out << stderr
end

super(out)
end
end
end
4 changes: 3 additions & 1 deletion lib/berkshelf/init_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Berkshelf
class InitGenerator < BaseGenerator
include Mixin::Git

def initialize(*args)
super(*args)
if @options[:cookbook_name]
Expand Down Expand Up @@ -75,7 +77,7 @@ def generate

unless File.exists?(target.join('.git'))
inside target do
run 'git init', capture: true
git 'init'
end
end
end
Expand Down
47 changes: 1 addition & 46 deletions lib/berkshelf/locations/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,7 @@

module Berkshelf
class GitLocation < BaseLocation
class GitError < BerkshelfError; status_code(400); end

class GitNotInstalled < GitError
def initialize
super 'You need to install Git before you can download ' \
'cookbooks from git repositories. For more information, please ' \
'see the Git docs: http://git-scm.org.'
end
end

class GitCommandError < GitError
def initialize(command, path, stderr = nil)
out = "Git error: command `git #{command}` failed. If this error "
out << "persists, try removing the cache directory at '#{path}'."

if stderr
out << "Output from the command:\n\n"
out << stderr
end

super(out)
end
end
include Mixin::Git

attr_reader :uri
attr_reader :branch
Expand Down Expand Up @@ -146,29 +124,6 @@ def shortref

private

# Perform a git command.
#
# @param [String] command
# the command to run
# @param [Boolean] error
# whether to raise error if the command fails
#
# @raise [String]
# the +$stdout+ from the command
def git(command, error = true)
unless Berkshelf.which('git') || Berkshelf.which('git.exe')
raise GitNotInstalled.new
end

response = Buff::ShellOut.shell_out(%|git #{command}|)

if error && !response.success?
raise GitCommandError.new(command, cache_path, stderr = response.stderr)
end

response.stdout.strip
end

# Determine if this git repo has already been downloaded.
#
# @return [Boolean]
Expand Down
30 changes: 30 additions & 0 deletions lib/berkshelf/mixin/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'buff/shell_out'

module Berkshelf
module Mixin
module Git
# Perform a git command.
#
# @param [String] command
# the command to run
# @param [Boolean] error
# whether to raise error if the command fails
#
# @raise [String]
# the +$stdout+ from the command
def git(command, error = true)
unless Berkshelf.which('git') || Berkshelf.which('git.exe')
raise GitNotInstalled.new
end

response = Buff::ShellOut.shell_out(%|git #{command}|)

if error && !response.success?
raise GitCommandError.new(command, cache_path, response.stderr)
end

response.stdout.strip
end
end
end
end
4 changes: 2 additions & 2 deletions spec/unit/berkshelf/locations/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ def rev_parse(instance)

it 'raises an error if Git is not installed' do
Berkshelf.stub(:which).and_return(false)
expect { subject.git('foo') }.to raise_error(GitLocation::GitNotInstalled)
expect { subject.git('foo') }.to raise_error(GitNotInstalled)
end

it 'raises an error if the command fails' do
shell_out = double('shell_out', success?: false, stderr: nil)
Buff::ShellOut.stub(:shell_out).and_return(shell_out)
expect { subject.git('foo') }.to raise_error(GitLocation::GitCommandError)
expect { subject.git('foo') }.to raise_error(GitCommandError)
end
end
end
Expand Down

0 comments on commit 4bb47b2

Please sign in to comment.