summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-08-20 12:19:19 -0700
committerDouwe Maan <douwe@gitlab.com>2015-08-20 12:19:19 -0700
commit3d51a6d4351c6a15c92762f5710a967374d0c59b (patch)
tree10c5e87e0add51ef0bdcc9be42ac0176907a080c /app/models
parent8ec5fb138dde9937814ac138352177399d3e776d (diff)
parent8819007c83fdf1ac642836640a37cc541f6eddc6 (diff)
downloadgitlab-ce-3d51a6d4351c6a15c92762f5710a967374d0c59b.tar.gz
Merge branch 'master' into reply-by-email
Diffstat (limited to 'app/models')
-rw-r--r--app/models/application_setting.rb15
-rw-r--r--app/models/merge_request.rb55
-rw-r--r--app/models/merge_request_diff.rb32
-rw-r--r--app/models/namespace.rb5
-rw-r--r--app/models/project.rb16
-rw-r--r--app/models/project_services/gitlab_ci_service.rb4
-rw-r--r--app/models/repository.rb146
7 files changed, 223 insertions, 50 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
index 6d1ad82a262..8f27e35d723 100644
--- a/app/models/application_setting.rb
+++ b/app/models/application_setting.rb
@@ -22,10 +22,12 @@
# user_oauth_applications :boolean default(TRUE)
# after_sign_out_path :string(255)
# session_expire_delay :integer default(10080), not null
+# import_sources :text
#
class ApplicationSetting < ActiveRecord::Base
serialize :restricted_visibility_levels
+ serialize :import_sources
serialize :restricted_signup_domains, Array
attr_accessor :restricted_signup_domains_raw
@@ -52,6 +54,16 @@ class ApplicationSetting < ActiveRecord::Base
end
end
+ validates_each :import_sources do |record, attr, value|
+ unless value.nil?
+ value.each do |source|
+ unless Gitlab::ImportSources.options.has_value?(source)
+ record.errors.add(attr, "'#{source}' is not a import source")
+ end
+ end
+ end
+ end
+
def self.current
ApplicationSetting.last
end
@@ -70,7 +82,8 @@ class ApplicationSetting < ActiveRecord::Base
session_expire_delay: Settings.gitlab['session_expire_delay'],
default_project_visibility: Settings.gitlab.default_projects_features['visibility_level'],
default_snippet_visibility: Settings.gitlab.default_projects_features['visibility_level'],
- restricted_signup_domains: Settings.gitlab['restricted_signup_domains']
+ restricted_signup_domains: Settings.gitlab['restricted_signup_domains'],
+ import_sources: ['github','bitbucket','gitlab','gitorious','google_code','git']
)
end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 324d1795ab4..467b90861f9 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -41,8 +41,6 @@ class MergeRequest < ActiveRecord::Base
delegate :commits, :diffs, :last_commit, :last_commit_short_sha, to: :merge_request_diff, prefix: nil
- attr_accessor :should_remove_source_branch
-
# When this attribute is true some MR validation is ignored
# It allows us to close or modify broken merge requests
attr_accessor :allow_broken
@@ -57,7 +55,7 @@ class MergeRequest < ActiveRecord::Base
transition [:reopened, :opened] => :closed
end
- event :merge do
+ event :mark_as_merged do
transition [:reopened, :opened, :locked] => :merged
end
@@ -205,7 +203,10 @@ class MergeRequest < ActiveRecord::Base
end
def check_if_can_be_merged
- if Gitlab::Satellite::MergeAction.new(self.author, self).can_be_merged?
+ can_be_merged =
+ project.repository.can_be_merged?(source_sha, target_branch)
+
+ if can_be_merged
mark_as_mergeable
else
mark_as_unmergeable
@@ -220,18 +221,6 @@ class MergeRequest < ActiveRecord::Base
self.target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::CLOSED).last
end
- def automerge!(current_user, commit_message = nil)
- return unless automergeable?
-
- MergeRequests::AutoMergeService.
- new(target_project, current_user).
- execute(self, commit_message)
- end
-
- def remove_source_branch?
- self.should_remove_source_branch && !self.source_project.root_ref?(self.source_branch) && !self.for_fork?
- end
-
def open?
opened? || reopened?
end
@@ -240,11 +229,11 @@ class MergeRequest < ActiveRecord::Base
title =~ /\A\[?WIP\]?:? /i
end
- def automergeable?
+ def mergeable?
open? && !work_in_progress? && can_be_merged?
end
- def automerge_status
+ def gitlab_merge_status
if work_in_progress?
"work_in_progress"
else
@@ -271,14 +260,14 @@ class MergeRequest < ActiveRecord::Base
#
# see "git diff"
def to_diff(current_user)
- Gitlab::Satellite::MergeAction.new(current_user, self).diff_in_satellite
+ target_project.repository.diff_text(target_branch, source_sha)
end
# Returns the commit as a series of email patches.
#
# see "git format-patch"
def to_patch(current_user)
- Gitlab::Satellite::MergeAction.new(current_user, self).format_patch
+ target_project.repository.format_patch(target_branch, source_sha)
end
def hook_attrs
@@ -429,4 +418,30 @@ class MergeRequest < ActiveRecord::Base
"Open"
end
end
+
+ def target_sha
+ @target_sha ||= target_project.
+ repository.commit(target_branch).sha
+ end
+
+ def source_sha
+ commits.first.sha
+ end
+
+ def fetch_ref
+ target_project.repository.fetch_ref(
+ source_project.repository.path_to_repo,
+ "refs/heads/#{source_branch}",
+ "refs/merge-requests/#{iid}/head"
+ )
+ end
+
+ def in_locked_state
+ begin
+ lock_mr
+ yield
+ ensure
+ unlock_mr if locked?
+ end
+ end
end
diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb
index df1c2b78758..e317c8eac4d 100644
--- a/app/models/merge_request_diff.rb
+++ b/app/models/merge_request_diff.rb
@@ -16,9 +16,8 @@ require Rails.root.join("app/models/commit")
class MergeRequestDiff < ActiveRecord::Base
include Sortable
- # Prevent store of diff
- # if commits amount more then 200
- COMMITS_SAFE_SIZE = 200
+ # Prevent store of diff if commits amount more then 500
+ COMMITS_SAFE_SIZE = 500
attr_reader :commits, :diffs
@@ -124,12 +123,12 @@ class MergeRequestDiff < ActiveRecord::Base
if new_diffs.any?
if new_diffs.size > Commit::DIFF_HARD_LIMIT_FILES
self.state = :overflow_diff_files_limit
- new_diffs = []
+ new_diffs = new_diffs.first[Commit::DIFF_HARD_LIMIT_LINES]
end
if new_diffs.sum { |diff| diff.diff.lines.count } > Commit::DIFF_HARD_LIMIT_LINES
self.state = :overflow_diff_lines_limit
- new_diffs = []
+ new_diffs = new_diffs.first[Commit::DIFF_HARD_LIMIT_LINES]
end
end
@@ -160,12 +159,21 @@ class MergeRequestDiff < ActiveRecord::Base
private
def compare_result
- @compare_result ||= CompareService.new.execute(
- merge_request.author,
- merge_request.source_project,
- merge_request.source_branch,
- merge_request.target_project,
- merge_request.target_branch,
- )
+ @compare_result ||=
+ begin
+ # Update ref for merge request
+ merge_request.fetch_ref
+
+ # Get latest sha of branch from source project
+ source_sha = merge_request.source_project.commit(source_branch).sha
+
+ Gitlab::CompareResult.new(
+ Gitlab::Git::Compare.new(
+ merge_request.target_project.repository.raw_repository,
+ merge_request.target_branch,
+ source_sha,
+ )
+ )
+ end
end
end
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 30ffacadded..161a16ca61c 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -118,12 +118,11 @@ class Namespace < ActiveRecord::Base
gitlab_shell.add_namespace(path_was)
if gitlab_shell.mv_namespace(path_was, path)
- # If repositories moved successfully we need to remove old satellites
- # and send update instructions to users.
+ # If repositories moved successfully we need to
+ # send update instructions to users.
# However we cannot allow rollback since we moved namespace dir
# So we basically we mute exceptions in next actions
begin
- gitlab_shell.rm_satellites(path_was)
send_update_instructions
rescue
# Returning false does not rollback after_* transaction but gives
diff --git a/app/models/project.rb b/app/models/project.rb
index 3dc1729e812..69f9af91c51 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -215,7 +215,7 @@ class Project < ActiveRecord::Base
end
def search(query)
- joins(:namespace).where('projects.archived = ?', false).
+ joins(:namespace).
where('LOWER(projects.name) LIKE :query OR
LOWER(projects.path) LIKE :query OR
LOWER(namespaces.name) LIKE :query OR
@@ -520,14 +520,6 @@ class Project < ActiveRecord::Base
!repository.exists? || repository.empty?
end
- def ensure_satellite_exists
- self.satellite.create unless self.satellite.exists?
- end
-
- def satellite
- @satellite ||= Gitlab::Satellite::Satellite.new(self)
- end
-
def repo
repository.raw
end
@@ -597,14 +589,11 @@ class Project < ActiveRecord::Base
new_path_with_namespace = File.join(namespace_dir, path)
if gitlab_shell.mv_repository(old_path_with_namespace, new_path_with_namespace)
- # If repository moved successfully we need to remove old satellite
- # and send update instructions to users.
+ # If repository moved successfully we need to send update instructions to users.
# However we cannot allow rollback since we moved repository
# So we basically we mute exceptions in next actions
begin
gitlab_shell.mv_repository("#{old_path_with_namespace}.wiki", "#{new_path_with_namespace}.wiki")
- gitlab_shell.rm_satellites(old_path_with_namespace)
- ensure_satellite_exists
send_move_instructions
reset_events_cache
rescue
@@ -702,7 +691,6 @@ class Project < ActiveRecord::Base
def create_repository
if forked?
if gitlab_shell.fork_repository(forked_from_project.path_with_namespace, self.namespace.path)
- ensure_satellite_exists
true
else
errors.add(:base, 'Failed to fork repository via gitlab-shell')
diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb
index 5aaa4e85cbc..ecdcd48ae60 100644
--- a/app/models/project_services/gitlab_ci_service.rb
+++ b/app/models/project_services/gitlab_ci_service.rb
@@ -74,6 +74,8 @@ class GitlabCiService < CiService
else
:error
end
+ rescue Errno::ECONNREFUSED
+ :error
end
def fork_registration(new_project, private_token)
@@ -103,6 +105,8 @@ class GitlabCiService < CiService
if response.code == 200 and response["coverage"]
response["coverage"]
end
+ rescue Errno::ECONNREFUSED
+ nil
end
def build_page(sha, ref)
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 24c32d90051..79b48ebfedf 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -1,4 +1,9 @@
+require 'securerandom'
+
class Repository
+ class PreReceiveError < StandardError; end
+ class CommitError < StandardError; end
+
include Gitlab::ShellAdapter
attr_accessor :raw_repository, :path_with_namespace, :project
@@ -368,6 +373,89 @@ class Repository
@root_ref ||= raw_repository.root_ref
end
+ def commit_file(user, path, content, message, branch)
+ commit_with_hooks(user, branch) do |ref|
+ path[0] = '' if path[0] == '/'
+
+ committer = user_to_comitter(user)
+ options = {}
+ options[:committer] = committer
+ options[:author] = committer
+ options[:commit] = {
+ message: message,
+ branch: ref,
+ }
+
+ options[:file] = {
+ content: content,
+ path: path
+ }
+
+ Gitlab::Git::Blob.commit(raw_repository, options)
+ end
+ end
+
+ def remove_file(user, path, message, branch)
+ commit_with_hooks(user, branch) do |ref|
+ path[0] = '' if path[0] == '/'
+
+ committer = user_to_comitter(user)
+ options = {}
+ options[:committer] = committer
+ options[:author] = committer
+ options[:commit] = {
+ message: message,
+ branch: ref
+ }
+
+ options[:file] = {
+ path: path
+ }
+
+ Gitlab::Git::Blob.remove(raw_repository, options)
+ end
+ end
+
+ def user_to_comitter(user)
+ {
+ email: user.email,
+ name: user.name,
+ time: Time.now
+ }
+ end
+
+ def can_be_merged?(source_sha, target_branch)
+ our_commit = rugged.branches[target_branch].target
+ their_commit = rugged.lookup(source_sha)
+
+ if our_commit && their_commit
+ !rugged.merge_commits(our_commit, their_commit).conflicts?
+ else
+ false
+ end
+ end
+
+ def merge(user, source_sha, target_branch, options = {})
+ our_commit = rugged.branches[target_branch].target
+ their_commit = rugged.lookup(source_sha)
+
+ raise "Invalid merge target" if our_commit.nil?
+ raise "Invalid merge source" if their_commit.nil?
+
+ merge_index = rugged.merge_commits(our_commit, their_commit)
+ return false if merge_index.conflicts?
+
+ commit_with_hooks(user, target_branch) do |ref|
+ actual_options = options.merge(
+ parents: [our_commit, their_commit],
+ tree: merge_index.write_tree(rugged),
+ update_ref: ref
+ )
+
+ Rugged::Commit.create(rugged, actual_options)
+ end
+ end
+
def merged_to_root_ref?(branch_name)
branch_commit = commit(branch_name)
root_ref_commit = commit(root_ref)
@@ -412,6 +500,64 @@ class Repository
)
end
+ def fetch_ref(source_path, source_ref, target_ref)
+ args = %W(git fetch #{source_path} #{source_ref}:#{target_ref})
+ Gitlab::Popen.popen(args, path_to_repo)
+ end
+
+ def commit_with_hooks(current_user, branch)
+ oldrev = Gitlab::Git::BLANK_SHA
+ ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
+ gl_id = Gitlab::ShellEnv.gl_id(current_user)
+ was_empty = empty?
+
+ # Create temporary ref
+ random_string = SecureRandom.hex
+ tmp_ref = "refs/tmp/#{random_string}/head"
+
+ unless was_empty
+ oldrev = find_branch(branch).target
+ rugged.references.create(tmp_ref, oldrev)
+ end
+
+ # Make commit in tmp ref
+ newrev = yield(tmp_ref)
+
+ unless newrev
+ raise CommitError.new('Failed to create commit')
+ end
+
+ # Run GitLab pre-receive hook
+ pre_receive_hook = Gitlab::Git::Hook.new('pre-receive', path_to_repo)
+ status = pre_receive_hook.trigger(gl_id, oldrev, newrev, ref)
+
+ if status
+ if was_empty
+ # Create branch
+ rugged.references.create(ref, newrev)
+ else
+ # Update head
+ current_head = find_branch(branch).target
+
+ # Make sure target branch was not changed during pre-receive hook
+ if current_head == oldrev
+ rugged.references.update(ref, newrev)
+ else
+ raise CommitError.new('Commit was rejected because branch received new push')
+ end
+ end
+
+ # Run GitLab post receive hook
+ post_receive_hook = Gitlab::Git::Hook.new('post-receive', path_to_repo)
+ status = post_receive_hook.trigger(gl_id, oldrev, newrev, ref)
+ else
+ # Remove tmp ref and return error to user
+ rugged.references.delete(tmp_ref)
+
+ raise PreReceiveError.new('Commit was rejected by pre-receive hook')
+ end
+ end
+
private
def cache