summaryrefslogtreecommitdiff
path: root/app/models/repository.rb
blob: 3b09471596cbd8779367c44b00e2fdefcaeee591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Repository
  attr_accessor :project

  def self.default_ref
    "master"
  end

  def initialize(project)
    @project = project
  end

  def path 
    @path ||= project.path
  end

  def project_id
    project.id
  end

  def repo
    @repo ||= Grit::Repo.new(project.path_to_repo)
  end

  def url_to_repo
    if !GITOSIS["port"] or GITOSIS["port"] == 22
      "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"
    else
      "ssh://#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{GITOSIS["port"]}/#{path}.git"
    end
  end

  def path_to_repo
    GITOSIS["base_path"] + path + ".git"
  end

  def update_gitosis_project
    Gitosis.new.configure do |c|
      c.update_project(path, project.gitosis_writers)
    end
  end

  def destroy_gitosis_project
    Gitosis.new.configure do |c|
      c.destroy_project(@project)
    end
  end

  def repo_exists?
    repo rescue false
  end

  def tags
    repo.tags.map(&:name).sort.reverse
  end

  def heads
    @heads ||= repo.heads
  end

  def tree(fcommit, path = nil)
    fcommit = commit if fcommit == :head
    tree = fcommit.tree
    path ? (tree / path) : tree
  end

  def commit(commit_id = nil)
    if commit_id
      repo.commits(commit_id).first
    else
      repo.commits.first
    end
  end

  def fresh_commits(n = 10)
    commits = heads.map do |h|
      repo.commits(h.name, n)
    end.flatten.uniq { |c| c.id }

    commits.sort! do |x, y|
      y.committed_date <=> x.committed_date
    end

    commits[0...n]
  end

  def commits_since(date)
    commits = heads.map do |h|
      repo.log(h.name, nil, :since => date)
    end.flatten.uniq { |c| c.id }

    commits.sort! do |x, y|
      y.committed_date <=> x.committed_date
    end

    commits
  end
end