BigW Consortium Gitlab

tree_helper.rb 2.71 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
    if gitlab_markdown?(readme.name)
29
      preserve(markdown(readme.data))
30
    elsif markup?(readme.name)
31 32 33 34 35 36
      render_markup(readme.name, readme.data)
    else
      simple_format(readme.data)
    end
  end

37 38 39 40
  # Return an image icon depending on the file type
  #
  # type - String type of the tree item; either 'folder' or 'file'
  def tree_icon(type)
41
    icon_class = if type == 'folder'
42
                   'fa fa-folder'
43
                 else
44
                   'fa fa-file-o'
45 46 47
                 end

    content_tag :i, nil, class: icon_class
48 49
  end

50 51
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
52
  end
53

54 55 56 57
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
58

59 60 61 62
  def allowed_tree_edit?(project = nil, ref = nil)
    project ||= @project
    ref ||= @ref
    return false unless project.repository.branch_names.include?(ref)
63

64
    ::Gitlab::GitAccess.can_push_to_branch?(current_user, project, ref)
65 66
  end

67
  def tree_breadcrumbs(tree, max_links = 2)
68
    if @path.present?
69
      part_path = ""
70
      parts = @path.split('/')
71 72

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

74 75 76
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
77

78
        next unless parts.last(2).include?(part) if parts.count > max_links
79
        yield(part, tree_join(@ref, part_path))
80 81
      end
    end
82
  end
83

84
  def up_dir_path
85 86
    file = File.join(@path, "..")
    tree_join(@ref, file)
87
  end
88

Steven Burgart committed
89
  # returns the relative path of the first subdir that doesn't have only one directory descendant
90 91 92 93 94 95 96 97
  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
98
end