Class: Git::Gitosis

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitosis

Returns a new instance of Gitosis.



69
70
71
72
73
74
75
76
77
# File 'lib/it_tools/git.rb', line 69

def initialize
  @ops = {}
  @log = Logger.new 'log.txt'
  if level = @ops[:debug_level]
    @log.level = level
  else
    @log.level = Logger::INFO
  end
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



68
69
70
# File 'lib/it_tools/git.rb', line 68

def log
  @log
end

#opsObject

Returns the value of attribute ops.



68
69
70
# File 'lib/it_tools/git.rb', line 68

def ops
  @ops
end

Instance Method Details

#add_project_to_gitosis(project_name, to_group, gitosis_read, gitosis_write) ⇒ Object

Add a project to the gitosis security conf file

  • Args :

    • project_folder -> This is the folder where the new project that you want to add to gitosis is located

    • to_group -> This is the name of the group that you want to add this project to

    • gitosis_read -> This is location of the gitosis file you are reading from

    • gitosis_write -> This is location of the gitosis file you are writing to

  • Returns : -



87
88
89
90
91
92
93
94
95
96
# File 'lib/it_tools/git.rb', line 87

def add_project_to_gitosis project_name, to_group, gitosis_read, gitosis_write
  groups = consume_file gitosis_read
  group = groups[to_group]
  raise "Couldn't find group: #{to_group}, in file: #{gitosis_read}." if group.nil? 
  writable = group['writable'].strip
  writable += " " + project_name + "\n"
  group['writable'] = writable
  groups[to_group] = group
  write_gitosis_file gitosis_write, groups
end

#consume_file(gitosis_file = nil) ⇒ Object

  • Args :

    • ++ ->

  • Returns : -



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/it_tools/git.rb', line 119

def consume_file (gitosis_file = nil)
  @log.debug "Will try to read file: " + File.absolute_path(gitosis_file)
  groups = {}
  File.open(gitosis_file, "r") do |infile|
    while (line = infile.gets)
      if /^\[group (.*)\]/ =~ line
        group_name = $1
        writable = infile.gets
        members = infile.gets
        group_data = { 'members' => members, 'writable' => writable }
        groups[group_name] = group_data
      end
    end
  end
  return groups
end

#match_line(regex, candidate) ⇒ Object

  • Args :

    • regex -> A regular expression. Should be surrounded by

    forward slashes ‘/’, for example: /fe(..)on/

    • candidate -> This is the string for which you want to test to

    see if the regular expression is in it. For example: fenton

  • Returns :

    • nil if the regex is not found in the candidate string, otherwise

    it returns the found regex, in the above example it would return the string nt



163
164
165
166
167
168
# File 'lib/it_tools/git.rb', line 163

def match_line regex, candidate
  if regex =~ candidate
    return $1
  end
  return nil
end

#remove_project_from_gitosis(project_name, from_group, gitosis_read, gitosis_write) ⇒ Object

  • Args :

    • ++ ->

  • Returns : -



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/it_tools/git.rb', line 102

def remove_project_from_gitosis project_name, from_group, gitosis_read, gitosis_write
  groups = consume_file gitosis_read
  group = groups[from_group]
  raise "Couldn't find group: #{to_group}, in file: #{gitosis_read}." if group.nil? 
  writable = group['writable'].split " "
  writable.delete_at(writable.index(project_name) || li.length)
  group['writable'] = writable.join " "
  group['writable'] += "\n"
  groups[from_group] = group
  write_gitosis_file gitosis_write, groups
end

#write_gitosis_file(filename, data) ⇒ Object

returned by the consume_file command.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/it_tools/git.rb', line 139

def write_gitosis_file filename, data
  File.open(filename, 'w'){|f| 
    f.write "[gitosis]\n\n"
    data.each_pair do |group,grp_data|
      members = grp_data['members']
      writable = grp_data['writable']
      f.write "[group #{group}]\n"
      f.write writable
      f.write members + "\n"
    end
  }
end