summaryrefslogtreecommitdiffstats
path: root/lib/git_backend.rb
blob: 5692922d0747c198f290b163c700627986026316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require "fileutils"

class GitBackend
  class << self
    # Creates a new bare Git repository at +path+
    # sets git-daemon-export-ok if +set_export_ok+ is true (default)
    def create(repos_path, set_export_ok = true)
      FileUtils.mkdir_p(repos_path, :mode => 0750)
      Dir.chdir(repos_path) do |path| 
        template = File.expand_path(File.join(File.dirname(__FILE__), "../data/git-template"))
        git = Grit::Git.new(path)
        git.init(:template => template)
        post_create(path) if set_export_ok
      end
    end
    
    # Clones a new bare Git repository at +target-path+ from +source_path+
    # sets git-daemon-export-ok if +set_export_ok+ is true (default)
    def clone(target_path, source_path, set_export_ok = true)
      parent_path = File.expand_path(File.join(target_path, ".."))
      FileUtils.mkdir_p(parent_path, :mode => 0750)
      template = File.expand_path(File.join(File.dirname(__FILE__), "../data/git-template"))
      git = Grit::Git.new(target_path)
      git.clone({:bare => true, :template => template}, source_path, target_path)
      post_create(target_path) if set_export_ok
    end
    
    def delete!(repos_path)
      if repos_path.index(GitoriousConfig["repository_base_path"]) == 0
        FileUtils.rm_rf(repos_path)
      else
        raise "bad path"
      end
    end
    
    def repository_has_commits?(repos_path)
      Dir[File.join(repos_path, "refs/heads/*")].size > 0
    end
    
    protected
      def post_create(path)
        FileUtils.touch(File.join(path, "git-daemon-export-ok"))
        execute_command(%Q{GIT_DIR="#{path}" git update-server-info})
      end
      
      def execute_command(command)
        system(command)
      end
  end
end