Class: R10K::Git::Alternates

Inherits:
Object
  • Object
show all
Defined in:
lib/r10k/git/alternates.rb

Overview

Manage ‘$GIT_DIR/objects/info/alternates`

See Also:

  • gitrepository-layout(5)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_dir) ⇒ Alternates

Returns a new instance of Alternates.

Parameters:

  • git_dir (Pathname)

    The path to the git repository



13
14
15
16
# File 'lib/r10k/git/alternates.rb', line 13

def initialize(git_dir)
  @file = git_dir + File.join('objects', 'info', 'alternates')
  @entries = []
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/r10k/git/alternates.rb', line 10

def file
  @file
end

Instance Method Details

#add(path) ⇒ Object Also known as: <<



18
19
20
# File 'lib/r10k/git/alternates.rb', line 18

def add(path)
  write(to_a << path)
end

#add?(path) ⇒ true, false

Conditionally add path to the alternates file

Parameters:

  • path (String)

    The file path to add to the file if not already present

Returns:

  • (true, false)

    If the entry was added.



27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/r10k/git/alternates.rb', line 40

def include?(path)
  to_a.include?(path)
end

#readObject Also known as: to_a



55
56
57
58
59
60
61
# File 'lib/r10k/git/alternates.rb', line 55

def read
  entries = []
  if @file.file?
    entries = @file.readlines.map(&:chomp)
  end
  entries
end

#write(entries) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# 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") % {file: @file}
  end
  @file.open("w") do |fh|
    entries.each do |entry|
      fh.puts(entry)
    end
  end
end