Module: ExtractsRef
- Included in:
- ExtractsPath, Snippets::BlobsActions
- Defined in:
- lib/extracts_ref.rb
Overview
Module providing methods for dealing with separating a tree-ish string and a file path string when combined in a request parameter Can be extended for different types of repository object, e.g. Project or Snippet
Constant Summary collapse
- InvalidPathError =
Class.new(StandardError)
Instance Method Summary collapse
-
#assign_ref_vars ⇒ Object
Assigns common instance variables for views working with Git tree-ish objects.
-
#extract_ref(id) ⇒ Object
Given a string containing both a Git tree-ish, such as a branch or tag, and a filesystem path joined by forward slashes, attempts to separate the two.
-
#tree ⇒ Object
rubocop:enable Gitlab/ModuleWithInstanceVariables.
Instance Method Details
#assign_ref_vars ⇒ Object
Assigns common instance variables for views working with Git tree-ish objects
Assignments are:
-
@id - A string representing the joined ref and path
-
@ref - A string representing the ref (e.g., the branch, tag, or commit SHA)
-
@path - A string representing the filesystem path
-
@commit - A Commit representing the commit from the given ref
If the :id parameter appears to be requesting a specific response format, that will be handled as well.
rubocop:disable Gitlab/ModuleWithInstanceVariables
64 65 66 67 68 69 70 71 72 |
# File 'lib/extracts_ref.rb', line 64 def assign_ref_vars @id = get_id @ref, @path = extract_ref(@id) @repo = repository_container.repository raise InvalidPathError if @ref.match?(/\s/) @commit = @repo.commit(@ref) if @ref.present? end |
#extract_ref(id) ⇒ Object
Given a string containing both a Git tree-ish, such as a branch or tag, and a filesystem path joined by forward slashes, attempts to separate the two.
Expects a repository_container method that returns the active repository object. This is used to check the input against a list of valid repository refs.
Examples
# No repository_container available
extract_ref('master')
# => ['', '']
extract_ref('master')
# => ['master', '']
extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG")
# => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG']
extract_ref("v2.0.0/README.md")
# => ['v2.0.0', 'README.md']
extract_ref('master/app/models/project.rb')
# => ['master', 'app/models/project.rb']
extract_ref('issues/1234/app/models/project.rb')
# => ['issues/1234', 'app/models/project.rb']
# Given an invalid branch, we fall back to just splitting on the first slash
extract_ref('non/existent/branch/README.md')
# => ['non', 'existent/branch/README.md']
Returns an Array where the first value is the tree-ish and the second is the path
42 43 44 45 46 47 48 49 |
# File 'lib/extracts_ref.rb', line 42 def extract_ref(id) pair = extract_raw_ref(id) [ pair[0].strip, pair[1].delete_prefix('/').delete_suffix('/') ] end |
#tree ⇒ Object
rubocop:enable Gitlab/ModuleWithInstanceVariables
75 76 77 |
# File 'lib/extracts_ref.rb', line 75 def tree @tree ||= @repo.tree(@commit.id, @path) # rubocop:disable Gitlab/ModuleWithInstanceVariables end |