Commit 511d07c47c9bf3a18bfa276d452c899369432a22
Exists in
master
and in
4 other branches
Merge pull request #713 from gitlabhq/fast_automerge
Fast automerge
Showing
5 changed files
with
67 additions
and
11 deletions
Show diff stats
app/models/project/hooks_trait.rb
app/models/project/repository_trait.rb
lib/gitlab_merge.rb
| 1 | 1 | class GitlabMerge |
| 2 | - attr_accessor :project, :merge_path, :merge_request, :user | |
| 2 | + attr_accessor :project, :merge_request, :user | |
| 3 | 3 | |
| 4 | 4 | def initialize(merge_request, user) |
| 5 | 5 | self.user = user |
| 6 | 6 | self.merge_request = merge_request |
| 7 | 7 | self.project = merge_request.project |
| 8 | - self.merge_path = File.join(Rails.root, "tmp", "merge_repo", project.path, merge_request.id.to_s) | |
| 9 | - FileUtils.rm_rf(merge_path) | |
| 10 | - FileUtils.mkdir_p merge_path | |
| 11 | 8 | end |
| 12 | 9 | |
| 13 | 10 | def can_be_merged? |
| 14 | - pull do |repo, output| | |
| 11 | + process do |repo, output| | |
| 15 | 12 | !(output =~ /Automatic merge failed/) |
| 16 | 13 | end |
| 17 | 14 | end |
| 18 | 15 | |
| 19 | 16 | def merge |
| 20 | - pull do |repo, output| | |
| 17 | + process do |repo, output| | |
| 21 | 18 | if output =~ /Automatic merge failed/ |
| 22 | 19 | false |
| 23 | 20 | else |
| ... | ... | @@ -27,19 +24,23 @@ class GitlabMerge |
| 27 | 24 | end |
| 28 | 25 | end |
| 29 | 26 | |
| 30 | - def pull | |
| 27 | + def process | |
| 31 | 28 | Grit::Git.with_timeout(30.seconds) do |
| 32 | 29 | File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f| |
| 33 | 30 | f.flock(File::LOCK_EX) |
| 34 | 31 | |
| 35 | - self.project.repo.git.clone({:branch => merge_request.target_branch}, project.url_to_repo, merge_path) | |
| 36 | - unless File.exist?(self.merge_path) | |
| 37 | - raise "Gitlab user do not have access to repo. You should run: rake gitlab_enable_automerge" | |
| 32 | + unless project.satellite.exists? | |
| 33 | + raise "You should run: rake gitlab_enable_automerge" | |
| 38 | 34 | end |
| 39 | - Dir.chdir(merge_path) do | |
| 35 | + | |
| 36 | + project.satellite.clear | |
| 37 | + | |
| 38 | + Dir.chdir(project.satellite.path) do | |
| 40 | 39 | merge_repo = Grit::Repo.new('.') |
| 40 | + merge_repo.git.sh "git fetch origin" | |
| 41 | 41 | merge_repo.git.sh "git config user.name \"#{user.name}\"" |
| 42 | 42 | merge_repo.git.sh "git config user.email \"#{user.email}\"" |
| 43 | + merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}" | |
| 43 | 44 | output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch) |
| 44 | 45 | yield(merge_repo, output) |
| 45 | 46 | end | ... | ... |
| ... | ... | @@ -0,0 +1,41 @@ |
| 1 | +module Gitlabhq | |
| 2 | + class Satellite | |
| 3 | + | |
| 4 | + PARKING_BRANCH = "__parking_branch" | |
| 5 | + | |
| 6 | + attr_accessor :project | |
| 7 | + | |
| 8 | + def initialize project | |
| 9 | + self.project = project | |
| 10 | + end | |
| 11 | + | |
| 12 | + def create | |
| 13 | + `git clone #{project.url_to_repo} #{path}` | |
| 14 | + end | |
| 15 | + | |
| 16 | + def path | |
| 17 | + File.join(Rails.root, "tmp", "repo_satellites", project.path) | |
| 18 | + end | |
| 19 | + | |
| 20 | + def exists? | |
| 21 | + File.exists? path | |
| 22 | + end | |
| 23 | + | |
| 24 | + #will be deleted all branches except PARKING_BRANCH | |
| 25 | + def clear | |
| 26 | + Dir.chdir(path) do | |
| 27 | + heads = Grit::Repo.new(".").heads.map{|head| head.name} | |
| 28 | + if heads.include? PARKING_BRANCH | |
| 29 | + `git checkout #{PARKING_BRANCH}` | |
| 30 | + else | |
| 31 | + `git checkout -b #{PARKING_BRANCH}` | |
| 32 | + end | |
| 33 | + heads.delete(PARKING_BRANCH) | |
| 34 | + heads.each do |head| | |
| 35 | + `git branch -D #{head}` | |
| 36 | + end | |
| 37 | + end | |
| 38 | + end | |
| 39 | + | |
| 40 | + end | |
| 41 | +end | ... | ... |
lib/tasks/gitlab/enable_automerge.rake
| ... | ... | @@ -6,6 +6,13 @@ namespace :gitlab do |
| 6 | 6 | git.admin_all_repo |
| 7 | 7 | end |
| 8 | 8 | |
| 9 | + Project.find_each do |project| | |
| 10 | + if project.repo_exists? && !project.satellite.exists? | |
| 11 | + puts "Creating satellite for #{project.name}...".green | |
| 12 | + project.satellite.create | |
| 13 | + end | |
| 14 | + end | |
| 15 | + | |
| 9 | 16 | puts "Done!".green |
| 10 | 17 | end |
| 11 | 18 | end | ... | ... |