OSDN Git Service

fast automerge: done
authorValery Sizov <vsv2711@gmail.com>
Sun, 22 Apr 2012 13:14:01 +0000 (16:14 +0300)
committerValery Sizov <vsv2711@gmail.com>
Sun, 22 Apr 2012 13:14:01 +0000 (16:14 +0300)
app/models/project/hooks_trait.rb
app/models/project/repository_trait.rb
lib/gitlab_merge.rb
lib/gitlabhq/satellite.rb [new file with mode: 0644]
lib/tasks/gitlab/enable_automerge.rake

index c290ae6..2f97eb6 100644 (file)
@@ -102,8 +102,8 @@ module Project::HooksTrait
       # Execute web hooks
       self.execute_web_hooks(oldrev, newrev, ref, user)
 
-      # Create repo satellite
-      self.create_repo_satellite unless self.satellite_exists?
+      # Create satellite
+      self.satellite.create unless self.satellite.exists?
     end
   end
 end
index ee305a0..d8bdbf2 100644 (file)
@@ -37,21 +37,8 @@ module Project::RepositoryTrait
       end
     end
 
-    def path_to_repo_satellite
-      File.join(Rails.root, "tmp", "repo_satellites", self.path)
-    end
-
-    def satellite_exists?
-      File.exist? path_to_repo_satellite
-    end
-
-    def create_repo_satellite
-      `git clone #{url_to_repo} #{path_to_repo_satellite}`
-      Dir.chdir(path_to_repo_satellite) do
-        primary_branch = Grit::Repo.new(".").heads.first.name #usually it`s master
-        `git checkout -b __parking_branch`
-        `git branch -D #{primary_branch}`
-      end
+    def satellite
+      @satellite ||= Gitlabhq::Satellite.new(self)
     end
 
     def write_hook(name, content)
index 689d0e7..98772bf 100644 (file)
@@ -29,10 +29,13 @@ class GitlabMerge
       File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f|
         f.flock(File::LOCK_EX)
         
-        unless project.satellite_exists?
+        unless project.satellite.exists?
           raise "You should run: rake gitlab_enable_automerge"
         end
-        Dir.chdir(project.path_to_repo_satellite) do
+
+        project.satellite.clear
+
+        Dir.chdir(project.satellite.path) do
           merge_repo = Grit::Repo.new('.')
           merge_repo.git.sh "git fetch origin"
           merge_repo.git.sh "git config user.name \"#{user.name}\""
diff --git a/lib/gitlabhq/satellite.rb b/lib/gitlabhq/satellite.rb
new file mode 100644 (file)
index 0000000..cd713a6
--- /dev/null
@@ -0,0 +1,41 @@
+module Gitlabhq
+  class Satellite
+    
+    PARKING_BRANCH = "__parking_branch"
+
+    attr_accessor :project
+
+    def initialize project
+      self.project = project
+    end
+
+    def create
+      `git clone #{project.url_to_repo} #{path}`
+    end
+
+    def path
+      File.join(Rails.root, "tmp", "repo_satellites", project.path)
+    end
+
+    def exists?
+      File.exists? path
+    end
+
+    #will be deleted all branches except PARKING_BRANCH
+    def clear
+      Dir.chdir(path) do
+        heads = Grit::Repo.new(".").heads.map{|head| head.name}
+        if heads.include? PARKING_BRANCH
+          `git checkout #{PARKING_BRANCH}`
+        else
+          `git checkout -b #{PARKING_BRANCH}`
+        end
+        heads.delete(PARKING_BRANCH)
+        heads.each do |head|
+          `git branch -D #{head}`
+        end
+      end
+    end
+    
+  end
+end
index 5eebd63..647e06f 100644 (file)
@@ -7,9 +7,9 @@ namespace :gitlab do
       end
 
       Project.find_each do |project|
-        if project.repo_exists? && !project.satellite_exists?
+        if project.repo_exists? && !project.satellite.exists?
           puts "Creating satellite for #{project.name}...".green
-          project.create_repo_satellite
+          project.satellite.create
         end
       end