summaryrefslogtreecommitdiffstats
path: root/camping/gitweb.rb
blob: 433f58299693353b7b837d71a7fa3b7cfd3c9e26 (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
98
99
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'rubygems'
require 'camping'
require 'git'

# this is meant to be a git-less web head to your git repo
#
# install dependencies
#   sudo gem install camping-omnibus --source http://code.whytheluckystiff.net
#
# author : scott chacon
# /usr/local/lib/ruby/gems/1.8/gems/camping-1.5.180/examples/

Camping.goes :GitWeb

module GitWeb::Models
  class Repository < Base; end
  
  class CreateGitWeb < V 0.1
    def self.up
      create_table :gitweb_repositories, :force => true do |t|
        t.column :name,  :string 
        t.column :path,  :string 
        t.column :bare,  :boolean 
      end
    end
  end
end

module GitWeb::Controllers
  class Index < R '/'
    def get
      @repos = Repository.find :all
      render :index
    end
  end
  
  class View < R '/view/(\d+)'
    def get repo_id
      @repo = Repository.find repo_id
      @git = Git.bare(@repo.path)      
      render :view
    end
  end
  
  class Fetch < R '/git/(\d+)'
    def get repo_id
      @repo = Repository.find repo_id
      @git = Git.bare(@repo.path)
    end
  end
  
  class Add < R '/add'
    def get
      @repo = Repository.new
      render :add
    end
    def post
      if Git.bare(input.repository_path)
        repo = Repository.create :name => input.repo_name, :path => input.repo_path, :bare => input.repo_bare
        redirect View, repo
      else
        redirect Index
      end
    end
  end

  class Commit < R '/commit/(\d+)/(\w+)'
    def get repo_id, sha
      @repo = Repository.find repo_id
      @git = Git.bare(@repo.path)      
      @commit = @git.gcommit(sha)
      render :commit
    end
  end
  class Diff < R '/diff/(\d+)/(\w+)'
  end
  class Tree < R '/tree/(\d+)/(\w+)'
  end
  class Archive < R '/archive/(\d+)/(\W+)'
  end
end

module GitWeb::Views
  def layout
    html do
      body do
        self << yield
      end
    end
  end

  def view
    h1 @repo.name
    h2 @repo.path

    url = 'http:' + URL(Fetch, @repo.id).to_s

    h3 'info'
    table.info do
      tr { td 'owner: '; td @git.config('user.name') }
      tr { td 'email: '; td @git.config('user.email') }
      tr { td 'url: '; td { a url, :href => url } }
    end
    
    h3 'shortlog'
    table.shortlog do
      @git.log.each do |log|
        tr do
          td log.date.strftime("%Y-%m-%d")
          td log.sha[0, 8]
          td { em log.author.name }
          td log.message[0, 60]
          td { a 'commit', :href => R(Commit, @repo, log.sha) }
          td { a 'diff', :href => R(Diff, @repo, log.sha) }
          td { a 'tree', :href => R(Tree, @repo, log.sha) }
          td { a 'archive', :href => R(Archive, @repo, log.sha) }
        end
      end
    end
    
    h3 'heads'
    @git.branches.each do |branch|
      li branch.full
    end
  end
  
  def commit
    h1 @commit.name
    h2 @commit.sha
    p @commit.message
    p @commit.parent
    p @commit.author.name
    p @commit.author.email
    p @commit.committer.name
    p @commit.committer.email
    p @commit.gtree.sha
  end
  
  def add
    _form(@repo)
  end
  
  def _form(repo)
    form(:method => 'post') do
      label 'Path', :for => 'repo_path'; br
      input :name => 'repo_path', :type => 'text', :value => repo.path; br

      label 'Name', :for => 'repo_name'; br
      input :name => 'repo_name', :type => 'text', :value => repo.name; br

      label 'Bare', :for => 'repo_bare'; br
      input :type => 'checkbox', :name => 'repo_bare', :value => repo.bare; br

      input :type => 'hidden', :name => 'repo_id', :value => repo.id
      input :type => 'submit'
    end
  end
  
  def index
    @repos.each do | repo |
      h1 repo.name
      a repo.path, :href => R(View, repo.id)
    end
    br
    br
    a 'add new repo', :href => R(Add)
  end
end

def GitWeb.create
  GitWeb::Models.create_schema
end