Class: Berkshelf::Git
- Inherits:
-
Object
- Object
- Berkshelf::Git
- Defined in:
- lib/berkshelf/git.rb
Overview
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
-
.checkout(repo_path, ref) ⇒ Object
Checkout the given reference in the given repository.
-
.clone(uri, destination = Dir.mktmpdir) ⇒ String
Clone a remote Git repository to disk.
-
.find_git ⇒ String
Return an absolute path to the Git executable on your system.
-
.git(commands) ⇒ String
Shellout to the Git executable on your system with the given commands.
- .rev_parse(repo_path) ⇒ Object
-
.validate_uri(uri) ⇒ Boolean
Determines if the given URI is a valid Git URI.
- .validate_uri!(uri) ⇒ Object
Class Method Details
.checkout(repo_path, ref) ⇒ Object
Checkout the given reference in the given repository
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
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_git ⇒ String
Return an absolute path to the Git executable on your system
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
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
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.
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
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 |