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.
-
#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
- #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
- #version(options = { }) ⇒ Object
- #version!(options = { }) ⇒ Object
Constructor Details
#initialize(environment, path) ⇒ Repository
Returns a new instance of Repository.
45 46 47 48 |
# File 'lib/librarian/source/git/repository.rb', line 45 def initialize(environment, path) self.environment = environment self.path = Pathname.new(path) end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
42 43 44 |
# File 'lib/librarian/source/git/repository.rb', line 42 def environment @environment end |
#path ⇒ Object
Returns the value of attribute path.
42 43 44 |
# File 'lib/librarian/source/git/repository.rb', line 42 def path @path end |
Class Method Details
.bin ⇒ Object
17 18 19 |
# File 'lib/librarian/source/git/repository.rb', line 17 def bin @bin ||= which("git") or raise Error, "cannot find git" end |
.clone!(environment, path, repository_url) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/librarian/source/git/repository.rb', line 9 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
96 97 98 |
# File 'lib/librarian/source/git/repository.rb', line 96 def checked_out?(sha) current_commit_hash == sha end |
#checkout!(reference, options = { }) ⇒ Object
74 75 76 77 78 |
# File 'lib/librarian/source/git/repository.rb', line 74 def checkout!(reference, ={ }) command = %W(checkout #{reference} --quiet) command << "--force" if [:force] run!(command, :chdir => true) end |
#clean! ⇒ Object
91 92 93 94 |
# File 'lib/librarian/source/git/repository.rb', line 91 def clean! command = %w(clean -x -d --force --force) run!(command, :chdir => true) end |
#clone!(repository_url) ⇒ Object
69 70 71 72 |
# File 'lib/librarian/source/git/repository.rb', line 69 def clone!(repository_url) command = %W(clone #{repository_url} . --quiet) run!(command, :chdir => true) end |
#current_commit_hash ⇒ Object
130 131 132 133 |
# File 'lib/librarian/source/git/repository.rb', line 130 def current_commit_hash command = %W(rev-parse HEAD --quiet) run!(command, :chdir => true).strip! end |
#default_remote ⇒ Object
54 55 56 |
# File 'lib/librarian/source/git/repository.rb', line 54 def default_remote "origin" end |
#fetch!(remote, options = { }) ⇒ Object
80 81 82 83 84 |
# File 'lib/librarian/source/git/repository.rb', line 80 def fetch!(remote, = { }) command = %W(fetch #{remote} --quiet) command << "--tags" if [:tags] run!(command, :chdir => true) end |
#git? ⇒ Boolean
50 51 52 |
# File 'lib/librarian/source/git/repository.rb', line 50 def git? path.join('.git').exist? end |
#hash_from(remote, reference) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/librarian/source/git/repository.rb', line 120 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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/librarian/source/git/repository.rb', line 105 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
100 101 102 103 |
# File 'lib/librarian/source/git/repository.rb', line 100 def remote_names command = %W(remote) run!(command, :chdir => true).strip.lines.map(&:strip) end |
#reset_hard! ⇒ Object
86 87 88 89 |
# File 'lib/librarian/source/git/repository.rb', line 86 def reset_hard! command = %W(reset --hard --quiet) run!(command, :chdir => true) end |
#version(options = { }) ⇒ Object
58 59 60 |
# File 'lib/librarian/source/git/repository.rb', line 58 def version( = { }) version!().strip end |
#version!(options = { }) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/librarian/source/git/repository.rb', line 62 def version!( = { }) silent = .delete(:silent) command = %w(--version) run!(command, :silent => silent) end |