summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2018-02-21 16:40:35 -0800
committerMichael Kozono <mkozono@gmail.com>2018-02-21 16:47:47 -0800
commitf634e3f73a125e14cdd1a78c82d4728fb212e2fc (patch)
tree958df32fce7444f35dc159a654292c256a26e104
parent414445f233c2b3dba228cf2798d21a0ab7a450ca (diff)
downloadgitlab-ce-mk/jej/commit-api-tracks-lfs.tar.gz
Refactor Lfs::FileModificationHandlermk/jej/commit-api-tracks-lfs
-rw-r--r--app/services/files/create_service.rb3
-rw-r--r--app/services/files/multi_service.rb10
-rw-r--r--app/services/lfs/file_modification_handler.rb35
3 files changed, 26 insertions, 22 deletions
diff --git a/app/services/files/create_service.rb b/app/services/files/create_service.rb
index 46acdc5406c..11d30076ccf 100644
--- a/app/services/files/create_service.rb
+++ b/app/services/files/create_service.rb
@@ -3,7 +3,8 @@ module Files
def create_commit!
handler = Lfs::FileModificationHandler.new(project, @branch_name)
- handler.new_file(@file_path, @file_content) do |content_or_lfs_pointer|
+ handler.begin_commit do |file_handler|
+ content_or_lfs_pointer = file_handler.new_file(@file_path, @file_content)
create_transformed_commit(content_or_lfs_pointer)
end
end
diff --git a/app/services/files/multi_service.rb b/app/services/files/multi_service.rb
index 21e4b606071..865baec066e 100644
--- a/app/services/files/multi_service.rb
+++ b/app/services/files/multi_service.rb
@@ -5,13 +5,11 @@ module Files
def create_commit!
handler = Lfs::FileModificationHandler.new(project, @branch_name)
- actions = actions_after_lfs_transformation(handler, params[:actions])
+ handler.begin_commit do |file_handler|
+ actions = actions_after_lfs_transformation(file_handler, params[:actions])
- success = commit_actions!(actions)
-
- handler.on_success if success
-
- success
+ commit_actions!(actions)
+ end
end
private
diff --git a/app/services/lfs/file_modification_handler.rb b/app/services/lfs/file_modification_handler.rb
index e7464e1454f..c684ba3f18a 100644
--- a/app/services/lfs/file_modification_handler.rb
+++ b/app/services/lfs/file_modification_handler.rb
@@ -1,4 +1,13 @@
module Lfs
+ # Usage: Open a `begin_commit` block, call `new_file` on the yielded object
+ # as many times as needed, and return the commit result to the block
+ #
+ # handler = Lfs::FileModificationHandler.new(project, branch_name)
+ #
+ # handler.begin_commit do |file_handler|
+ # content_or_lfs_pointer = file_handler.new_file(file_path, file_content)
+ # create_transformed_commit(content_or_lfs_pointer)
+ # end
class FileModificationHandler
attr_reader :project, :branch_name
@@ -9,28 +18,24 @@ module Lfs
@branch_name = branch_name
end
- def on_success
- on_success_actions.map(&:call)
- end
+ def begin_commit
+ result = yield(self)
- # In the block form this yields content to commit and links LfsObjectsProject on success
- # In the non-block form this returns content to commit and requires handler.on_success to be called to link LfsObjectsProjects
- def new_file(file_path, file_content)
- content = transform_content(file_path, file_content)
+ on_success
- if block_given?
- result = yield(content)
-
- on_success if result
+ result
+ end
- result
- else
- content
- end
+ def new_file(file_path, file_content)
+ transform_content(file_path, file_content)
end
private
+ def on_success
+ on_success_actions.map(&:call)
+ end
+
def transform_content(file_path, file_content)
if project.lfs_enabled? && lfs_file?(file_path)
lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)