Class: Berkshelf::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/git.rb

Overview

Author:

Constant Summary collapse

GIT_REGEXP =
URI.regexp(%w(http https ssh git+ssh git rsync))
SCP_REGEXP =
/^(.+@)?[\w\d\.-]+:.*$/
HAS_QUOTE_RE =
%r{\"}.freeze
HAS_SPACE_RE =
%r{\s}.freeze

Class Method Summary collapse

Class Method Details

.checkout(repo_path, ref) ⇒ Object

Checkout the given reference in the given repository

Parameters:

  • repo_path (String)

    path to a Git repo on disk

  • ref (String)

    reference to checkout



55
56
57
58
59
# File 'lib/berkshelf/git.rb', line 55

def checkout(repo_path, ref)
  Dir.chdir repo_path do
    git("checkout", "-q", ref)
  end
end

.clone(uri, destination = Dir.mktmpdir) ⇒ String

Clone a remote Git repository to disk

Parameters:

  • uri (String)

    a Git URI to clone

  • destination (#to_s) (defaults to: Dir.mktmpdir)

    a local path on disk to clone to

Returns:

  • (String)

    the destination the URI was cloned to



43
44
45
46
47
# File 'lib/berkshelf/git.rb', line 43

def clone(uri, destination = Dir.mktmpdir)
  git("clone", uri, destination.to_s)

  destination
end

.find_gitString

Return an absolute path to the Git executable on your system

Returns:

  • (String)

    absolute path to git executable

Raises:

  • (GitNotFound)

    if executable is not found in system path



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/berkshelf/git.rb', line 74

def find_git
  git_path = nil
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    git_path = detect_git_path(path)
    break if git_path
  end

  unless git_path
    raise GitNotFound
  end

  return git_path
end

.git(commands) ⇒ String

Shellout to the Git executable on your system with the given commands.

Parameters:

Returns:

  • (String)

    the output of the execution of the Git command



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/berkshelf/git.rb', line 21

def git(*command)
  command.unshift(git_cmd)
  command_str = command.map { |p| quote_cmd_arg(p) }.join(' ')
  cmd = Mixlib::ShellOut.new(command_str)
  cmd.run_command

  unless cmd.exitstatus == 0
    raise GitError.new(cmd.stderr)
  end

  cmd.stdout.chomp
end

.rev_parse(repo_path) ⇒ Object

Parameters:

  • repo_path (Strin)


62
63
64
65
66
# File 'lib/berkshelf/git.rb', line 62

def rev_parse(repo_path)
  Dir.chdir repo_path do
    git("rev-parse", "HEAD")
  end
end

.validate_uri(uri) ⇒ Boolean

Determines if the given URI is a valid Git URI. A valid Git URI is a string containing the location of a Git repository by either the Git protocol, SSH protocol, or HTTPS protocol.

Examples:

Valid Git protocol URI

"git://github.com/reset/thor-foodcritic.git"

Valid HTTPS URI

"https://github.com/reset/solve.git"

Valid SSH protocol URI

"[email protected]:reset/solve.git"

Parameters:

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/berkshelf/git.rb', line 102

def validate_uri(uri)

  unless uri.is_a?(String)
    return false
  end

  unless uri.slice(GIT_REGEXP).nil?
    return true
  end

  unless uri.slice(SCP_REGEXP).nil?
    return true
  end

  false
end

.validate_uri!(uri) ⇒ Object

Raises:

  • (InvalidGitURI)

    if the given object is not a String containing a valid Git URI

See Also:



122
123
124
125
126
127
128
# File 'lib/berkshelf/git.rb', line 122

def validate_uri!(uri)
  unless validate_uri(uri)
    raise InvalidGitURI.new(uri)
  end

  true
end