public
Fork of sr/git-wiki
Description: A wiki engine that uses a Git repository as its data store.
Homepage: http://atonie.org/2008/02/git-wiki
Clone URL: git://github.com/al3x/git-wiki.git
Search Repo:
schacon (author)
Sat Mar 08 12:27:38 -0800 2008
commit  3fe21594034d22381cc5dbbfef0181b3949e2893
tree    ab87643e09b020797f15477bc5d84965511b5697
parent  e7718c564e758a37489123ba3a097261e73b0369
git-wiki / git-wiki.rb
100755 164 lines (137 sloc) 4.034 kb
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
#!/usr/bin/env ruby
 
require 'environment'
 
require_gem_with_feedback 'sinatra'
 
layout { File.read('views/layout.erb') }
 
def show(template, title)
  @title = title
  erb(template)
end
 
def page_with_ext
  if params[:format] == "html"
    params[:page]
  else
    "#{params[:page]}.#{params[:format]}"
  end
end
 
def page_url
  "#{request.env["rack.url_scheme"]}://#{request.env["HTTP_HOST"]}#{request.env["REQUEST_PATH"]}"
end
 
def touchfile
  # adds meta file to repo so we have somthing to commit initially
  $repo.chdir do
    f = File.new(".meta", "w+")
    f.puts($repo.current_branch)
    f.close
    $repo.add('.meta')
  end
end
 
get('/') { redirect '/' + HOMEPAGE }
get('/_style.css') { header 'Content-Type' => 'text/css'; File.read(File.join(File.dirname(__FILE__), 'css', 'style.css')) }
get('/_code.css') { header 'Content-Type' => 'text/css'; File.read(File.join(File.dirname(__FILE__), 'css', "#{UV_THEME}.css")) }
get('/_app.js') { header 'Content-Type' => 'application/x-javascript'; File.read(File.join(File.dirname(__FILE__), 'javascripts', "application.js")) }
get('/_search.png') { header 'Content-Type' => 'image/png'; File.read(File.join(File.dirname(__FILE__), 'images', "search.png")) }
 
get '/_list' do
  @pages = $repo.log.first.gtree.children.map { |name, blob| Page.new(name) } rescue []
  show(:list, 'Listing pages')
end
 
get '/:page' do
  @page_url = page_url
  @page = Page.new(page_with_ext)
  @page.tracked? ? show(:show, @page.name) : redirect('/e/' + @page.name)
end
 
get '/:page/append' do
  @page = Page.new(page_with_ext)
  @page.body = @page.raw_body + "\n\n" + params[:text]
  redirect '/' + @page.name
end
 
get '/e/:page' do
  @page = Page.new(page_with_ext)
  show :edit, "Editing #{@page.name}"
end
 
post '/e/:page' do
  @page = Page.new(page_with_ext)
  @page.update(params[:body], params[:message])
  redirect '/' + @page.name
end
 
get '/h/:page' do
  @page = Page.new(page_with_ext)
  show :history, "History of #{@page.name}"
end
 
['/h/:page/:rev', '/h/:page.:format/:rev'].each do |r|
  get r do
    @page = Page.new(page_with_ext, params[:rev])
    show :show, "#{@page.name} (version #{params[:rev]})"
  end
end
 
['/d/:page/:rev', '/d/:page.:format/:rev'].each do |r|
  get r do
    @page = Page.new(page_with_ext)
    show :delta, "Diff of #{@page.name}"
  end
end
 
get '/a/patch/:page/:rev' do
  @page = Page.new(page_with_ext)
  header 'Content-Type' => 'text/x-diff'
  header 'Content-Disposition' => 'filename=patch.diff'
  @page.delta(params[:rev])
end
 
get '/a/tarball' do
  header 'Content-Type' => 'application/x-gzip'
  header 'Content-Disposition' => 'filename=archive.tgz'
  archive = $repo.archive('HEAD', nil, :format => 'tgz', :prefix => 'wiki/')
  File.open(archive).read
end
 
get '/a/branches' do
  @branches = $repo.branches
  show :branches, "Branches List"
end
 
get '/a/branch/:branch' do
  $repo.checkout(params[:branch])
  redirect '/' + HOMEPAGE
end
 
get '/a/history' do
  @history = $repo.log
  show :branch_history, "Branch History"
end
 
get '/a/revert_branch/:sha' do
  $repo.with_temp_index do
    $repo.read_tree params[:sha]
    $repo.checkout_index
    $repo.commit('reverted branch')
  end
  redirect '/a/history'
end
 
get '/a/merge_branch/:branch' do
  $repo.merge(params[:branch])
  redirect '/' + HOMEPAGE
end
 
get '/a/delete_branch/:branch' do
  $repo.branch(params[:branch]).delete
  redirect '/a/branches'
end
 
post '/a/new_branch' do
  $repo.branch(params[:branch]).create
  $repo.checkout(params[:branch])
  if params[:type] == 'blank'
    # clear out the branch
    $repo.chdir do
      Dir.glob("*").each do |f|
        File.unlink(f)
        $repo.remove(f)
      end
      touchfile
      $repo.commit('clean branch start')
    end
  end
  redirect '/a/branches'
end
 
post '/a/new_remote' do
  $repo.add_remote(params[:branch_name], params[:branch_url])
  $repo.fetch(params[:branch_name])
  redirect '/a/branches'
end
 
get '/a/search' do
  @search = params[:search]
  @grep = $repo.grep(@search)
  show :search, 'Search Results'
end