Class: Dependabot::DependencyFile

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/dependency_file.rb

Defined Under Namespace

Classes: ContentEncoding, Mode, Operation

Constant Summary collapse

VALID_MODES =
T.let(
  [Mode::FILE, Mode::EXECUTABLE, Mode::TREE, Mode::SUBMODULE, Mode::SYMLINK].freeze,
  T::Array[String]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, content:, directory: "/", type: "file", support_file: false, vendored_file: false, symlink_target: nil, content_encoding: ContentEncoding::UTF_8, deleted: false, operation: Operation::UPDATE, mode: nil) ⇒ DependencyFile

Returns a new instance of DependencyFile.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dependabot/dependency_file.rb', line 85

def initialize(
  name:,
  content:,
  directory: "/",
  type: "file",
  support_file: false,
  vendored_file: false,
  symlink_target: nil,
  content_encoding: ContentEncoding::UTF_8,
  deleted: false,
  operation: Operation::UPDATE,
  mode: nil
)
  @name = name
  @content = content
  @directory = T.let(clean_directory(directory), String)
  @symlink_target = symlink_target
  @support_file = support_file
  @vendored_file = vendored_file
  @content_encoding = content_encoding
  @operation = operation
  @mode = mode
  raise ArgumentError, "Invalid Git mode: #{mode}" if mode && !VALID_MODES.include?(mode)

  # Make deleted override the operation. Deleted is kept when operation
  # was introduced to keep compatibility with downstream dependants.
  @operation = Operation::DELETE if deleted

  # Type is used *very* sparingly. It lets the git_modules updater know that
  # a "file" is actually a submodule, and lets our Go updaters know which
  # file represents the main.go.
  # New use cases should be avoided if at all possible (and use the
  # support_file flag instead)
  @type = type

  return unless (type == "symlink") ^ symlink_target

  raise "Symlinks must specify a target!" unless symlink_target
  raise "Only symlinked files must specify a target!" if symlink_target
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



16
17
18
# File 'lib/dependabot/dependency_file.rb', line 16

def content
  @content
end

#content_encodingObject

Returns the value of attribute content_encoding.



36
37
38
# File 'lib/dependabot/dependency_file.rb', line 36

def content_encoding
  @content_encoding
end

#directoryObject

Returns the value of attribute directory.



21
22
23
# File 'lib/dependabot/dependency_file.rb', line 21

def directory
  @directory
end

#modeObject

Returns the value of attribute mode.



42
43
44
# File 'lib/dependabot/dependency_file.rb', line 42

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/dependabot/dependency_file.rb', line 13

def name
  @name
end

#operationObject

Returns the value of attribute operation.



39
40
41
# File 'lib/dependabot/dependency_file.rb', line 39

def operation
  @operation
end

#support_fileObject

Returns the value of attribute support_file.



27
28
29
# File 'lib/dependabot/dependency_file.rb', line 27

def support_file
  @support_file
end

Returns the value of attribute symlink_target.



33
34
35
# File 'lib/dependabot/dependency_file.rb', line 33

def symlink_target
  @symlink_target
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/dependabot/dependency_file.rb', line 24

def type
  @type
end

#vendored_fileObject

Returns the value of attribute vendored_file.



30
31
32
# File 'lib/dependabot/dependency_file.rb', line 30

def vendored_file
  @vendored_file
end

Instance Method Details

#==(other) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/dependabot/dependency_file.rb', line 155

def ==(other)
  case other
  when DependencyFile
    my_hash = to_h.reject { |k| k == "support_file" }
    their_hash = other.to_h.reject { |k| k == "support_file" }
    my_hash == their_hash
  else
    false
  end
end

#binary?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/dependabot/dependency_file.rb', line 202

def binary?
  content_encoding == ContentEncoding::BASE64
end

#blob_oid(algorithm: :sha1) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dependabot/dependency_file.rb', line 217

def blob_oid(algorithm: :sha1)
  return nil unless content

  raw = decoded_content.dup.force_encoding(Encoding::BINARY)
  header = "blob #{raw.bytesize}\0".b
  digest = case algorithm
           when :sha256 then Digest::SHA256.new
           else Digest::SHA1.new
           end
  digest.update(header)
  digest.update(raw)
  digest.hexdigest
end

#decoded_contentObject



207
208
209
210
211
# File 'lib/dependabot/dependency_file.rb', line 207

def decoded_content
  return Base64.decode64(T.must(content)) if binary?

  T.must(content)
end

#deletedObject



187
188
189
# File 'lib/dependabot/dependency_file.rb', line 187

def deleted
  @operation == Operation::DELETE
end

#deleted=(deleted) ⇒ Object



192
193
194
# File 'lib/dependabot/dependency_file.rb', line 192

def deleted=(deleted)
  @operation = deleted ? Operation::DELETE : Operation::UPDATE
end

#deleted?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/dependabot/dependency_file.rb', line 197

def deleted?
  deleted
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/dependabot/dependency_file.rb', line 172

def eql?(other)
  self == other
end

#hashObject



167
168
169
# File 'lib/dependabot/dependency_file.rb', line 167

def hash
  to_h.hash
end

#pathObject



145
146
147
# File 'lib/dependabot/dependency_file.rb', line 145

def path
  Pathname.new(File.join(directory, name)).cleanpath.to_path
end

#realpathObject



150
151
152
# File 'lib/dependabot/dependency_file.rb', line 150

def realpath
  (symlink_target || path).sub(%r{^/}, "")
end

#support_file?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/dependabot/dependency_file.rb', line 177

def support_file?
  @support_file
end

#to_hObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dependabot/dependency_file.rb', line 127

def to_h
  details = {
    "name" => name,
    "content" => content,
    "directory" => directory,
    "type" => type,
    "support_file" => support_file,
    "content_encoding" => content_encoding,
    "deleted" => deleted,
    "operation" => operation
  }
  details["mode"] = mode if mode

  details["symlink_target"] = symlink_target if symlink_target
  details
end

#vendored_file?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/dependabot/dependency_file.rb', line 182

def vendored_file?
  @vendored_file
end