Class: Librarian::Source::Git::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/librarian/source/git/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#environmentObject

Returns the value of attribute environment.



29
30
31
# File 'lib/librarian/source/git/repository.rb', line 29

def environment
  @environment
end

#git_ops_historyObject

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

#pathObject

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

.binObject



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

.git_versionObject



23
24
25
26
# File 'lib/librarian/source/git/repository.rb', line 23

def git_version
  command = %W[#{bin} version --silent]
  Posix.run!(command).strip =~ /\Agit version (\d+(\.\d+)*)/ && $1
end

Instance Method Details

#checked_out?(sha) ⇒ Boolean

Returns:

  • (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, options ={ })
  command = %W(checkout #{reference} --quiet)
  command << "--force" if options[: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_hashObject



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_remoteObject



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, options = { })
  command = %W(fetch #{remote} --quiet)
  command << "--tags" if options[:tags]
  run!(command, :chdir => true)
end

#git?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/librarian/source/git/repository.rb', line 38

def git?
  path.join('.git').exist?
end

#has_commit?(sha) ⇒ Boolean

Returns:

  • (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_namesObject



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_namesObject



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