|
| 1 | +require 'rubygems' |
| 2 | +require 'camping' |
| 3 | +require 'git' |
| 4 | + |
| 5 | +# this is meant to be a git-less web head to your git repo |
| 6 | +# |
| 7 | +# install dependencies |
| 8 | +# sudo gem install camping-omnibus --source http://code.whytheluckystiff.net |
| 9 | +# |
| 10 | +# author : scott chacon |
| 11 | +# /usr/local/lib/ruby/gems/1.8/gems/camping-1.5.180/examples/ |
| 12 | + |
| 13 | +Camping.goes :GitWeb |
| 14 | + |
| 15 | +module GitWeb::Models |
| 16 | + class Repository < Base; end |
| 17 | + |
| 18 | + class CreateGitWeb < V 0.1 |
| 19 | + def self.up |
| 20 | + create_table :gitweb_repositories, :force => true do |t| |
| 21 | + t.column :name, :string |
| 22 | + t.column :path, :string |
| 23 | + t.column :bare, :boolean |
| 24 | + end |
| 25 | + end |
| 26 | + end |
| 27 | +end |
| 28 | + |
| 29 | +module GitWeb::Controllers |
| 30 | + class Index < R '/' |
| 31 | + def get |
| 32 | + @repos = Repository.find :all |
| 33 | + render :index |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + class View < R '/view/(\d+)' |
| 38 | + def get repo_id |
| 39 | + @repo = Repository.find repo_id |
| 40 | + @git = Git.bare(@repo.path) |
| 41 | + render :view |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + class Fetch < R '/git/(\d+)' |
| 46 | + def get repo_id |
| 47 | + @repo = Repository.find repo_id |
| 48 | + @git = Git.bare(@repo.path) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + class Add < R '/add' |
| 53 | + def get |
| 54 | + @repo = Repository.new |
| 55 | + render :add |
| 56 | + end |
| 57 | + def post |
| 58 | + if Git.bare(input.repository_path) |
| 59 | + repo = Repository.create :name => input.repo_name, :path => input.repo_path, :bare => input.repo_bare |
| 60 | + redirect View, repo |
| 61 | + else |
| 62 | + redirect Index |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + class Commit < R '/commit/(\d+)/(\w+)' |
| 68 | + def get repo_id, sha |
| 69 | + @repo = Repository.find repo_id |
| 70 | + @git = Git.bare(@repo.path) |
| 71 | + @commit = @git.gcommit(sha) |
| 72 | + render :commit |
| 73 | + end |
| 74 | + end |
| 75 | + class Diff < R '/diff/(\d+)/(\w+)' |
| 76 | + end |
| 77 | + class Tree < R '/tree/(\d+)/(\w+)' |
| 78 | + end |
| 79 | + class Archive < R '/archive/(\d+)/(\W+)' |
| 80 | + end |
| 81 | +end |
| 82 | + |
| 83 | +module GitWeb::Views |
| 84 | + def layout |
| 85 | + html do |
| 86 | + body do |
| 87 | + self << yield |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def view |
| 93 | + h1 @repo.name |
| 94 | + h2 @repo.path |
| 95 | + |
| 96 | + url = 'http:' + URL(Fetch, @repo.id).to_s |
| 97 | + |
| 98 | + h3 'info' |
| 99 | + table.info do |
| 100 | + tr { td 'owner: '; td @git.config('user.name') } |
| 101 | + tr { td 'email: '; td @git.config('user.email') } |
| 102 | + tr { td 'url: '; td { a url, :href => url } } |
| 103 | + end |
| 104 | + |
| 105 | + h3 'shortlog' |
| 106 | + table.shortlog do |
| 107 | + @git.log.each do |log| |
| 108 | + tr do |
| 109 | + td log.date.strftime("%Y-%m-%d") |
| 110 | + td log.sha[0, 8] |
| 111 | + td { em log.author.name } |
| 112 | + td log.message[0, 60] |
| 113 | + td { a 'commit', :href => R(Commit, @repo, log.sha) } |
| 114 | + td { a 'diff', :href => R(Diff, @repo, log.sha) } |
| 115 | + td { a 'tree', :href => R(Tree, @repo, log.sha) } |
| 116 | + td { a 'archive', :href => R(Archive, @repo, log.sha) } |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + h3 'heads' |
| 122 | + @git.branches.each do |branch| |
| 123 | + li branch.full |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + def commit |
| 128 | + h1 @commit.name |
| 129 | + h2 @commit.sha |
| 130 | + p @commit.message |
| 131 | + p @commit.parent |
| 132 | + p @commit.author.name |
| 133 | + p @commit.author.email |
| 134 | + p @commit.committer.name |
| 135 | + p @commit.committer.email |
| 136 | + p @commit.gtree.sha |
| 137 | + end |
| 138 | + |
| 139 | + def add |
| 140 | + _form(@repo) |
| 141 | + end |
| 142 | + |
| 143 | + def _form(repo) |
| 144 | + form(:method => 'post') do |
| 145 | + label 'Path', :for => 'repo_path'; br |
| 146 | + input :name => 'repo_path', :type => 'text', :value => repo.path; br |
| 147 | + |
| 148 | + label 'Name', :for => 'repo_name'; br |
| 149 | + input :name => 'repo_name', :type => 'text', :value => repo.name; br |
| 150 | + |
| 151 | + label 'Bare', :for => 'repo_bare'; br |
| 152 | + input :type => 'checkbox', :name => 'repo_bare', :value => repo.bare; br |
| 153 | + |
| 154 | + input :type => 'hidden', :name => 'repo_id', :value => repo.id |
| 155 | + input :type => 'submit' |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + def index |
| 160 | + @repos.each do | repo | |
| 161 | + h1 repo.name |
| 162 | + a repo.path, :href => R(View, repo.id) |
| 163 | + end |
| 164 | + br |
| 165 | + br |
| 166 | + a 'add new repo', :href => R(Add) |
| 167 | + end |
| 168 | +end |
| 169 | + |
| 170 | +def GitWeb.create |
| 171 | + GitWeb::Models.create_schema |
| 172 | +end |
0 commit comments