Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-11-05 12:28:49 +0400
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-11-05 12:28:49 +0400
commitbd2b6f59445919cdcef627f7f1b1fca5d402168b (patch)
tree78ed9786fda049f1fe60a5a220103c963892651d /lib
parent2ee04b120a142ee227cb860ffddb991ef31a6910 (diff)
New feature: Create file from UI
Now you are able to create a new file in repository from your browser. You are not allowed to create a file if file with same name already exists in the repo. Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/satellite/files/edit_file_action.rb (renamed from lib/gitlab/satellite/edit_file_action.rb)19
-rw-r--r--lib/gitlab/satellite/files/file_action.rb20
-rw-r--r--lib/gitlab/satellite/files/new_file_action.rb44
3 files changed, 67 insertions, 16 deletions
diff --git a/lib/gitlab/satellite/edit_file_action.rb b/lib/gitlab/satellite/files/edit_file_action.rb
index d793d0ba8dc..72e12fb077c 100644
--- a/lib/gitlab/satellite/edit_file_action.rb
+++ b/lib/gitlab/satellite/files/edit_file_action.rb
@@ -1,15 +1,9 @@
+require_relative 'file_action'
+
module Gitlab
module Satellite
# GitLab server-side file update and commit
- class EditFileAction < Action
- attr_accessor :file_path, :ref
-
- def initialize(user, project, ref, file_path)
- super user, project, git_timeout: 10.seconds
- @file_path = file_path
- @ref = ref
- end
-
+ class EditFileAction < FileAction
# Updates the files content and creates a new commit for it
#
# Returns false if the ref has been updated while editing the file
@@ -45,13 +39,6 @@ module Gitlab
Gitlab::GitLogger.error(ex.message)
false
end
-
- protected
-
- def can_edit?(last_commit)
- current_last_commit = Gitlab::Git::Commit.last_for_path(@project.repository, ref, file_path).sha
- last_commit == current_last_commit
- end
end
end
end
diff --git a/lib/gitlab/satellite/files/file_action.rb b/lib/gitlab/satellite/files/file_action.rb
new file mode 100644
index 00000000000..4ac53c2cd5a
--- /dev/null
+++ b/lib/gitlab/satellite/files/file_action.rb
@@ -0,0 +1,20 @@
+module Gitlab
+ module Satellite
+ class FileAction < Action
+ attr_accessor :file_path, :ref
+
+ def initialize(user, project, ref, file_path)
+ super user, project, git_timeout: 10.seconds
+ @file_path = file_path
+ @ref = ref
+ end
+
+ protected
+
+ def can_edit?(last_commit)
+ current_last_commit = Gitlab::Git::Commit.last_for_path(@project.repository, ref, file_path).sha
+ last_commit == current_last_commit
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb
new file mode 100644
index 00000000000..9fe5a38eb80
--- /dev/null
+++ b/lib/gitlab/satellite/files/new_file_action.rb
@@ -0,0 +1,44 @@
+require_relative 'file_action'
+
+module Gitlab
+ module Satellite
+ class NewFileAction < FileAction
+ # Updates the files content and creates a new commit for it
+ #
+ # Returns false if the ref has been updated while editing the file
+ # Returns false if committing the change fails
+ # Returns false if pushing from the satellite to Gitolite failed or was rejected
+ # Returns true otherwise
+ def commit!(content, commit_message, file_name)
+ in_locked_and_timed_satellite do |repo|
+ prepare_satellite!(repo)
+
+ # create target branch in satellite at the corresponding commit from Gitolite
+ repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}")
+
+ # update the file in the satellite's working dir
+ file_path_in_satellite = File.join(repo.working_dir, file_path, file_name)
+ File.open(file_path_in_satellite, 'w') { |f| f.write(content) }
+
+ # add new file
+ repo.add(file_path_in_satellite)
+
+ # commit the changes
+ # will raise CommandFailed when commit fails
+ repo.git.commit(raise: true, timeout: true, a: true, m: commit_message)
+
+
+ # push commit back to Gitolite
+ # will raise CommandFailed when push fails
+ repo.git.push({raise: true, timeout: true}, :origin, ref)
+
+ # everything worked
+ true
+ end
+ rescue Grit::Git::CommandFailed => ex
+ Gitlab::GitLogger.error(ex.message)
+ false
+ end
+ end
+ end
+end