BigW Consortium Gitlab

namespace.rb 2.34 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# == Schema Information
#
# Table name: namespaces
#
#  id         :integer          not null, primary key
#  name       :string(255)      not null
#  path       :string(255)      not null
#  owner_id   :integer          not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  type       :string(255)
#

14
class Namespace < ActiveRecord::Base
15
  attr_accessible :name, :path
16

17
  has_many :projects, dependent: :destroy
18 19 20
  belongs_to :owner, class_name: "User"

  validates :name, presence: true, uniqueness: true
21
  validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
22
            format: { with: Gitlab::Regex.path_regex,
23
                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
24 25 26 27
  validates :owner, presence: true

  delegate :name, to: :owner, allow_nil: true, prefix: true

28 29
  after_create :ensure_dir_exist
  after_update :move_dir
30
  after_commit :update_gitolite, on: :update, if: :require_update_gitolite
31
  after_destroy :rm_dir
32

33 34
  scope :root, where('type IS NULL')

35 36
  attr_accessor :require_update_gitolite

37
  def self.search query
38
    where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
39 40
  end

41 42 43 44
  def self.global_id
    'GLN'
  end

45
  def to_param
46
    path
47
  end
48 49 50 51

  def human_name
    owner_name
  end
52 53

  def ensure_dir_exist
Dmitriy Zaporozhets committed
54 55 56
    unless dir_exists?
      system("mkdir -m 770 #{namespace_full_path}")
    end
57 58 59
  end

  def dir_exists?
Dmitriy Zaporozhets committed
60 61 62 63 64
    File.exists?(namespace_full_path)
  end

  def namespace_full_path
    @namespace_full_path ||= File.join(Gitlab.config.gitolite.repos_path, path)
65
  end
66 67

  def move_dir
68
    if path_changed?
69 70
      old_path = File.join(Gitlab.config.gitolite.repos_path, path_was)
      new_path = File.join(Gitlab.config.gitolite.repos_path, path)
71 72 73
      if File.exists?(new_path)
        raise "Already exists"
      end
74 75 76

      if system("mv #{old_path} #{new_path}")
        send_update_instructions
77 78 79
        @require_update_gitolite = true
      else
        raise "Namespace move error #{old_path} #{new_path}"
80
      end
81
    end
82
  end
83

84 85 86 87 88
  def update_gitolite
    @require_update_gitolite = false
    projects.each(&:update_repository)
  end

89
  def rm_dir
90
    dir_path = File.join(Gitlab.config.gitolite.repos_path, path)
91 92
    system("rm -rf #{dir_path}")
  end
93 94 95 96

  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
97
end