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.



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

#environmentObject

Returns the value of attribute environment.



42
43
44
# File 'lib/librarian/source/git/repository.rb', line 42

def environment
  @environment
end

#pathObject

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

.binObject



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

Returns:

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



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_remoteObject



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

#git?Boolean

Returns:

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



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_namesObject



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(options = { })
  version!(options).strip
end

#version!(options = { }) ⇒ Object



62
63
64
65
66
67
# File 'lib/librarian/source/git/repository.rb', line 62

def version!(options = { })
  silent = options.delete(:silent)

  command = %w(--version)
  run!(command, :silent => silent)
end