From c463eeb0908e5c00008073c07e71e06434393342 Mon Sep 17 00:00:00 2001 From: gitlabhq Date: Thu, 20 Oct 2011 22:00:00 +0300 Subject: [PATCH 1/5] refactoring --- app/helpers/application_helper.rb | 21 ----------- app/models/snippet.rb | 7 ++++ app/views/commits/_diff.html.haml | 7 ++-- app/views/projects/_tree_file.html.haml | 10 +++--- app/views/snippets/show.html.haml | 3 +- config/initializers/grit_ext.rb | 8 +++++ lib/utils.rb | 47 ++++++++++++++++++++++--- 7 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 config/initializers/grit_ext.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c389fd4a..0895eb0d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -53,25 +53,4 @@ module ApplicationHelper [projects, default_nav, project_nav].flatten.to_json end - def handle_file_type(file_name, mime_type = nil) - if file_name =~ /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/ - :ruby - elsif file_name =~ /\.py$/ - :python - elsif file_name =~ /(\.pl|\.scala|\.c|\.cpp|\.java|\.haml|\.html|\.sass|\.scss|\.xml|\.php|\.erb)$/ - $1[1..-1].to_sym - elsif file_name =~ /\.js$/ - :javascript - elsif file_name =~ /\.sh$/ - :bash - elsif file_name =~ /\.coffee$/ - :coffeescript - elsif file_name =~ /\.yml$/ - :yaml - elsif file_name =~ /\.md$/ - :minid - else - :text - end - end end diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 0a54fee7..119b69a4 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -1,4 +1,6 @@ class Snippet < ActiveRecord::Base + include Utils::Colorize + belongs_to :project belongs_to :author, :class_name => "User" has_many :notes, :as => :noteable @@ -28,6 +30,11 @@ class Snippet < ActiveRecord::Base ".js", ".sh", ".coffee", ".yml", ".md" ] end + + def colorize + ft = handle_file_type(file_name) + Albino.colorize(content, ft, :html, 'utf-8', "linenos=True") + end end # == Schema Information # diff --git a/app/views/commits/_diff.html.haml b/app/views/commits/_diff.html.haml index dff99bf1..73652aaf 100644 --- a/app/views/commits/_diff.html.haml +++ b/app/views/commits/_diff.html.haml @@ -1,4 +1,3 @@ -- require "utils" .file_stats - @commit.diffs.each do |diff| - if diff.deleted_file @@ -35,7 +34,7 @@ %strong{:id => "#{diff.b_path}"}= diff.b_path %br/ .diff_file_content - - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data) + - if file.text? - lines_arr = diff.diff.lines.to_a - line_old = lines_arr[2].match(/-(\d)/)[0].to_i.abs rescue 0 - line_new = lines_arr[2].match(/\+(\d)/)[0].to_i.abs rescue 0 @@ -50,9 +49,9 @@ - else - line_new += 1 - line_old += 1 - - elsif file.mime_type =~ /image/ + - elsif file.image? .diff_file_content_image - %img{:src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"} + %img{:src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"} - else %p %center No preview for this file type diff --git a/app/views/projects/_tree_file.html.haml b/app/views/projects/_tree_file.html.haml index 3463bfc5..ae95d483 100644 --- a/app/views/projects/_tree_file.html.haml +++ b/app/views/projects/_tree_file.html.haml @@ -1,4 +1,3 @@ -- require "utils" .view_file .view_file_header %strong @@ -6,14 +5,13 @@ = link_to "raw", blob_project_path(@project, :commit_id => @commit.id, :path => params[:path] ), :class => "right", :target => "_blank" = link_to "history", project_commits_path(@project, :path => params[:path]), :class => "right", :style => "margin-right:10px;" %br/ - - if file.mime_type =~ /application|text/ && !Utils.binary?(file.data) + - if file.text? .view_file_content - - ft = handle_file_type(file.name, file.mime_type) :erb - <%= raw Albino.colorize(content, ft, :html, 'utf-8', "linenos=True") %> - - elsif file.mime_type =~ /image/ + <%= raw file.colorize %> + - elsif file.image? .view_file_content_image - %img{ :src => "data:image/jpeg;base64,#{Base64.encode64(file.data)}"} + %img{ :src => "data:#{file.mime_type};base64,#{Base64.encode64(file.data)}"} - else %p %center No preview for this file type diff --git a/app/views/snippets/show.html.haml b/app/views/snippets/show.html.haml index bb444efe..899950b7 100644 --- a/app/views/snippets/show.html.haml +++ b/app/views/snippets/show.html.haml @@ -7,9 +7,8 @@ = @snippet.file_name %br/ .view_file_content - - ft = handle_file_type(@snippet.file_name) :erb - <%= raw Albino.colorize(@snippet.content, ft, :html, 'utf-8', "linenos=True") %> + <%= raw @snippet.colorize %> - if can?(current_user, :admin_snippet, @project) || @snippet.author == current_user = link_to 'Edit', edit_project_snippet_path(@project, @snippet), :class => "lbutton positive" diff --git a/config/initializers/grit_ext.rb b/config/initializers/grit_ext.rb new file mode 100644 index 00000000..8f1329a6 --- /dev/null +++ b/config/initializers/grit_ext.rb @@ -0,0 +1,8 @@ +require 'grit' +require 'albino' +require "utils" + +Grit::Blob.class_eval do + include Utils::FileHelper + include Utils::Colorize +end diff --git a/lib/utils.rb b/lib/utils.rb index 6e7460ed..40bf2673 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,8 +1,47 @@ module Utils - def self.binary?(string) - string.each_byte do |x| - x.nonzero? or return true + module FileHelper + def binary?(string) + string.each_byte do |x| + x.nonzero? or return true + end + false + end + + def image? + mime_type =~ /image/ + end + + def text? + mime_type =~ /application|text/ && !binary?(data) + end + end + + module Colorize + def colorize + ft = handle_file_type(name, mime_type) + Albino.colorize(data, ft, :html, 'utf-8', "linenos=True") + end + + def handle_file_type(file_name, mime_type = nil) + if file_name =~ /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/ + :ruby + elsif file_name =~ /\.py$/ + :python + elsif file_name =~ /(\.pl|\.scala|\.c|\.cpp|\.java|\.haml|\.html|\.sass|\.scss|\.xml|\.php|\.erb)$/ + $1[1..-1].to_sym + elsif file_name =~ /\.js$/ + :javascript + elsif file_name =~ /\.sh$/ + :bash + elsif file_name =~ /\.coffee$/ + :coffeescript + elsif file_name =~ /\.yml$/ + :yaml + elsif file_name =~ /\.md$/ + :minid + else + :text + end end - false end end From b5a5e5a416a33147e99be2e8c07e2bcc7fb7ee6c Mon Sep 17 00:00:00 2001 From: gitlabhq Date: Fri, 21 Oct 2011 09:58:28 +0300 Subject: [PATCH 2/5] prepare for 1.1 --- CHANGELOG | 2 ++ VERSION | 2 +- update.rb | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index aa846166..96acfba1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,6 @@ v 1.1.0 + - project dashboard + - wall redesigned - feature: code snippets - fixed horizontal scroll on file preview - fixed app crash if commit message has invalid chars diff --git a/VERSION b/VERSION index 6d7de6e6..9084fa2f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.2 +1.1.0 diff --git a/update.rb b/update.rb index dccb7df3..b81a4088 100644 --- a/update.rb +++ b/update.rb @@ -19,8 +19,8 @@ env = if envs.include?(ARGV[0]) puts yellow "== RAILS ENV | #{env}" current_version = version puts yellow "Your version is #{current_version}" -puts yellow "Check for new version: $ git pull origin" -`git pull origin` # pull from origin +puts yellow "Check for new version: $ git pull origin 1x" +`git pull origin 1x` # pull from origin # latest version if version == current_version From a07923a549d53527442967aac9c4baf9596f3c1f Mon Sep 17 00:00:00 2001 From: gitlabhq Date: Fri, 21 Oct 2011 14:03:34 +0300 Subject: [PATCH 3/5] recent radio button --- app/controllers/projects_controller.rb | 32 ++++++++++++-------------- app/models/note.rb | 1 + app/models/project.rb | 28 ++++++++++++++++++++++ app/views/projects/show.html.haml | 7 ++++-- app/views/projects/wall.html.haml | 5 +++- spec/requests/projects_spec.rb | 2 +- 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index dc94bae2..5ccfe4bb 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -60,24 +60,21 @@ class ProjectsController < ApplicationController end def show - if @project.repo_exists? - @date = case params[:view] - when "week" then Date.today - 7.days - else Date.today - end.at_beginning_of_day + return render "projects/empty" unless @project.repo_exists? + @date = case params[:view] + when "week" then Date.today - 7.days + when "day" then Date.today + else nil + end - @heads = @project.repo.heads - @commits = @heads.map do |h| - @project.repo.log(h.name, nil, :since => @date) - end.flatten.uniq { |c| c.id } - - @commits.sort! do |x, y| - y.committed_date <=> x.committed_date - end + if @date + @date = @date.at_beginning_of_day + @commits = @project.commits_since(@date) @messages = project.notes.since(@date).order("created_at DESC") - else - return render "projects/empty" + else + @commits = @project.fresh_commits + @messages = project.notes.fresh.limit(10) end end @@ -89,11 +86,12 @@ class ProjectsController < ApplicationController @date = case params[:view] when "week" then Date.today - 7.days when "all" then nil - else Date.today + when "day" then Date.today + else nil end @notes = @project.common_notes.order("created_at DESC") - @notes = @notes.since(@date.at_beginning_of_day) if @date + @notes = @date ? @notes.since(@date.at_beginning_of_day) : @notes.fresh.limit(10) @note = Note.new end diff --git a/app/models/note.rb b/app/models/note.rb index 71fd9dcd..e3dabce4 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -24,6 +24,7 @@ class Note < ActiveRecord::Base scope :last_week, where("created_at >= :date", :date => (Date.today - 7.days)) scope :since, lambda { |day| where("created_at >= :date", :date => (day)) } + scope :fresh, order("created_at DESC") mount_uploader :attachment, AttachmentUploader end diff --git a/app/models/project.rb b/app/models/project.rb index f51bd9b3..d70b18e7 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -126,6 +126,34 @@ class Project < ActiveRecord::Base end end + def heads + @heads ||= repo.heads + end + + def fresh_commits + commits = heads.map do |h| + repo.commits(h.name, 10) + end.flatten.uniq { |c| c.id } + + commits.sort! do |x, y| + y.committed_date <=> x.committed_date + end + + commits[0..10] + end + + def commits_since(date) + commits = heads.map do |h| + repo.log(h.name, nil, :since => date) + end.flatten.uniq { |c| c.id } + + commits.sort! do |x, y| + y.committed_date <=> x.committed_date + end + + commits + end + def tree(fcommit, path = nil) fcommit = commit if fcommit == :head tree = fcommit.tree diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml index ff6078c1..85019ecb 100644 --- a/app/views/projects/show.html.haml +++ b/app/views/projects/show.html.haml @@ -1,9 +1,12 @@ %div - %h2.left Recent history + %h2.left History .right = form_tag project_path(@project), :method => :get do .span-2 - = radio_button_tag :view, "day", (params[:view] || "day") == "day", :onclick => "this.form.submit()", :id => "day_view" + = radio_button_tag :view, "recent", (params[:view] || "recent") == "recent", :onclick => "this.form.submit()", :id => "recent_view" + = label_tag "recent_view","Recent" + .span-2 + = radio_button_tag :view, "day", params[:view] == "day", :onclick => "this.form.submit()", :id => "day_view" = label_tag "day_view","Today" .span-2 = radio_button_tag :view, "week", params[:view] == "week", :onclick => "this.form.submit()", :id => "week_view" diff --git a/app/views/projects/wall.html.haml b/app/views/projects/wall.html.haml index d3dcb353..ed22478c 100644 --- a/app/views/projects/wall.html.haml +++ b/app/views/projects/wall.html.haml @@ -4,7 +4,10 @@ .right = form_tag wall_project_path(@project), :method => :get do .span-2 - = radio_button_tag :view, "day", (params[:view] || "day") == "day", :onclick => "this.form.submit()", :id => "day_view" + = radio_button_tag :view, "recent", (params[:view] || "recent") == "recent", :onclick => "this.form.submit()", :id => "recent_view" + = label_tag "recent_view","Recent" + .span-2 + = radio_button_tag :view, "day", params[:view] == "day", :onclick => "this.form.submit()", :id => "day_view" = label_tag "day_view","Today" .span-2 = radio_button_tag :view, "week", params[:view] == "week", :onclick => "this.form.submit()", :id => "week_view" diff --git a/spec/requests/projects_spec.rb b/spec/requests/projects_spec.rb index a1bac062..945c1ea2 100644 --- a/spec/requests/projects_spec.rb +++ b/spec/requests/projects_spec.rb @@ -73,7 +73,7 @@ describe "Projects" do end it "should beahave like dashboard" do - page.should have_content("Recent history") + page.should have_content("History") end end From f2808191893d2d3f158f569627ac6005509ea05b Mon Sep 17 00:00:00 2001 From: gitlabhq Date: Fri, 21 Oct 2011 14:25:42 +0300 Subject: [PATCH 4/5] fixed wall --all option --- app/controllers/projects_controller.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 5ccfe4bb..54d19af7 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -83,16 +83,15 @@ class ProjectsController < ApplicationController # def wall - @date = case params[:view] - when "week" then Date.today - 7.days - when "all" then nil - when "day" then Date.today - else nil - end - - @notes = @project.common_notes.order("created_at DESC") - @notes = @date ? @notes.since(@date.at_beginning_of_day) : @notes.fresh.limit(10) @note = Note.new + @notes = @project.common_notes.order("created_at DESC") + + @notes = case params[:view] + when "week" then @notes.since((Date.today - 7.days).at_beginning_of_day) + when "all" then @notes.all + when "day" then @notes.since(Date.today.at_beginning_of_day) + else @notes.fresh.limit(10) + end end # From 1477a6c8aaecbde1f2b5903cccb267d632212ee4 Mon Sep 17 00:00:00 2001 From: gitlabhq Date: Fri, 21 Oct 2011 15:35:42 +0300 Subject: [PATCH 5/5] moved from albino -> pygments.rb --- Gemfile | 2 +- Gemfile.lock | 15 +++++++-------- app/models/snippet.rb | 3 +-- app/views/projects/_tree_file.html.haml | 1 + app/views/projects/empty.html.erb | 7 ++++--- config/initializers/grit_ext.rb | 2 +- lib/utils.rb | 8 ++++++-- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 523793e7..77a00659 100644 --- a/Gemfile +++ b/Gemfile @@ -15,7 +15,7 @@ gem 'therubyracer' gem 'faker' gem 'seed-fu', :git => 'git://github.com/mbleigh/seed-fu.git' gem "inifile" -gem "albino", :git => "git://github.com/gitlabhq/albino.git" +gem "pygments.rb", "0.2.3" gem "kaminari" gem "thin" gem "git" diff --git a/Gemfile.lock b/Gemfile.lock index 2aa894aa..5ba34459 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,13 +4,6 @@ GIT specs: annotate (2.4.1.beta1) -GIT - remote: git://github.com/gitlabhq/albino.git - revision: 118380924969f3a856659f86ea1f40c1ba7bfcb1 - specs: - albino (1.3.3) - posix-spawn (>= 0.3.6) - GIT remote: git://github.com/gitlabhq/grit.git revision: ff015074ef35bd94cba943f9c0f98e161ab5851c @@ -72,6 +65,7 @@ GEM ZenTest (= 4.5) awesome_print (0.4.0) bcrypt-ruby (3.0.1) + blankslate (2.1.2.4) builder (3.0.0) capybara (1.0.1) mime-types (>= 1.16) @@ -138,6 +132,8 @@ GEM orm_adapter (0.0.5) polyglot (0.3.2) posix-spawn (0.3.6) + pygments.rb (0.2.3) + rubypython (>= 0.5.1) rack (1.3.2) rack-cache (1.0.3) rack (>= 0.4) @@ -189,6 +185,9 @@ GEM ruby-debug-base19 (>= 0.11.19) ruby_core_source (0.1.5) archive-tar-minitar (>= 0.5.2) + rubypython (0.5.1) + blankslate (>= 2.1.2.3) + ffi (~> 1.0.7) rubyzip (0.9.4) sass (3.1.7) sass-rails (3.1.1) @@ -242,7 +241,6 @@ PLATFORMS DEPENDENCIES acts_as_list - albino! annotate! autotest autotest-rails @@ -260,6 +258,7 @@ DEPENDENCIES jquery-rails kaminari launchy + pygments.rb (= 0.2.3) rails (= 3.1.0) rails-footnotes (>= 3.7.5.rc4) rspec-rails diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 119b69a4..95d6a07d 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -32,8 +32,7 @@ class Snippet < ActiveRecord::Base end def colorize - ft = handle_file_type(file_name) - Albino.colorize(content, ft, :html, 'utf-8', "linenos=True") + system_colorize(content, file_name) end end # == Schema Information diff --git a/app/views/projects/_tree_file.html.haml b/app/views/projects/_tree_file.html.haml index ae95d483..41a2287a 100644 --- a/app/views/projects/_tree_file.html.haml +++ b/app/views/projects/_tree_file.html.haml @@ -1,3 +1,4 @@ +:css .view_file .view_file_header %strong diff --git a/app/views/projects/empty.html.erb b/app/views/projects/empty.html.erb index a8917471..4c60facd 100644 --- a/app/views/projects/empty.html.erb +++ b/app/views/projects/empty.html.erb @@ -1,3 +1,4 @@ +<% bash_lexer = Pygments::Lexer[:bash] %>

Git global setup:

@@ -6,7 +7,7 @@ git config --global user.name "#{current_user.name}" git config --global user.email "#{current_user.email}" eos %> - <%= raw Albino.colorize(setup_str, :bash) %> + <%= raw bash_lexer.highlight(setup_str) %>

Next steps:

@@ -21,7 +22,7 @@ git remote add origin #{@project.url_to_repo} git push -u origin master eos %> - <%= raw Albino.colorize(repo_setup_str, :bash) %> + <%= raw bash_lexer.highlight(repo_setup_str) %>

Existing Git Repo?

@@ -31,7 +32,7 @@ git remote add origin #{@project.url_to_repo} git push -u origin master eos %> - <%= raw Albino.colorize(exist_repo_setup_str, :bash) %> + <%= raw bash_lexer.highlight(exist_repo_setup_str) %>

Remove this project?

diff --git a/config/initializers/grit_ext.rb b/config/initializers/grit_ext.rb index 8f1329a6..4bd71003 100644 --- a/config/initializers/grit_ext.rb +++ b/config/initializers/grit_ext.rb @@ -1,5 +1,5 @@ require 'grit' -require 'albino' +require 'pygments' require "utils" Grit::Blob.class_eval do diff --git a/lib/utils.rb b/lib/utils.rb index 40bf2673..e57121a3 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -18,8 +18,12 @@ module Utils module Colorize def colorize - ft = handle_file_type(name, mime_type) - Albino.colorize(data, ft, :html, 'utf-8', "linenos=True") + system_colorize(data, name) + end + + def system_colorize(data, file_name) + ft = handle_file_type(file_name) + Pygments.highlight(data, :lexer => ft, :options => { :encoding => 'utf-8', :linenos => 'True' }) end def handle_file_type(file_name, mime_type = nil)