BigW Consortium Gitlab

tree_helper.rb 2.5 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 allowed_tree_edit?(project = nil, ref = nil)
    project ||= @project
    ref ||= @ref
    return false unless project.repository.branch_names.include?(ref)
53

54
    ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref)
55 56
  end

57
  def tree_breadcrumbs(tree, max_links = 2)
58
    if @path.present?
59
      part_path = ""
60
      parts = @path.split('/')
61 62

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

64 65 66
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
67

68
        next unless parts.last(2).include?(part) if parts.count > max_links
69
        yield(part, tree_join(@ref, part_path))
70 71
      end
    end
72
  end
73

74
  def up_dir_path
75 76
    file = File.join(@path, "..")
    tree_join(@ref, file)
77
  end
78

Steven Burgart committed
79
  # returns the relative path of the first subdir that doesn't have only one directory descendant
80 81 82 83 84 85 86 87
  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
88
end