class R10K::Git::Alternates

Manage `$GIT_DIR/objects/info/alternates`

@see man gitrepository-layout(5)

Attributes

file[R]

@attribute [r] file

@return [Pathname] The alternates file

Public Class Methods

new(git_dir) click to toggle source

@param git_dir [Pathname] The path to the git repository

# File lib/r10k/git/alternates.rb, line 13
def initialize(git_dir)
  @file = git_dir + File.join('objects', 'info', 'alternates')
  @entries = []
end

Public Instance Methods

<<(path)
Alias for: add
add(path) click to toggle source
# File lib/r10k/git/alternates.rb, line 18
def add(path)
  write(to_a << path)
end
Also aliased as: <<
add?(path) click to toggle source

Conditionally add path to the alternates file

@param path [String] The file path to add to the file if not already present @return [true, false] If the entry was added.

# File lib/r10k/git/alternates.rb, line 27
def add?(path)
  paths = read()

  add_entry = !paths.include?(path)

  if add_entry
    paths << path
    write(paths)
  end

  add_entry
end
include?(path) click to toggle source
# File lib/r10k/git/alternates.rb, line 40
def include?(path)
  to_a.include?(path)
end
read() click to toggle source
# File lib/r10k/git/alternates.rb, line 55
def read
  entries = []
  if @file.file?
    entries = @file.readlines.map(&:chomp)
  end
  entries
end
Also aliased as: to_a
to_a()
Alias for: read
write(entries) click to toggle source
# File lib/r10k/git/alternates.rb, line 44
def write(entries)
  if ! @file.parent.directory?
    raise R10K::Git::GitError, "Cannot write #{@file}; parent directory does not exist"
  end
  @file.open("w") do |fh|
    entries.each do |entry|
      fh.puts(entry)
    end
  end
end