BigW Consortium Gitlab

tree_helper.rb 3.17 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
  # Return an image icon depending on the file type and mode
16 17
  #
  # type - String type of the tree item; either 'folder' or 'file'
18 19 20 21
  # mode - File unix mode
  # name - File name
  def tree_icon(type, mode, name)
    icon("#{file_type_icon_class(type, mode, name)} fw")
22 23
  end

24 25
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
26
  end
27

28 29 30 31
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
32

33
  def on_top_of_branch?(project = @project, ref = @ref)
34
    project.repository.branch_exists?(ref)
35 36
  end

37
  def can_edit_tree?(project = nil, ref = nil)
38
    project ||= @project
39
    ref ||= @ref
40

41 42
    return false unless on_top_of_branch?(project, ref)

43
    can_collaborate_with_project?(project)
44
  end
45

46
  def tree_edit_branch(project = @project, ref = @ref)
47 48 49 50 51 52
    return unless can_edit_tree?(project, ref)

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

65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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

79
  def path_breadcrumbs(max_links = 6)
80
    if @path.present?
81
      part_path = ""
82
      parts = @path.split('/')
83

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

86 87 88
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
89

90
        next if parts.count > max_links && !parts.last(2).include?(part)
91
        yield(part, part_path)
92 93
      end
    end
94
  end
95

96
  def up_dir_path
97 98
    file = File.join(@path, "..")
    tree_join(@ref, file)
99
  end
100

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