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, custom_data = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/dolt/sinatra/actions.rb', line 106

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

  data = (custom_data || {}).merge(actions.blame(repo, u(ref), path))
  add_headers(response, :ref => ref)
  body(renderer.render(:blame, data))
end

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dolt/sinatra/actions.rb', line 71

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

  data = (custom_data || {}).merge(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))
end

#history(repo, ref, path, count, custom_data = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/dolt/sinatra/actions.rb', line 116

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

  data = (custom_data || {}).merge(actions.history(repo, u(ref), path, count))
  add_headers(response, :ref => ref)
  body(renderer.render(:commits, data))
end

#lookup_ref_oid(repo, ref) ⇒ Object



147
148
149
150
# File 'lib/dolt/sinatra/actions.rb', line 147

def lookup_ref_oid(repo, ref)
  return if !respond_to?(:redirect_refs?) || !redirect_refs? || ref.length == 40
  actions.rev_parse_oid(repo, ref)
end

#raw(repo, ref, path, custom_data = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dolt/sinatra/actions.rb', line 59

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

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

#redirect(url, status = 302) ⇒ Object



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

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

#refs(repo, custom_data = {}) ⇒ Object



126
127
128
129
130
# File 'lib/dolt/sinatra/actions.rb', line 126

def refs(repo, custom_data = {})
  data = (custom_data || {}).merge(actions.refs(repo))
  add_headers(response, :content_type => "application/json")
  body(renderer.render(:refs, data, :layout => nil))
end

#render_error(error, repo, ref) ⇒ Object



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
# File 'lib/dolt/sinatra/actions.rb', line 31

def render_error(error, repo, ref)
  if error.class.to_s == "Rugged::ReferenceError" && ref == "HEAD"
    return body(renderer.render("empty", { :repository => repo, :ref => ref }))
  end
  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

#resolve_repository(repo) ⇒ Object



142
143
144
145
# File 'lib/dolt/sinatra/actions.rb', line 142

def resolve_repository(repo)
  @cache ||= {}
  @cache[repo] ||= actions.resolve_repository(repo)
end

#tree(repo, ref, path, custom_data = {}) ⇒ Object



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

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

  data = (custom_data || {}).merge(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))
end

#tree_entry(repo, ref, path, custom_data = {}) ⇒ Object



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

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

  data = (custom_data || {}).merge(actions.tree_entry(repo, u(ref), path))
  add_headers(response, :ref => ref)
  body(renderer.render(data.key?(:tree) ? :tree : :blob, data))
end

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



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

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

  data = (custom_data || {}).merge(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))
end