summaryrefslogtreecommitdiff
path: root/lib/gitosis.rb
blob: 54d885011a1b0f7b433464de0eb8d3eb603c4522 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'inifile'
require 'timeout'
require 'fileutils'

class Gitosis
  class AccessDenied < StandardError; end

  def pull
    # create tmp dir
    @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}")

    Dir.mkdir @local_dir

    @repo = Git.clone(GITOSIS['admin_uri'], "#{@local_dir}/gitosis")
  end

  def push
    @repo.add('.')
    @repo.commit_all "Gitlab"
    @repo.push

    #FileUtils.rm_rf(@local_dir)
  end

  def configure
    status = Timeout::timeout(5) do
      File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f|
        f.flock(File::LOCK_EX)

        pull
        yield(self)
        push

        f.flock(File::LOCK_UN)
      end
    end
  rescue Exception => ex
    raise Gitosis::AccessDenied.new("gitosis timeout")
  end

  def destroy_project(project)
    #FileUtils.rm_rf(project.path_to_repo)
    
    conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))

    conf.delete_section("group #{project.path}")

    conf.write
  end

   #update or create
  def update_keys(user, key)
    File.open(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
  end

  def delete_key(user)
    File.unlink(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"))
    `cd #{File.join(@local_dir,'gitosis')} ; git rm keydir/#{user}.pub`
  end

  #update or create
  def update_project(repo_name, name_writers)
    # write config file
    conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))

    conf["group #{repo_name}"]['writable'] = repo_name
    conf["group #{repo_name}"]['members'] = name_writers.join(' ')

    conf.write
  end
end