Class: GitAuth::Repo

Inherits:
SaveableClass
  • Object
show all
Includes:
Loggable
Defined in:
lib/gitauth/repo.rb

Constant Summary collapse

NAME_RE =
/^([\w\_\-\.\+]+(\.git)?)$/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, auto_create = false) ⇒ Repo

Returns a new instance of Repo.



42
43
44
45
# File 'lib/gitauth/repo.rb', line 42

def initialize(name, path, auto_create = false)
  @name, @path = name, path
  @permissions = {}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



40
41
42
# File 'lib/gitauth/repo.rb', line 40

def name
  @name
end

#pathObject

Returns the value of attribute path.



40
41
42
# File 'lib/gitauth/repo.rb', line 40

def path
  @path
end

#permissionsObject

Returns the value of attribute permissions.



40
41
42
# File 'lib/gitauth/repo.rb', line 40

def permissions
  @permissions
end

Class Method Details

.create(name, path = name) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/gitauth/repo.rb', line 31

def self.create(name, path = name)
  return false if name.nil? || path.nil?
  return false if self.get(name) || self.all.any? { |r| r.path == path } || name !~ NAME_RE || path !~ NAME_RE
  repository = new(name, path)
  return false unless repository.create_repo!
  add_item(repository)
  repository
end

.get(name) ⇒ Object



26
27
28
29
# File 'lib/gitauth/repo.rb', line 26

def self.get(name)
  logger.debug "Getting Repo w/ name: '#{name}'"
  (all || []).detect { |r| r.name == name }
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
# File 'lib/gitauth/repo.rb', line 47

def ==(other)
  other.is_a?(Repo) && other.name == name && other.path == path
end

#create_repo!Object



84
85
86
87
88
89
90
91
92
# File 'lib/gitauth/repo.rb', line 84

def create_repo!
  return false if !GitAuth.has_git?
  unless File.directory?(real_path)
    FileUtils.mkdir_p(real_path)
    output = ""
    Dir.chdir(real_path) { IO.popen("git --bare init") { |f| output << f.read } }
    !!(output =~ /Initialized empty Git repository/)
  end
end

#destroy!Object



94
95
96
97
98
# File 'lib/gitauth/repo.rb', line 94

def destroy!
  FileUtils.rm_rf(real_path) if File.exist?(real_path)
  self.class.all.reject! { |r| r == self }
  self.class.save!
end

#execute_post_create_hook!Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/gitauth/repo.rb', line 126

def execute_post_create_hook!
  script = File.expand_path("~/.gitauth/post-create")
  if File.executable?(script)
    system(script, @name, @path)
    return $?.success?
  else
    # If there isn't a file, run it ourselves.
    return true
  end
end

#make_empty!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gitauth/repo.rb', line 100

def make_empty!
  tmp_path = "/tmp/gitauth-#{rand(100000)}-#{Time.now.to_i}"
  logger.info "Creating temporary dir at #{tmp_path}"
  FileUtils.mkdir_p("#{tmp_path}/current-repo")
  logger.info "Changing to new directory"
  Dir.chdir("#{tmp_path}/current-repo") do
    logger.info "Marking as git repo"
    GitAuth.run "git init"
    logger.info "Touching .gitignore"
    GitAuth.run "touch .gitignore"
    # Configure it
    GitAuth.run "git config push.default current"
    logger.info "Commiting"
    GitAuth.run "git add ."
    GitAuth.run "git commit -am 'Initialize Empty Repository'"
    # Push the changes to the actual repository
    logger.info "Adding origin #{self.real_path}"
    GitAuth.run "git remote add origin '#{self.real_path}'"
    logger.info "Pushing..."
    GitAuth.run "git push origin master"
  end
ensure
  logger.info "Cleaning up old tmp file"
  FileUtils.rm_rf(tmp_path) if File.directory?(tmp_path)
end

#readable_by(whom) ⇒ Object



55
56
57
# File 'lib/gitauth/repo.rb', line 55

def readable_by(whom)
  add_permissions :read, whom
end

#readable_by?(user_or_group) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/gitauth/repo.rb', line 70

def readable_by?(user_or_group)
  has_permissions_for :read, user_or_group
end

#real_pathObject



80
81
82
# File 'lib/gitauth/repo.rb', line 80

def real_path
  File.join(GitAuth::Settings.base_path, @path)
end

#remove_permissions_for(user_or_group) ⇒ Object



74
75
76
77
78
# File 'lib/gitauth/repo.rb', line 74

def remove_permissions_for(user_or_group)
  @permissions.each_value do |val|
    val.reject! { |m| m == user_or_group.to_s }
  end
end

#update_permissions!(user, permissions = []) ⇒ Object



59
60
61
62
63
64
# File 'lib/gitauth/repo.rb', line 59

def update_permissions!(user, permissions = [])
  remove_permissions_for(user)
  writeable_by(user) if permissions.include?("write")
  readable_by(user)  if permissions.include?("read")
  self.class.save!
end

#writeable_by(whom) ⇒ Object



51
52
53
# File 'lib/gitauth/repo.rb', line 51

def writeable_by(whom)
  add_permissions :write, whom
end

#writeable_by?(user_or_group) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/gitauth/repo.rb', line 66

def writeable_by?(user_or_group)
  has_permissions_for :write, user_or_group
end