Class: Gitolite::GitoliteAdmin

Inherits:
Object
  • Object
show all
Defined in:
lib/gitolite/gitolite_admin.rb

Constant Summary collapse

CONF =
"gitolite.conf"
CONFDIR =
"conf"
KEYDIR =
"keydir"
DEFAULT_COMMIT_MSG =

Gitolite gem’s default git commit message

"Committed by the gitolite gem"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ GitoliteAdmin

Intialize with the path to the gitolite-admin repository



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitolite/gitolite_admin.rb', line 14

def initialize(path, options = {})
  @path = path
  @gl_admin = Grit::Repo.new(path)

  @conf = options[:conf] || CONF
  @confdir = options[:confdir] || CONFDIR
  @keydir = options[:keydir] || KEYDIR

  # Load the ssh keys and the configuration
  load_data
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/gitolite/gitolite_admin.rb', line 3

def config
  @config
end

#gl_adminObject

Returns the value of attribute gl_admin.



3
4
5
# File 'lib/gitolite/gitolite_admin.rb', line 3

def gl_admin
  @gl_admin
end

#ssh_keysObject

Returns the value of attribute ssh_keys.



3
4
5
# File 'lib/gitolite/gitolite_admin.rb', line 3

def ssh_keys
  @ssh_keys
end

Class Method Details

.bootstrap(path, options = {}) ⇒ Object

This method will bootstrap a gitolite-admin repo at the given path. A typical gitolite-admin repo will have the following tree:

gitolite-admin

conf
  gitolite.conf
keydir


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
# File 'lib/gitolite/gitolite_admin.rb', line 34

def self.bootstrap(path, options = {})
  if self.is_gitolite_admin_repo?(path)
    if options[:overwrite]
      FileUtils.rm_rf(File.join(path, '*'))
    else
      return self.new(path)
    end
  end

  FileUtils.mkdir_p([File.join(path,"conf"), File.join(path,"keydir")])

  options[:perm] ||= "RW+"
  options[:refex] ||= ""
  options[:user] ||= "git"

  c = Config.init
  r = Config::Repo.new(options[:repo] || "gitolite-admin")
  r.add_permission(options[:perm], options[:refex], options[:user])
  c.add_repo(r)
  config = c.to_file(File.join(path, "conf"))

  repo = Grit::Repo.init(path)
  Dir.chdir(path) do
    repo.add(config)
    repo.commit_index(options[:message] || "Config bootstrapped by the gitolite gem")
  end

  self.new(path)
end

.is_gitolite_admin_repo?(dir) ⇒ Boolean

Checks to see if the given path is a gitolite-admin repository A valid repository contains a conf folder, keydir folder, and a configuration file within the conf folder

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gitolite/gitolite_admin.rb', line 146

def self.is_gitolite_admin_repo?(dir)
  # First check if it is a git repository
  begin
    Grit::Repo.new(dir)
  rescue Grit::InvalidGitRepositoryError
    return false
  end

  # If we got here it is a valid git repo,
  # now check directory structure
  File.exists?(File.join(dir, 'conf')) &&
    File.exists?(File.join(dir, 'keydir')) &&
    !Dir.glob(File.join(dir, 'conf', '*.conf')).empty?
end

Instance Method Details

#add_key(key) ⇒ Object



133
134
135
136
# File 'lib/gitolite/gitolite_admin.rb', line 133

def add_key(key)
  raise "Key must be of type Gitolite::SSHKey!" unless key.instance_of? Gitolite::SSHKey
  @ssh_keys[key.owner] << key
end

#apply(commit_message = DEFAULT_COMMIT_MSG) ⇒ Object

commits all staged changes and pushes back to origin

TODO: generate a better commit message TODO: add the ability to specify the remote and branch TODO: detect existance of origin instead of just dying



110
111
112
113
# File 'lib/gitolite/gitolite_admin.rb', line 110

def apply(commit_message = DEFAULT_COMMIT_MSG)
  @gl_admin.commit_index(commit_message)
  @gl_admin.git.push({}, "origin", "master")
end

#reload!Object

This method will destroy the in-memory data structures and reload everything from the file system



100
101
102
# File 'lib/gitolite/gitolite_admin.rb', line 100

def reload!
  load_data
end

#reset!Object

This method will destroy all local tracked changes, resetting the local gitolite git repo to HEAD and reloading the entire repository Note that this will also delete all untracked files



90
91
92
93
94
95
96
# File 'lib/gitolite/gitolite_admin.rb', line 90

def reset!
  Dir.chdir(@gl_admin.working_dir) do
    @gl_admin.git.reset({:hard => true}, 'HEAD')
    @gl_admin.git.clean({:d => true, :q => true, :f => true})
  end
  reload!
end

#rm_key(key) ⇒ Object



138
139
140
141
# File 'lib/gitolite/gitolite_admin.rb', line 138

def rm_key(key)
  raise "Key must be of type Gitolite::SSHKey!" unless key.instance_of? Gitolite::SSHKey
  @ssh_keys[key.owner].delete key
end

#saveObject

Writes all aspects out to the file system will also stage all changes



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gitolite/gitolite_admin.rb', line 66

def save
  Dir.chdir(@gl_admin.working_dir) do
    #Process config file
    new_conf = @config.to_file(@confdir)
    @gl_admin.add(new_conf)

    #Process ssh keys
    files = list_keys(@keydir).map{|f| File.basename f}
    keys = @ssh_keys.values.map{|f| f.map {|t| t.filename}}.flatten

    to_remove = (files - keys).map { |f| File.join(@keydir, f)}
    @gl_admin.remove(to_remove)

    @ssh_keys.each_value do |key|
      key.each do |k|
        @gl_admin.add(k.to_file(@keydir))
      end
    end
  end
end

#save_and_apply(commit_message = DEFAULT_COMMIT_MSG) ⇒ Object



115
116
117
118
# File 'lib/gitolite/gitolite_admin.rb', line 115

def save_and_apply(commit_message = DEFAULT_COMMIT_MSG)
  self.save
  self.apply(commit_message)
end

#update(options = {}) ⇒ Object

Updates the repo with changes from remote master



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gitolite/gitolite_admin.rb', line 121

def update(options = {})
  options = {:reset => true, :rebase => false }.merge(options)

  reset! if options[:reset]

  Dir.chdir(@gl_admin.working_dir) do
    @gl_admin.git.pull({:rebase => options[:rebase]}, "origin", "master")
  end

  reload!
end