Class: Bringit::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/bringit/index.rb

Constant Summary collapse

DEFAULT_MODE =
0o100644

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ Index

Returns a new instance of Index.



7
8
9
10
# File 'lib/bringit/index.rb', line 7

def initialize(repository)
  @repository = repository
  @raw_index = repository.rugged.index
end

Instance Attribute Details

#raw_indexObject (readonly)

Returns the value of attribute raw_index.



5
6
7
# File 'lib/bringit/index.rb', line 5

def raw_index
  @raw_index
end

#repositoryObject (readonly)

Returns the value of attribute repository.



5
6
7
# File 'lib/bringit/index.rb', line 5

def repository
  @repository
end

Instance Method Details

#create(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/bringit/index.rb', line 22

def create(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  if file_entry
    raise Bringit::Repository::InvalidBlobName.new("Filename already exists")
  end

  add_blob(options)
end

#create_dir(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bringit/index.rb', line 33

def create_dir(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  if file_entry
    raise Bringit::Repository::InvalidBlobName.new("Directory already exists as a file")
  end

  if dir_exists?(options[:file_path])
    raise Bringit::Repository::InvalidBlobName.new("Directory already exists")
  end

  options = options.dup
  options[:file_path] += '/.gitkeep'
  options[:content] = ''

  add_blob(options)
end

#delete(options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/bringit/index.rb', line 80

def delete(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  raw_index.remove(options[:file_path])
end

#dir_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/bringit/index.rb', line 18

def dir_exists?(path)
  raw_index.find { |entry| entry[:path].start_with?("#{path}/") }
end

#move(options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bringit/index.rb', line 63

def move(options)
  options = normalize_options(options)

  file_entry = get(options[:previous_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  if get(options[:file_path])
    raise IndexError, "A file with this name already exists"
  end

  raw_index.remove(options[:previous_path])

  add_blob(options, mode: file_entry[:mode])
end

#update(options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/bringit/index.rb', line 52

def update(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  add_blob(options, mode: file_entry[:mode])
end

#write_treeObject



14
15
16
# File 'lib/bringit/index.rb', line 14

def write_tree
  raw_index.write_tree(repository.rugged)
end