2018-01-11 23:12:34 +00:00
|
|
|
module Lfs
|
2018-01-11 23:12:34 +00:00
|
|
|
# Usage: Open a `.link_lfs_objects` block, call `new_file` on the yielded object
|
|
|
|
# as many times as needed. LfsObjectProject links will be saved if the
|
|
|
|
# return value of the block is truthy.
|
|
|
|
#
|
|
|
|
# Calling `new_file` will check the path to see if it should be in LFS,
|
|
|
|
# save and LFS pointer of needed and return its content to be saved in
|
|
|
|
# a commit. If the file isn't LFS the untransformed content is returned.
|
|
|
|
#
|
|
|
|
# Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer|
|
|
|
|
# content_or_lfs_pointer = transformer.new_file(file_path, file_content)
|
|
|
|
# create_transformed_commit(content_or_lfs_pointer)
|
|
|
|
# end
|
2018-03-15 00:04:43 +00:00
|
|
|
class FileTransformer
|
2018-01-11 23:12:34 +00:00
|
|
|
attr_reader :project, :branch_name
|
|
|
|
|
|
|
|
delegate :repository, to: :project
|
|
|
|
|
|
|
|
def initialize(project, branch_name)
|
|
|
|
@project = project
|
|
|
|
@branch_name = branch_name
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
def self.link_lfs_objects(project, branch_name)
|
|
|
|
transformer = new(project, branch_name)
|
|
|
|
result = yield(transformer)
|
|
|
|
transformer.after_transform! if result
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
def new_file(file_path, file_content)
|
|
|
|
if project.lfs_enabled? && lfs_file?(file_path)
|
|
|
|
lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
|
|
|
|
lfs_object = create_lfs_object!(lfs_pointer_file, file_content)
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
on_success_actions << -> { link_lfs_object!(lfs_object) }
|
2018-01-11 23:12:34 +00:00
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
lfs_pointer_file.pointer
|
2018-01-11 23:12:34 +00:00
|
|
|
else
|
2018-01-11 23:12:34 +00:00
|
|
|
file_content
|
2018-01-11 23:12:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
def after_transform!
|
|
|
|
on_success_actions.map(&:call)
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def lfs_file?(file_path)
|
|
|
|
repository.attributes_at(branch_name, file_path)['filter'] == 'lfs'
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
def on_success_actions
|
|
|
|
@on_success_actions ||= []
|
|
|
|
end
|
|
|
|
|
2018-01-11 23:12:34 +00:00
|
|
|
def create_lfs_object!(lfs_pointer_file, file_content)
|
|
|
|
LfsObject.find_or_create_by(oid: lfs_pointer_file.sha256, size: lfs_pointer_file.size) do |lfs_object|
|
|
|
|
lfs_object.file = CarrierWaveStringFile.new(file_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_lfs_object!(lfs_object)
|
|
|
|
project.lfs_objects << lfs_object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|