Commit 7527408ad0c72ea7b7aba5c14550c5b08b6aae60
Exists in
spb-stable
and in
3 other branches
Merge branch 'feature/base64_content' into 'master'
Feature: base64 encoding for blob content
Showing
9 changed files
with
30 additions
and
11 deletions
Show diff stats
app/assets/stylesheets/generic/files.scss
app/contexts/files/create_context.rb
| ... | ... | @@ -33,7 +33,8 @@ module Files |
| 33 | 33 | new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, file_path) |
| 34 | 34 | created_successfully = new_file_action.commit!( |
| 35 | 35 | params[:content], |
| 36 | - params[:commit_message] | |
| 36 | + params[:commit_message], | |
| 37 | + params[:encoding] | |
| 37 | 38 | ) |
| 38 | 39 | |
| 39 | 40 | if created_successfully | ... | ... |
app/contexts/files/update_context.rb
| ... | ... | @@ -23,10 +23,11 @@ module Files |
| 23 | 23 | return error("You can only edit text files") |
| 24 | 24 | end |
| 25 | 25 | |
| 26 | - new_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path) | |
| 27 | - created_successfully = new_file_action.commit!( | |
| 26 | + edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path) | |
| 27 | + created_successfully = edit_file_action.commit!( | |
| 28 | 28 | params[:content], |
| 29 | - params[:commit_message] | |
| 29 | + params[:commit_message], | |
| 30 | + params[:encoding] | |
| 30 | 31 | ) |
| 31 | 32 | |
| 32 | 33 | if created_successfully | ... | ... |
app/views/projects/new_tree/show.html.haml
| ... | ... | @@ -15,6 +15,12 @@ |
| 15 | 15 | %span= @ref |
| 16 | 16 | |
| 17 | 17 | .form-group.commit_message-group |
| 18 | + = label_tag :encoding, class: "control-label" do | |
| 19 | + Encoding | |
| 20 | + .col-sm-10 | |
| 21 | + = select_tag :encoding, options_for_select([ "base64", "text" ], "text"), class: 'form-control' | |
| 22 | + | |
| 23 | + .form-group.commit_message-group | |
| 18 | 24 | = label_tag 'commit_message', class: "control-label" do |
| 19 | 25 | Commit message |
| 20 | 26 | .col-sm-10 | ... | ... |
doc/api/repositories.md
| ... | ... | @@ -400,6 +400,7 @@ Parameters: |
| 400 | 400 | |
| 401 | 401 | + `file_path` (optional) - Full path to new file. Ex. lib/class.rb |
| 402 | 402 | + `branch_name` (required) - The name of branch |
| 403 | ++ `encoding` (optional) - 'text' or 'base64'. Text is default. | |
| 403 | 404 | + `content` (required) - File content |
| 404 | 405 | + `commit_message` (required) - Commit message |
| 405 | 406 | |
| ... | ... | @@ -413,6 +414,7 @@ Parameters: |
| 413 | 414 | |
| 414 | 415 | + `file_path` (required) - Full path to file. Ex. lib/class.rb |
| 415 | 416 | + `branch_name` (required) - The name of branch |
| 417 | ++ `encoding` (optional) - 'text' or 'base64'. Text is default. | |
| 416 | 418 | + `content` (required) - New file content |
| 417 | 419 | + `commit_message` (required) - Commit message |
| 418 | 420 | ... | ... |
lib/api/files.rb
| ... | ... | @@ -18,7 +18,7 @@ module API |
| 18 | 18 | # |
| 19 | 19 | post ":id/repository/files" do |
| 20 | 20 | required_attributes! [:file_path, :branch_name, :content, :commit_message] |
| 21 | - attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message] | |
| 21 | + attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] | |
| 22 | 22 | branch_name = attrs.delete(:branch_name) |
| 23 | 23 | file_path = attrs.delete(:file_path) |
| 24 | 24 | result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute |
| ... | ... | @@ -48,7 +48,7 @@ module API |
| 48 | 48 | # |
| 49 | 49 | put ":id/repository/files" do |
| 50 | 50 | required_attributes! [:file_path, :branch_name, :content, :commit_message] |
| 51 | - attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message] | |
| 51 | + attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] | |
| 52 | 52 | branch_name = attrs.delete(:branch_name) |
| 53 | 53 | file_path = attrs.delete(:file_path) |
| 54 | 54 | result = ::Files::UpdateContext.new(user_project, current_user, attrs, branch_name, file_path).execute | ... | ... |
lib/gitlab/satellite/files/edit_file_action.rb
| ... | ... | @@ -10,7 +10,7 @@ module Gitlab |
| 10 | 10 | # Returns false if committing the change fails |
| 11 | 11 | # Returns false if pushing from the satellite to bare repo failed or was rejected |
| 12 | 12 | # Returns true otherwise |
| 13 | - def commit!(content, commit_message) | |
| 13 | + def commit!(content, commit_message, encoding) | |
| 14 | 14 | in_locked_and_timed_satellite do |repo| |
| 15 | 15 | prepare_satellite!(repo) |
| 16 | 16 | |
| ... | ... | @@ -26,7 +26,8 @@ module Gitlab |
| 26 | 26 | return false |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | - File.open(file_path_in_satellite, 'w') { |f| f.write(content) } | |
| 29 | + # Write file | |
| 30 | + write_file(file_path_in_satellite, content, encoding) | |
| 30 | 31 | |
| 31 | 32 | # commit the changes |
| 32 | 33 | # will raise CommandFailed when commit fails | ... | ... |
lib/gitlab/satellite/files/file_action.rb
| ... | ... | @@ -12,6 +12,14 @@ module Gitlab |
| 12 | 12 | def safe_path?(path) |
| 13 | 13 | File.absolute_path(path) == path |
| 14 | 14 | end |
| 15 | + | |
| 16 | + def write_file(abs_file_path, content, file_encoding = 'text') | |
| 17 | + if file_encoding == 'base64' | |
| 18 | + File.open(abs_file_path, 'wb') { |f| f.write(Base64.decode64(content)) } | |
| 19 | + else | |
| 20 | + File.open(abs_file_path, 'w') { |f| f.write(content) } | |
| 21 | + end | |
| 22 | + end | |
| 15 | 23 | end |
| 16 | 24 | end |
| 17 | 25 | end | ... | ... |
lib/gitlab/satellite/files/new_file_action.rb
| ... | ... | @@ -9,7 +9,7 @@ module Gitlab |
| 9 | 9 | # Returns false if committing the change fails |
| 10 | 10 | # Returns false if pushing from the satellite to bare repo failed or was rejected |
| 11 | 11 | # Returns true otherwise |
| 12 | - def commit!(content, commit_message) | |
| 12 | + def commit!(content, commit_message, encoding) | |
| 13 | 13 | in_locked_and_timed_satellite do |repo| |
| 14 | 14 | prepare_satellite!(repo) |
| 15 | 15 | |
| ... | ... | @@ -29,7 +29,7 @@ module Gitlab |
| 29 | 29 | FileUtils.mkdir_p(dir_name_in_satellite) |
| 30 | 30 | |
| 31 | 31 | # Write file |
| 32 | - File.open(file_path_in_satellite, 'w') { |f| f.write(content) } | |
| 32 | + write_file(file_path_in_satellite, content, encoding) | |
| 33 | 33 | |
| 34 | 34 | # add new file |
| 35 | 35 | repo.add(file_path_in_satellite) | ... | ... |