Class: Berkshelf::Git

Inherits:
Object
  • Object
show all
Extended by:
Buff::ShellOut
Defined in:
lib/berkshelf/git.rb

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', '-qf', revision_from_ref(repo_path, 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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/berkshelf/git.rb', line 122

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



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

def git(*command)
  command.unshift(git_cmd)
  command_str = command.map { |p| quote_cmd_arg(p) }.join(' ')
  response    = shell_out(command_str)

  unless response.success?
    raise GitError.new(response.stderr.strip)
  end

  response.stdout.strip
end

.rev_parse(repo_path) ⇒ Object

Parameters:



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

.revision_from_ref(repo_path, ref) ⇒ String

Return the sha revision for the given reference or revision in the given repository

This method is useful when you have either a sha revision, tag, or branch and you’d like to end up with a sha revision.

Parameters:

  • repo_path (String)

    path to a Git repo on disk

  • ref (String)

    reference to show

Returns:

  • (String)

    the sha revision for the given ref

Raises:



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

def revision_from_ref(repo_path, ref)
  begin
    show_ref(repo_path, ref)
  rescue GitError
    begin
      git('rev-parse', ref)
      ref
    rescue GitError
      raise InvalidGitRef, ref
    end
  end
end

.show_ref(repo_path, ref) ⇒ String

Return the sha revision for the given reference in the given repository

Parameters:

  • repo_path (String)

    path to a Git repo on disk

  • ref (String)

    reference to show

Returns:

  • (String)

    the sha revision for the given ref

Raises:



79
80
81
82
83
84
85
86
87
# File 'lib/berkshelf/git.rb', line 79

def show_ref(repo_path, ref)
  Dir.chdir repo_path do
    lines = git('show-ref', ref).lines.to_a

    raise AmbiguousGitRef, ref if lines.size > 1

    lines.first.chomp.split(/\s/).first
  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)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/berkshelf/git.rb', line 150

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:



170
171
172
173
174
175
176
# File 'lib/berkshelf/git.rb', line 170

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

  true
end