projects_controller.rb 3.65 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1 2
require File.join(Rails.root, 'lib', 'graph_commit')

gitlabhq's avatar
gitlabhq committed
3
class ProjectsController < ApplicationController
Nihad Abbasov's avatar
Nihad Abbasov committed
4
  before_filter :project, :except => [:index, :new, :create]
gitlabhq's avatar
gitlabhq committed
5
  layout :determine_layout
gitlabhq's avatar
gitlabhq committed
6 7 8

  # Authorize
  before_filter :add_project_abilities
Nihad Abbasov's avatar
Nihad Abbasov committed
9 10
  before_filter :authorize_read_project!, :except => [:index, :new, :create]
  before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy]
gitlabhq's avatar
gitlabhq committed
11
  before_filter :require_non_empty_project, :only => [:blob, :tree]
12
  before_filter :load_refs, :only => :tree # load @branch, @tag & @ref
gitlabhq's avatar
gitlabhq committed
13

gitlabhq's avatar
gitlabhq committed
14
  def index
15 16 17
    source = current_user.projects
    source = source.tagged_with(params[:tag]) unless params[:tag].blank?
    @projects = source.all
gitlabhq's avatar
gitlabhq committed
18 19 20 21 22 23 24 25 26 27 28 29 30
  end

  def new
    @project = Project.new
  end

  def edit
  end

  def create
    @project = Project.new(params[:project])
    @project.owner = current_user

Nihad Abbasov's avatar
Nihad Abbasov committed
31
    Project.transaction do
gitlabhq's avatar
gitlabhq committed
32 33 34 35 36 37 38
      @project.save!
      @project.users_projects.create!(:admin => true, :read => true, :write => true, :user => current_user)
    end

    respond_to do |format|
      if @project.valid?
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
Nihad Abbasov's avatar
Nihad Abbasov committed
39
        format.js
gitlabhq's avatar
gitlabhq committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
      else
        format.html { render action: "new" }
        format.js
      end
    end
  rescue Gitosis::AccessDenied
    render :js => "location.href = '#{errors_gitosis_path}'" and return
  rescue StandardError => ex
    @project.errors.add(:base, "Cant save project. Please try again later")
    respond_to do |format|
      format.html { render action: "new" }
      format.js
    end
  end
gitlabhq's avatar
gitlabhq committed
54

gitlabhq's avatar
gitlabhq committed
55
  def update
gitlabhq's avatar
gitlabhq committed
56
    respond_to do |format|
gitlabhq's avatar
gitlabhq committed
57
      if project.update_attributes(params[:project])
gitlabhq's avatar
gitlabhq committed
58
        format.html { redirect_to project, :notice => 'Project was successfully updated.' }
Nihad Abbasov's avatar
Nihad Abbasov committed
59
        format.js
gitlabhq's avatar
gitlabhq committed
60 61
      else
        format.html { render action: "edit" }
Nihad Abbasov's avatar
Nihad Abbasov committed
62
        format.js
gitlabhq's avatar
gitlabhq committed
63
      end
gitlabhq's avatar
gitlabhq committed
64 65 66 67
    end
  end

  def show
gitlabhq's avatar
gitlabhq committed
68
    return render "projects/empty" unless @project.repo_exists?
69 70
    limit = (params[:limit] || 40).to_i
    @activities = @project.updates(limit)
gitlabhq's avatar
gitlabhq committed
71 72
  end

gitlabhq's avatar
gitlabhq committed
73 74 75
  #
  # Wall
  #
76

gitlabhq's avatar
gitlabhq committed
77 78
  def wall
    @note = Note.new
gitlabhq's avatar
gitlabhq committed
79
    @notes = @project.common_notes.order("created_at DESC")
gitlabhq's avatar
gitlabhq committed
80
    @notes = @notes.fresh.limit(20)
gitlabhq's avatar
gitlabhq committed
81

82
    respond_to do |format|
gitlabhq's avatar
gitlabhq committed
83
      format.html
84
      format.js { respond_with_notes }
gitlabhq's avatar
gitlabhq committed
85
    end
gitlabhq's avatar
gitlabhq committed
86
  end
87

Valery Sizov's avatar
Valery Sizov committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  def graph
    @repo = project.repo
    commits = Grit::Commit.find_all(@repo, nil, {:max_count => 650})
    ref_cache = {}
    commits.collect! do |commit|
      add_refs(commit, ref_cache)
      GraphCommit.new(commit)
    end

    days = GraphCommit.index_commits(commits)
    @days_json = days.compact.collect{|d| [d.day, d.strftime("%b")] }.to_json
    @commits_json = commits.collect do |c|
      h = {}
      h[:parents] = c.parents.collect do |p|
        [p.id,0,0]
      end
      h[:author] = c.author.name.force_encoding("UTF-8")
      h[:time] = c.time
      h[:space] = c.space
      h[:refs] = c.refs.collect{|r|r.name}.join(" ") unless c.refs.nil?
      h[:id] = c.sha
      h[:date] = c.date
      h[:message] = c.message.force_encoding("UTF-8")
Valery Sizov's avatar
Valery Sizov committed
111
      h[:login] = c.author.email
Valery Sizov's avatar
Valery Sizov committed
112 113 114 115
      h
    end.to_json
  end

gitlabhq's avatar
gitlabhq committed
116 117 118 119 120 121 122 123
  def destroy
    project.destroy

    respond_to do |format|
      format.html { redirect_to projects_url }
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
124
  protected
gitlabhq's avatar
gitlabhq committed
125

Valery Sizov's avatar
Valery Sizov committed
126 127
  def add_refs(commit, ref_cache)
    if ref_cache.empty?
128
      @repo.refs.each do |ref|
Valery Sizov's avatar
Valery Sizov committed
129 130 131
        ref_cache[ref.commit.id] ||= []
        ref_cache[ref.commit.id] << ref
      end
Valery Sizov's avatar
Valery Sizov committed
132 133 134 135 136
    end
    commit.refs = ref_cache[commit.id] if ref_cache.include? commit.id
    commit.refs ||= []
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
137
  def project
gitlabhq's avatar
gitlabhq committed
138 139
    @project ||= Project.find_by_code(params[:id])
  end
gitlabhq's avatar
gitlabhq committed
140 141 142 143 144 145 146 147

  def determine_layout
    if @project && !@project.new_record?
      "project"
    else
      "application"
    end
  end
gitlabhq's avatar
gitlabhq committed
148
end