BigW Consortium Gitlab

tree_helper.rb 3.58 KB
Newer Older
1
module TreeHelper
2 3 4 5
  # Sorts a repository's tree so that folders are before files and renders
  # their corresponding partials
  #
  # contents - A Grit::Tree object for the current tree
6
  def render_tree(tree)
7
    # Render Folders before Files/Submodules
8
    folders, files, submodules = tree.trees, tree.blobs, tree.submodules
9 10 11 12

    tree = ""

    # Render folders if we have any
13 14
    tree << render(partial: 'projects/tree/tree_item', collection: folders,
                   locals: { type: 'folder' }) if folders.present?
15

16
    # Render files if we have any
17 18
    tree << render(partial: 'projects/tree/blob_item', collection: files,
                   locals: { type: 'file' }) if files.present?
19 20

    # Render submodules if we have any
21 22
    tree << render(partial: 'projects/tree/submodule_item',
                   collection: submodules) if submodules.present?
23 24

    tree.html_safe
25 26
  end

27
  def render_readme(readme)
28
    render_markup(readme.name, readme.data)
29 30
  end

31
  # Return an image icon depending on the file type and mode
32 33
  #
  # type - String type of the tree item; either 'folder' or 'file'
34 35 36 37
  # mode - File unix mode
  # name - File name
  def tree_icon(type, mode, name)
    icon("#{file_type_icon_class(type, mode, name)} fw")
38 39
  end

40 41
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
42
  end
43

44 45 46 47
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
48

49 50 51 52
  def on_top_of_branch?(project = @project, ref = @ref)
    project.repository.branch_names.include?(ref)
  end

53
  def can_edit_tree?(project = nil, ref = nil)
54
    project ||= @project
55
    ref ||= @ref
56

57 58
    return false unless on_top_of_branch?(project, ref)

59
    can_collaborate_with_project?(project)
60
  end
61

62
  def tree_edit_branch(project = @project, ref = @ref)
63 64 65 66 67 68
    return unless can_edit_tree?(project, ref)

    if can_push_branch?(project, ref)
      ref
    else
      project = tree_edit_project(project)
69
      project.repository.next_branch('patch')
70 71 72 73 74 75 76 77
    end
  end

  def tree_edit_project(project = @project)
    if can?(current_user, :push_code, project)
      project
    elsif current_user && current_user.already_forked?(project)
      current_user.fork_of(project)
78
    end
79 80
  end

81 82 83 84 85 86 87 88 89 90 91 92 93 94
  def edit_in_new_fork_notice_now
    "You're not allowed to make changes to this project directly." +
      " A fork of this project is being created that you can make changes in, so you can submit a merge request."
  end

  def edit_in_new_fork_notice
    "You're not allowed to make changes to this project directly." +
      " A fork of this project has been created that you can make changes in, so you can submit a merge request."
  end

  def commit_in_fork_help
    "A new branch will be created in your fork and a new merge request will be started."
  end

95
  def tree_breadcrumbs(tree, max_links = 2)
96
    if @path.present?
97
      part_path = ""
98
      parts = @path.split('/')
99 100

      yield('..', nil) if parts.count > max_links
101

102 103 104
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
105

106
        next if parts.count > max_links && !parts.last(2).include?(part)
107
        yield(part, tree_join(@ref, part_path))
108 109
      end
    end
110
  end
111

112
  def up_dir_path
113 114
    file = File.join(@path, "..")
    tree_join(@ref, file)
115
  end
116

Steven Burgart committed
117
  # returns the relative path of the first subdir that doesn't have only one directory descendant
118 119 120 121 122 123 124 125
  def flatten_tree(tree)
    subtree = Gitlab::Git::Tree.where(@repository, @commit.id, tree.path)
    if subtree.count == 1 && subtree.first.dir?
      return tree_join(tree.name, flatten_tree(subtree.first))
    else
      return tree.name
    end
  end
126
end