Class: Librarian::Source::Git::Repository
- Inherits:
-
Object
- Object
- Librarian::Source::Git::Repository
- Defined in:
- lib/librarian/source/git/repository.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#git_ops_history ⇒ Object
readonly
Returns the value of attribute git_ops_history.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #checked_out?(sha) ⇒ Boolean
- #checkout!(reference, options = { }) ⇒ Object
- #clean! ⇒ Object
- #clone!(repository_url) ⇒ Object
- #current_commit_hash ⇒ Object
- #default_remote ⇒ Object
- #fetch!(remote, options = { }) ⇒ Object
- #git? ⇒ Boolean
- #has_commit?(sha) ⇒ Boolean
- #hash_from(remote, reference) ⇒ Object
-
#initialize(environment, path) ⇒ Repository
constructor
A new instance of Repository.
- #remote_branch_names ⇒ Object
- #remote_names ⇒ Object
- #reset_hard! ⇒ Object
Constructor Details
#initialize(environment, path) ⇒ Repository
Returns a new instance of Repository.
32 33 34 35 36 |
# File 'lib/librarian/source/git/repository.rb', line 32 def initialize(environment, path) self.environment = environment self.path = Pathname.new(path) self.git_ops_history = [] end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
29 30 31 |
# File 'lib/librarian/source/git/repository.rb', line 29 def environment @environment end |
#git_ops_history ⇒ Object
Returns the value of attribute git_ops_history.
29 30 31 |
# File 'lib/librarian/source/git/repository.rb', line 29 def git_ops_history @git_ops_history end |
#path ⇒ Object
Returns the value of attribute path.
29 30 31 |
# File 'lib/librarian/source/git/repository.rb', line 29 def path @path end |
Class Method Details
.bin ⇒ Object
19 20 21 |
# File 'lib/librarian/source/git/repository.rb', line 19 def bin @bin ||= Posix.which!("git") end |
.clone!(environment, path, repository_url) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/librarian/source/git/repository.rb', line 11 def clone!(environment, path, repository_url) path = Pathname.new(path) path.mkpath git = new(environment, path) git.clone!(repository_url) git end |
Instance Method Details
#checked_out?(sha) ⇒ Boolean
80 81 82 |
# File 'lib/librarian/source/git/repository.rb', line 80 def checked_out?(sha) current_commit_hash == sha end |
#checkout!(reference, options = { }) ⇒ Object
51 52 53 54 55 |
# File 'lib/librarian/source/git/repository.rb', line 51 def checkout!(reference, ={ }) command = %W(checkout #{reference} --quiet) command << "--force" if [:force] run!(command, :chdir => true) end |
#clean! ⇒ Object
68 69 70 71 |
# File 'lib/librarian/source/git/repository.rb', line 68 def clean! command = %w(clean -x -d --force --force) run!(command, :chdir => true) end |
#clone!(repository_url) ⇒ Object
46 47 48 49 |
# File 'lib/librarian/source/git/repository.rb', line 46 def clone!(repository_url) command = %W(clone #{repository_url} . --quiet) run!(command, :chdir => true) end |
#current_commit_hash ⇒ Object
114 115 116 117 |
# File 'lib/librarian/source/git/repository.rb', line 114 def current_commit_hash command = %W(rev-parse HEAD --quiet) run!(command, :chdir => true).strip! end |
#default_remote ⇒ Object
42 43 44 |
# File 'lib/librarian/source/git/repository.rb', line 42 def default_remote "origin" end |
#fetch!(remote, options = { }) ⇒ Object
57 58 59 60 61 |
# File 'lib/librarian/source/git/repository.rb', line 57 def fetch!(remote, = { }) command = %W(fetch #{remote} --quiet) command << "--tags" if [:tags] run!(command, :chdir => true) end |
#git? ⇒ Boolean
38 39 40 |
# File 'lib/librarian/source/git/repository.rb', line 38 def git? path.join('.git').exist? end |
#has_commit?(sha) ⇒ Boolean
73 74 75 76 77 78 |
# File 'lib/librarian/source/git/repository.rb', line 73 def has_commit?(sha) command = %W(log -1 --no-color --format=tformat:%H #{sha}) run!(command, :chdir => true).strip == sha rescue Posix::CommandFailure => e false end |
#hash_from(remote, reference) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/librarian/source/git/repository.rb', line 104 def hash_from(remote, reference) branch_names = remote_branch_names[remote] if branch_names.include?(reference) reference = "#{remote}/#{reference}" end command = %W(rev-list #{reference} -1) run!(command, :chdir => true).strip end |
#remote_branch_names ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/librarian/source/git/repository.rb', line 89 def remote_branch_names remotes = remote_names.sort_by(&:length).reverse command = %W(branch -r --no-color) names = run!(command, :chdir => true).strip.lines.map(&:strip).to_a names.each{|n| n.gsub!(/\s*->.*$/, "")} names.reject!{|n| n =~ /\/HEAD$/} Hash[remotes.map do |r| matching_names = names.select{|n| n.start_with?("#{r}/")} matching_names.each{|n| names.delete(n)} matching_names.each{|n| n.slice!(0, r.size + 1)} [r, matching_names] end] end |
#remote_names ⇒ Object
84 85 86 87 |
# File 'lib/librarian/source/git/repository.rb', line 84 def remote_names command = %W(remote) run!(command, :chdir => true).strip.lines.map(&:strip) end |
#reset_hard! ⇒ Object
63 64 65 66 |
# File 'lib/librarian/source/git/repository.rb', line 63 def reset_hard! command = %W(reset --hard --quiet) run!(command, :chdir => true) end |