BigW Consortium Gitlab

tree_helper.rb 3.5 KB
Newer Older
1
module TreeHelper
2 3
  FILE_LIMIT = 1_000

4 5 6
  # Sorts a repository's tree so that folders are before files and renders
  # their corresponding partials
  #
7
  # tree - A `Tree` object for the current tree
8
  def render_tree(tree)
9
    # Sort submodules and folders together by name ahead of files
10
    folders, files, submodules = tree.trees, tree.blobs, tree.submodules
11
    tree = ''
12
    items = (folders + submodules).sort_by(&:name) + files
13 14 15 16 17 18 19 20

    if items.size > FILE_LIMIT
      tree << render(partial: 'projects/tree/truncated_notice_tree_row',
                     locals: { limit: FILE_LIMIT, total: items.size })
      items = items.take(FILE_LIMIT)
    end

    tree << render(partial: 'projects/tree/tree_row', collection: items) if items.present?
21
    tree.html_safe
22 23
  end

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

33 34
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
35
  end
36

37 38 39 40
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
41

42
  def on_top_of_branch?(project = @project, ref = @ref)
43
    project.repository.branch_exists?(ref)
44 45
  end

46
  def can_edit_tree?(project = nil, ref = nil)
47
    project ||= @project
48
    ref ||= @ref
49

50 51
    return false unless on_top_of_branch?(project, ref)

52
    can_collaborate_with_project?(project)
53
  end
54

55
  def tree_edit_branch(project = @project, ref = @ref)
56 57 58 59 60 61
    return unless can_edit_tree?(project, ref)

    if can_push_branch?(project, ref)
      ref
    else
      project = tree_edit_project(project)
62
      project.repository.next_branch('patch')
63 64 65 66 67 68 69 70
    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)
71
    end
72 73
  end

74 75 76 77 78 79 80 81 82 83 84 85 86 87
  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

88
  def path_breadcrumbs(max_links = 6)
89
    if @path.present?
90
      part_path = ""
91
      parts = @path.split('/')
92

93
      yield('..', File.join(*parts.first(parts.count - 2))) if parts.count > max_links
94

95 96 97
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
98

99
        next if parts.count > max_links && !parts.last(2).include?(part)
100

101
        yield(part, part_path)
102 103
      end
    end
104
  end
105

106
  def up_dir_path
107 108
    file = File.join(@path, "..")
    tree_join(@ref, file)
109
  end
110

Steven Burgart committed
111
  # returns the relative path of the first subdir that doesn't have only one directory descendant
112 113 114
  def flatten_tree(root_path, tree)
    return tree.flat_path.sub(/\A#{root_path}\//, '') if tree.flat_path.present?

115 116
    subtree = Gitlab::Git::Tree.where(@repository, @commit.id, tree.path)
    if subtree.count == 1 && subtree.first.dir?
117
      return tree_join(tree.name, flatten_tree(root_path, subtree.first))
118 119 120 121
    else
      return tree.name
    end
  end
122
end