Class: Berkshelf::Git
- Inherits:
-
Object
- Object
- Berkshelf::Git
- 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
-
.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
-
.revision_from_ref(repo_path, ref) ⇒ String
Return the sha revision for the given reference or revision in the given repository.
-
.show_ref(repo_path, ref) ⇒ String
Return the sha revision for the given reference in the given repository.
-
.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', '-qf', revision_from_ref(repo_path, 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
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
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
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.
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
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.
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
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 |