forked from al3x/git-wiki
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-wiki.rb
executable file
·254 lines (214 loc) · 5.58 KB
/
git-wiki.rb
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env ruby
require 'fileutils'
require './environment'
require 'sinatra'
require 'sinatra/content_for'
configure do
set :erb, layout: :'layouts/defaults.slim'
set :slim, layout: :'layouts/default'
end
helpers do
def gravatar_url(email, size: 80)
hash = Digest::MD5.hexdigest(email.to_s.strip.downcase)
'http://www.gravatar.com/avatar/' + hash + "?s=#{size}&d=identicon"
end
end
Dir["./app/helpers/*.rb"].each { |file| require file }
helpers PathHelpers
helpers HTMLHelpers
get('/') do
@no_search_in_header = true
@title = 'Git Wiki'
slim :root
end
get '/a/history' do
@history = $repo.log
@title = "Branch History"
slim :"git/history"
end
# page paths
get '/:page' do
@page = Page.new(params[:page])
return redirect(edit_page_path(@page)) unless @page.tracked?
@title = @page.nice_name
slim :show
end
get '/:page/raw' do
@page = Page.new(params[:page])
send_data @page.raw_body, :type => 'text/plain', :disposition => 'inline'
end
get '/:page/edit' do
@page = Page.new(params[:page])
@title = "Editing #{@page.nice_name}"
slim :edit
end
# post '/:page/append' do
# @page = Page.new(params[:page])
# @page.update(@page.raw_body + "\n\n" + params[:text], params[:message])
# redirect '/' + @page.name
# end
post '/:page' do
@page = Page.new(params[:page])
@page.update(params[:body], params[:message])
new_name = params[:name].gsub(/\s/, '_')
if params[:page] == new_name
redirect '/' + @page.name
else
$repo.lib.mv(params[:page], new_name)
$repo.commit("Renamed #{params[:page]} to #{new_name}")
redirect '/' + new_name
end
end
post '/eip/:page' do
@page = Page.new(params[:page])
@page.update(params[:body])
@page.body
end
get '/:page/history' do
@page = Page.new(params[:page])
@title = "History of #{@page.nice_name}"
slim :page_history
end
get '/:page/history/:rev.diff' do
@page = Page.new(params[:page])
@diff = @page.delta(params[:rev])
send_data @diff, :type => 'text/plain', :disposition => 'inline'
end
get '/:page/history/:rev' do
@page = Page.new(params[:page], params[:rev])
@revision = params[:rev]
@title = "#{@page.nice_name} (version #{@revision})"
slim :show
end
get '/:page/history/:rev/diff' do
@page = Page.new(params[:page])
@revision = params[:rev]
@title = "Diff of #{@page.nice_name}"
@diff = @page.delta(@revision)
slim :delta
end
# application paths (/a/ namespace)
# maybe I should use an underscore as the app-path _
get '/a/list' do
pages = $repo.log.first.gtree.children
@pages = pages.select { |f,bl| f[0,1] != '_'}.sort.map { |name, blob| Page.new(name) } rescue []
@title = 'All pages'
slim :list
end
get '/a/patch/:page/:rev' do
@page = Page.new(params[:page])
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
@title = "Branches"
slim :"/git/branches"
end
get '/a/branch/:branch' do
$repo.checkout(params[:branch])
redirect '/' + HOMEPAGE
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
@q = Query.new($repo, params[:q])
@perfect_match = @q.perfect_match
@results = @q.results
if @perfect_match
@results.reject! { |result| result.filename == @perfect_match.filename }
end
slim :search
end
post '/a/pull' do
Dir.chdir(GIT_REPO) do
`git fetch`
@output = `git merge || git merge --abort`
end
slim :"git/pull"
end
post '/a/push' do
Dir.chdir(GIT_REPO) do
@output = `git push`
end
@succesful = $?.success?
slim :"git/push"
end
get '/a/git-wiki.css' do
sass :'sass/git_wiki'
end
# file upload attachments
# get '/a/file/upload/:page' do
# @page = Page.new(params[:page])
# show :attach, 'Attach File for ' + @page.name
# end
# post '/a/file/upload/:page' do
# @page = Page.new(params[:page])
# @page.save_file(params[:file], params[:name])
# redirect '/e/' + @page.name
# end
# get '/a/file/delete/:page/:file.:ext' do
# @page = Page.new(params[:page])
# @page.delete_file(params[:file] + '.' + params[:ext])
# redirect '/e/' + @page.name
# end
# get '/_attachment/:page/:file.:ext' do
# @page = Page.new(params[:page])
# send_file(File.join(@page.attach_dir, params[:file] + '.' + params[:ext]))
# end
private
def show(template, title)
@title = title
erb(("#{template}.html").to_sym)
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