Module: CommitsHelper

Defined in:
app/helpers/commits_helper.rb

Instance Method Summary collapse

Instance Method Details


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/commits_helper.rb', line 147

def cherry_pick_commit_link(commit, continue_to_path, btn_class: nil)
  return unless current_user

  tooltip = "Cherry-pick this #{commit.change_type_title} in a new merge request"

  if can_collaborate_with_project?
    link_to 'Cherry-pick', '#modal-cherry-pick-commit', 'data-toggle' => 'modal', 'data-container' => 'body', title: tooltip, class: "btn btn-default btn-grouped btn-#{btn_class} has-tooltip"
  elsif can?(current_user, :fork_project, @project)
    continue_params = {
      to: continue_to_path,
      notice: edit_in_new_fork_notice + ' Try to cherry-pick this commit again.',
      notice_now: edit_in_new_fork_notice_now
    }
    fork_path = namespace_project_forks_path(@project.namespace, @project,
      namespace_key: current_user.namespace.id,
      continue: continue_params)

    link_to 'Cherry-pick', fork_path, class: 'btn btn-grouped btn-close', method: :post, 'data-toggle' => 'tooltip', 'data-container' => 'body', title: tooltip
  end
end

Returns a link to the commit author. If the author has a matching user and is a member of the current @project it will link to the team member page. Otherwise it will link to the author email as specified in the commit.

options:

avatar: true will prepend the avatar image
size:   size of the avatar image in px

10
11
12
# File 'app/helpers/commits_helper.rb', line 10

def commit_author_link(commit, options = {})
  commit_person_link(commit, options.merge(source: :author))
end

Returns the sorted alphabetically links to branches, separated by a comma


74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/commits_helper.rb', line 74

def commit_branches_links(project, branches)
  branches.sort.map do |branch|
    link_to(
      namespace_project_tree_path(project.namespace, project, branch)
    ) do
       :span, class: 'label label-gray' do
        icon('code-fork') + ' ' + branch
      end
    end
  end.join(" ").html_safe
end

Just like #author_link but for the committer.


15
16
17
# File 'app/helpers/commits_helper.rb', line 15

def commit_committer_link(commit, options = {})
  commit_person_link(commit, options.merge(source: :committer))
end

#commit_default_branch(project, branches) ⇒ Object

Return Project default branch, if it present in array Else - first branch in array (mb last actual branch)


69
70
71
# File 'app/helpers/commits_helper.rb', line 69

def commit_default_branch(project, branches)
  branches.include?(project.default_branch) ? branches.delete(project.default_branch) : branches.pop
end

Returns the sorted links to tags, separated by a comma


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/commits_helper.rb', line 87

def commit_tags_links(project, tags)
  sorted = VersionSorter.rsort(tags)
  sorted.map do |tag|
    link_to(
      namespace_project_commits_path(project.namespace, project,
                                     project.repository.find_tag(tag).name)
    ) do
       :span, class: 'label label-gray' do
        icon('tag') + ' ' + tag
      end
    end
  end.join(" ").html_safe
end

#commit_to_html(commit, project, inline = true) ⇒ Object


29
30
31
32
# File 'app/helpers/commits_helper.rb', line 29

def commit_to_html(commit, project, inline = true)
  template = inline ? "inline_commit" : "commit"
  render "projects/commits/#{template}", commit: commit, project: project unless commit.nil?
end

#commits_breadcrumbsObject

Breadcrumb links for a Project and, if applicable, a tree path


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
# File 'app/helpers/commits_helper.rb', line 35

def commits_breadcrumbs
  return unless @project && @ref

  # Add the root project link and the arrow icon
  crumbs = (:li) do
    link_to(
      @project.path,
      namespace_project_commits_path(@project.namespace, @project, @ref)
    )
  end

  if @path
    parts = @path.split('/')

    parts.each_with_index do |part, i|
      crumbs << (:li) do
        # The text is just the individual part, but the link needs all the parts before it
        link_to(
          part,
          namespace_project_commits_path(
            @project.namespace,
            @project,
            tree_join(@ref, parts[0..i].join('/'))
          )
        )
      end
    end
  end

  crumbs.html_safe
end

#image_diff_class(diff) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'app/helpers/commits_helper.rb', line 19

def image_diff_class(diff)
  if diff.deleted_file
    "deleted"
  elsif diff.new_file
    "added"
  else
    nil
  end
end

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/commits_helper.rb', line 101

def link_to_browse_code(project, commit)
  if current_controller?(:projects, :commits)
    if @repo.blob_at(commit.id, @path)
      return link_to(
        "Browse File »",
        namespace_project_blob_path(project.namespace, project,
                                    tree_join(commit.id, @path)),
        class: "pull-right"
      )
    elsif @path.present?
      return link_to(
        "Browse Directory »",
        namespace_project_tree_path(project.namespace, project,
                                    tree_join(commit.id, @path)),
        class: "pull-right"
      )
    end
  end
  link_to(
    "Browse Files",
    namespace_project_tree_path(project.namespace, project, commit),
    class: "pull-right"
  )
end

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/helpers/commits_helper.rb', line 126

def revert_commit_link(commit, continue_to_path, btn_class: nil)
  return unless current_user

  tooltip = "Revert this #{commit.change_type_title} in a new merge request"

  if can_collaborate_with_project?
    link_to 'Revert', '#modal-revert-commit', 'data-toggle' => 'modal', 'data-container' => 'body', title: tooltip, class: "btn btn-default btn-grouped btn-#{btn_class} has-tooltip"
  elsif can?(current_user, :fork_project, @project)
    continue_params = {
      to: continue_to_path,
      notice: edit_in_new_fork_notice + ' Try to revert this commit again.',
      notice_now: edit_in_new_fork_notice_now
    }
    fork_path = namespace_project_forks_path(@project.namespace, @project,
      namespace_key: current_user.namespace.id,
      continue: continue_params)

    link_to 'Revert', fork_path, class: 'btn btn-grouped btn-close', method: :post, 'data-toggle' => 'tooltip', 'data-container' => 'body', title: tooltip
  end
end