Module: Dolt::Sinatra::Actions

Included in:
Base
Defined in:
lib/dolt/sinatra/actions.rb

Instance Method Summary collapse

Instance Method Details

#blame(repo, ref, path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dolt/sinatra/actions.rb', line 108

def blame(repo, ref, path)
  if oid = lookup_ref_oid(repo, ref)
    redirect(blame_url(repo, oid, path)) and return
  end

  data = actions.blame(repo, u(ref), path)
  add_headers(response, :ref => ref)
  body(renderer.render(:blame, data))
rescue Exception => err
  error(err, repo, ref)
end

#blob(repo, ref, path, options = { :template => :blob }) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dolt/sinatra/actions.rb', line 67

def blob(repo, ref, path, options = { :template => :blob })
  if oid = lookup_ref_oid(repo, ref)
    redirect(blob_url(repo, oid, path)) and return
  end

  data = actions.blob(repo, u(ref), path)
  blob = data[:blob]
  return redirect(tree_url(repo, ref, path)) if blob.class.to_s !~ /\bBlob/
  add_headers(response, options.merge(:ref => ref))
  tpl_options = options[:template_options] || {}
  body(renderer.render(options[:template], data, tpl_options))
rescue Exception => err
  error(err, repo, ref)
end

#error(error, repo, ref) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dolt/sinatra/actions.rb', line 30

def error(error, repo, ref)
  template = error.class.to_s == "Rugged::IndexerError" ? :"404" : :"500"
  add_headers(response)
  body(renderer.render(template, {
                         :error => error,
                         :repository_slug => repo,
                         :ref => ref
                       }))
rescue Exception => err
  err_backtrace = err.backtrace.map { |s| "<li>#{s}</li>" }
  error_backtrace = error.backtrace.map { |s| "<li>#{s}</li>" }

  body(<<-HTML)
  <h1>Fatal Dolt Error</h1>
  <p>
    Dolt encountered an exception, and additionally
    triggered another exception trying to render the error.
  </p>
  <h2>Error: #{err.class} #{err.message}</h2>
  <ul>#{err_backtrace.join()}</ul>
  <h2>Original error: #{error.class} #{error.message}</h2>
  <ul>#{error_backtrace.join()}</ul>
  HTML
end

#history(repo, ref, path, count) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dolt/sinatra/actions.rb', line 120

def history(repo, ref, path, count)
  if oid = lookup_ref_oid(repo, ref)
    redirect(history_url(repo, oid, path)) and return
  end

  data = actions.history(repo, u(ref), path, count)
  add_headers(response, :ref => ref)
  body(renderer.render(:commits, data))
rescue Exception => err
  error(err, repo, ref)
end

#raw(repo, ref, path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dolt/sinatra/actions.rb', line 55

def raw(repo, ref, path)
  if oid = lookup_ref_oid(repo, ref)
    redirect(raw_url(repo, oid, path)) and return
  end

  blob(repo, ref, path, {
         :template => :raw,
         :content_type => "text/plain",
         :template_options => { :layout => nil }
       })
end

#redirect(url) ⇒ Object



24
25
26
27
28
# File 'lib/dolt/sinatra/actions.rb', line 24

def redirect(url)
  response.status = 302
  response["Location"] = url
  body ""
end

#refs(repo) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/dolt/sinatra/actions.rb', line 132

def refs(repo)
  data = actions.refs(repo)
  add_headers(response, :content_type => "application/json")
  body(renderer.render(:refs, data, :layout => nil))
rescue Exception => err
  error(err, repo, nil)
end

#resolve_repository(repo) ⇒ Object



152
153
154
# File 'lib/dolt/sinatra/actions.rb', line 152

def resolve_repository(repo)
  actions.resolve_repository(repo)
end

#tree(repo, ref, path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dolt/sinatra/actions.rb', line 82

def tree(repo, ref, path)
  if oid = lookup_ref_oid(repo, ref)
    redirect(tree_url(repo, oid, path)) and return
  end

  data = actions.tree(repo, u(ref), path)
  tree = data[:tree]
  return redirect(blob_url(repo, ref, path)) if tree.class.to_s !~ /\bTree/
  add_headers(response, :ref => ref)
  body(renderer.render(:tree, data))
rescue Exception => err
  error(err, repo, ref)
end

#tree_entry(repo, ref, path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dolt/sinatra/actions.rb', line 96

def tree_entry(repo, ref, path)
  if oid = lookup_ref_oid(repo, ref)
    redirect(tree_entry_url(repo, oid, path)) and return
  end

  data = actions.tree_entry(repo, u(ref), path)
  add_headers(response, :ref => ref)
  body(renderer.render(data.key?(:tree) ? :tree : :blob, data))
rescue Exception => err
  error(err, repo, ref)
end

#tree_history(repo, ref, path, count = 1) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dolt/sinatra/actions.rb', line 140

def tree_history(repo, ref, path, count = 1)
  if oid = lookup_ref_oid(repo, ref)
    redirect(tree_history_url(repo, oid, path)) and return
  end

  data = actions.tree_history(repo, u(ref), path, count)
  add_headers(response, :content_type => "application/json", :ref => ref)
  body(renderer.render(:tree_history, data, :layout => nil))
rescue Exception => err
  error(err, repo, ref)
end