BigW Consortium Gitlab

tree_helper.rb 3.24 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
    # Sort submodules and folders together by name ahead of files
8
    folders, files, submodules = tree.trees, tree.blobs, tree.submodules
9
    tree = ""
10 11
    items = (folders + submodules).sort_by(&:name) + files
    tree << render(partial: "projects/tree/tree_row", collection: items) if items.present?
12
    tree.html_safe
13 14
  end

15
  def render_readme(readme)
16
    render_markup(readme.name, readme.data)
17 18
  end

19
  # Return an image icon depending on the file type and mode
20 21
  #
  # type - String type of the tree item; either 'folder' or 'file'
22 23 24 25
  # mode - File unix mode
  # name - File name
  def tree_icon(type, mode, name)
    icon("#{file_type_icon_class(type, mode, name)} fw")
26 27
  end

28 29
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
30
  end
31

32 33 34 35
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
36

37 38 39 40
  def on_top_of_branch?(project = @project, ref = @ref)
    project.repository.branch_names.include?(ref)
  end

41
  def can_edit_tree?(project = nil, ref = nil)
42
    project ||= @project
43
    ref ||= @ref
44

45 46
    return false unless on_top_of_branch?(project, ref)

47
    can_collaborate_with_project?(project)
48
  end
49

50
  def tree_edit_branch(project = @project, ref = @ref)
51 52 53 54 55 56
    return unless can_edit_tree?(project, ref)

    if can_push_branch?(project, ref)
      ref
    else
      project = tree_edit_project(project)
57
      project.repository.next_branch('patch')
58 59 60 61 62 63 64 65
    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)
66
    end
67 68
  end

69 70 71 72 73 74 75 76 77 78 79 80 81 82
  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

83
  def tree_breadcrumbs(tree, max_links = 2)
84
    if @path.present?
85
      part_path = ""
86
      parts = @path.split('/')
87 88

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

90 91 92
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
93

94
        next if parts.count > max_links && !parts.last(2).include?(part)
95
        yield(part, tree_join(@ref, part_path))
96 97
      end
    end
98
  end
99

100
  def up_dir_path
101 102
    file = File.join(@path, "..")
    tree_join(@ref, file)
103
  end
104

Steven Burgart committed
105
  # returns the relative path of the first subdir that doesn't have only one directory descendant
106 107 108 109 110 111 112 113
  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
114
end