Module: GitModel

Defined in:
lib/gitmodel.rb,
lib/gitmodel/index.rb,
lib/gitmodel/errors.rb,
lib/gitmodel/persistable.rb,
lib/gitmodel/transaction.rb

Defined Under Namespace

Modules: Persistable Classes: AttributeNotIndexed, GitModelError, Index, IndexRequired, NullId, RecordDoesntExist, RecordExists, RecordNotFound, RecordNotSaved, Transaction

Class Method Summary collapse

Class Method Details

.create_db!Object

Create the database defined in db_root. Raises an exception if it exists.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitmodel.rb', line 40

def self.create_db!
  raise "Database #{db_root} already exists!" if File.exist? db_root
  if db_root =~ /.+\.git/
    #logger.info "Creating database (bare): #{db_root}"
    #Grit::Repo.init_bare db_root
    logger.error "Bare repositories aren't supported yet"
  else
    logger.info "Creating database: #{db_root}"
    Grit::Repo.init db_root
  end
end

.current_tree(branch = nil) ⇒ Object



72
73
74
75
# File 'lib/gitmodel.rb', line 72

def self.current_tree(branch = nil)
  c = last_commit(branch)
  c ? c.tree : nil
end

.index!Object



77
78
79
80
81
82
# File 'lib/gitmodel.rb', line 77

def self.index!
  dirs = (GitModel.current_tree).trees
  dirs.each do |dir|
    dir.name.classify.constantize.index!
  end
end

.last_commit(branch = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gitmodel.rb', line 59

def self.last_commit(branch = nil)
  branch ||= default_branch
  # PERFORMANCE Cache this somewhere and update it on commit?
  # (Need separate instance per branch)

  return nil unless repo.commits(branch).any?

  # We should be able to use just repo.commits(branch).first here but
  # this is a workaround for this bug: 
  # http://github.com/mojombo/grit/issues/issue/38
  GitModel.repo.commits("#{branch}^..#{branch}").first || GitModel.repo.commits(branch).first
end

.recreate_db!Object

Delete and re-create the database defined in db_root. Dangerous!



53
54
55
56
57
# File 'lib/gitmodel.rb', line 53

def self.recreate_db!
  logger.info "Deleting database #{db_root}!!"
  FileUtils.rm_rf db_root
  create_db!
end

.repoObject



35
36
37
# File 'lib/gitmodel.rb', line 35

def self.repo
  @@repo = Grit::Repo.new(GitModel.db_root)
end