Class: Gitgo::Controllers::Code

Inherits:
Gitgo::Controller show all
Includes:
Rest
Defined in:
lib/gitgo/controllers/code.rb

Constant Summary collapse

Comment =
Documents::Comment

Constants inherited from Gitgo::Controller

Gitgo::Controller::HEAD, Gitgo::Controller::MOUNT, Gitgo::Controller::ROOT

Instance Method Summary collapse

Methods included from Rest

#attrs, #create, #destroy, #edit, #preview, #preview?, #redirect_to_doc, #show, #update

Methods inherited from Gitgo::Controller

#call, #form, #format, #html, #initialize, #mount_point, #path, #repo, #session_head, #session_head=, #url

Constructor Details

This class inherits a constructor from Gitgo::Controller

Instance Method Details

#blob_grepObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitgo/controllers/code.rb', line 73

def blob_grep
  options = grep_opts(:e => request['pattern'])
  
  selected = []
  git.grep(options, treeish) do |path, blob|
    selected << [path, blob.id]
  end

  erb :grep, :locals => options.merge!(
    :type => 'blob',
    :at => treeish,
    :selected => selected,
    :refs => grit.refs
  )
end

#commit_grepObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gitgo/controllers/code.rb', line 105

def commit_grep
  options = grep_opts(
    :author => request['author'],
    :committer => request['committer'],
    :grep => request['grep'],
    :regexp_ignore_case => request['regexp_ignore_case'] == 'true',
    :fixed_strings => request['fixed_strings'] == 'true',
    :all_match => request['all_match'] == 'true',
    :max_count => request['max_count'] || '10'
  )
  
  selected = []
  git.commit_grep(options, treeish) {|sha| selected << sha }
  
  erb :commit_grep, :locals => options.merge!(
    :selected => selected
  )
end

#gitObject



38
39
40
# File 'lib/gitgo/controllers/code.rb', line 38

def git
  @git ||= repo.git
end

#grep_opts(overrides = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/gitgo/controllers/code.rb', line 65

def grep_opts(overrides={})
  {
    :ignore_case   => request['ignore_case'] == 'true',
    :invert_match  => request['invert_match'] == 'true',
    :fixed_strings => request['fixed_strings'] == 'true',
  }.merge!(overrides)
end

#gritObject



42
43
44
# File 'lib/gitgo/controllers/code.rb', line 42

def grit
  @grit ||= git.grit
end

#indexObject

actions



54
55
56
57
58
59
# File 'lib/gitgo/controllers/code.rb', line 54

def index
  erb :index, :locals => {
    :branches => grit.branches,
    :tags => grit.tags
  }
end

#modelObject



46
47
48
# File 'lib/gitgo/controllers/code.rb', line 46

def model
  Comment
end

#show_blob(treeish, path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gitgo/controllers/code.rb', line 124

def show_blob(treeish, path)
  commit = grit.commit(treeish) || not_found
  blob = commit.tree / path || not_found

  erb :blob, :locals => {
    :commit => commit, 
    :treeish => treeish, 
    :blob => blob, 
    :path => path
  }
end

#show_commit(treeish) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/gitgo/controllers/code.rb', line 151

def show_commit(treeish)
  commit = grit.commit(treeish) || not_found
  erb :diff, :locals => {
    :commit => commit, 
    :treeish => treeish
  }
end

#show_commits(treeish) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gitgo/controllers/code.rb', line 159

def show_commits(treeish)
  commit = grit.commit(treeish)
  page = (request[:page] || 0).to_i
  per_page = (request[:per_page] || 10).to_i

  erb :commits, :locals => {
    :treeish => treeish,
    :page => page,
    :per_page => per_page,
    :commits => grit.commits(commit.sha, per_page, page * per_page)
  }
end

#show_object(sha) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gitgo/controllers/code.rb', line 172

def show_object(sha)
  sha = git.resolve(sha)
  
  case
  when request['content'] == 'true'
    response['Content-Type'] = 'text/plain'
    grit.git.cat_file({:p => true}, sha)
    
  when request['download'] == 'true'
    response['Content-Type'] = 'text/plain'
    response['Content-Disposition'] = "attachment; filename=#{sha};"
    raw_object = grit.git.ruby_git.get_raw_object_by_sha1(sha)
    "%s %d\0" % [raw_object.type, raw_object.content.length] + raw_object.content
    
  else
    type = git.type(sha).to_sym
    obj = git.get(type, sha) or not_found
    
    erb type, :locals => {
      :sha => sha, 
      :obj => obj
    }, :views => path('views/code/obj')
  end
end

#show_tree(treeish, path) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gitgo/controllers/code.rb', line 136

def show_tree(treeish, path)
  commit = grit.commit(treeish) || not_found
  tree = path.split("/").inject(commit.tree) do |obj, name|
    not_found if obj.nil?
    obj.trees.find {|obj| obj.name == name }
  end

  erb :tree, :locals => {
    :commit => commit, 
    :treeish => treeish, 
    :tree => tree, 
    :path => path
  }
end

#tree_grepObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gitgo/controllers/code.rb', line 89

def tree_grep
  options = grep_opts(:e => request['pattern'])
  
  selected = []
  git.tree_grep(options, treeish) do |path, blob|
    selected << [path, blob.id]
  end
  
  erb :grep, :locals => options.merge!(
    :type => 'tree',
    :at => treeish,
    :selected => selected,
    :refs => grit.refs
  )
end

#treeishObject



61
62
63
# File 'lib/gitgo/controllers/code.rb', line 61

def treeish
  request['at'] || grit.head.commit
end