Class: Dolt::ControllerActions

Inherits:
Object
  • Object
show all
Defined in:
lib/libdolt/controller_actions.rb

Instance Method Summary collapse

Constructor Details

#initialize(router, lookup, renderer) ⇒ ControllerActions

Returns a new instance of ControllerActions.



24
25
26
27
28
# File 'lib/libdolt/controller_actions.rb', line 24

def initialize(router, lookup, renderer)
  @router = router
  @lookup = lookup
  @renderer = renderer
end

Instance Method Details

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



135
136
137
138
139
140
141
142
# File 'lib/libdolt/controller_actions.rb', line 135

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

  data = (custom_data || {}).merge(lookup.blame(repo, u(ref), path))
  [200, headers(:ref => ref), [renderer.render(:blame, data)]]
end

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/libdolt/controller_actions.rb', line 99

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

  data = (custom_data || {}).merge(lookup.blob(repo, u(ref), path))
  blob = data[:blob]
  return redirect(router.tree_url(repo, ref, path)) if blob.class.to_s !~ /\bBlob/

  tpl_options = options[:template_options] || {}
  [200, headers(options.merge(:ref => ref)), [
      renderer.render(options[:template], data, tpl_options)
    ]]
end

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



144
145
146
147
148
149
150
151
# File 'lib/libdolt/controller_actions.rb', line 144

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

  data = (custom_data || {}).merge(lookup.history(repo, u(ref), path, count))
  [200, headers(:ref => ref), [renderer.render(:commits, data)]]
end

#lookup_ref_oid(repo, ref) ⇒ Object



176
177
178
179
# File 'lib/libdolt/controller_actions.rb', line 176

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

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/libdolt/controller_actions.rb', line 87

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

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

#redirect(url, status = 302) ⇒ Object



30
31
32
33
# File 'lib/libdolt/controller_actions.rb', line 30

def redirect(url, status = 302)
  body = "You are being <a href=\"#{url}\">redirected to #{url}</a>"
  [status, { "Location" => url }, [body]]
end

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



153
154
155
156
157
158
# File 'lib/libdolt/controller_actions.rb', line 153

def refs(repo, custom_data = {})
  data = (custom_data || {}).merge(lookup.refs(repo))
  [200, headers(:content_type => "application/json"), [
      renderer.render(:refs, data, :layout => nil)
    ]]
end

#render_error(error, repo, ref, data = {}) ⇒ Object



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
# File 'lib/libdolt/controller_actions.rb', line 35

def render_error(error, repo, ref, data = {})
  $stderr.puts("#{error.class.to_s}: #{error.message}")
  $stderr.puts(error.backtrace)

  if error.class.to_s == "Rugged::ReferenceError" && ref == "HEAD"
    template = "empty"
    return [200, headers, [renderer.render(template, {
            :repository => repo,
            :ref => ref
          }.merge(data))]]
  end

  if error.class.to_s == "Rugged::ReferenceError"
    template = "non_existent"
    return [404, headers, [renderer.render(template, {
            :repository => repo,
            :ref => ref,
            :error => error
          }.merge(data))]]
  end

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

  [500, headers, [<<-HTML]]
    <h1>Fatal Dolt Error</h1>
    <p>
      Dolt encountered an exception, and additionally
      triggered another exception trying to render the error.
    </p>
    <p>Tried to render the #{template} template with the following data:</p>
    <dl>
      <dt>Repository</dt>
      <dd>#{repo}</dd>
      <dt>Ref</dt>
      <dd>#{ref}</dd>
    </dl>
    <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



171
172
173
174
# File 'lib/libdolt/controller_actions.rb', line 171

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

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



114
115
116
117
118
119
120
121
122
123
# File 'lib/libdolt/controller_actions.rb', line 114

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

  data = (custom_data || {}).merge(lookup.tree(repo, u(ref), path))
  tree = data[:tree]
  return redirect(router.blob_url(repo, ref, path)) if tree.class.to_s !~ /\bTree/
  [200, headers(:ref => ref), [renderer.render(:tree, data)]]
end

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



125
126
127
128
129
130
131
132
133
# File 'lib/libdolt/controller_actions.rb', line 125

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

  data = (custom_data || {}).merge(lookup.tree_entry(repo, u(ref), path))
  body = renderer.render(data.key?(:tree) ? :tree : :blob, data)
  [200, headers(:ref => ref), [body]]
end

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



160
161
162
163
164
165
166
167
168
169
# File 'lib/libdolt/controller_actions.rb', line 160

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

  data = (custom_data || {}).merge(lookup.tree_history(repo, u(ref), path, count))
  [200, headers(:content_type => "application/json", :ref => ref), [
      renderer.render(:tree_history, data, :layout => nil)
    ]]
end