class Gitdocs::Repository::Path

Constants

DirEntry

Attributes

relative_path[R]

Public Class Methods

new(repository, relative_path) click to toggle source

@param [Gitdocs::Repository] repository @param [String] relative_path

# File lib/gitdocs/repository/path.rb, line 11
def initialize(repository, relative_path)
  @repository    = repository
  @relative_path = relative_path.gsub(%r{^/}, '')
  @absolute_path = File.join(
    File.absolute_path(@repository.root), @relative_path
  )
end

Public Instance Methods

absolute_path(ref = nil) click to toggle source
# File lib/gitdocs/repository/path.rb, line 101
def absolute_path(ref = nil)
  return @absolute_path unless ref

  blob    = @repository.blob_at(@relative_path, ref)
  content = blob ? blob.text : ''
  tmp_path = File.expand_path(File.basename(@relative_path), Dir.tmpdir)
  File.open(tmp_path, 'w') { |f| f.puts content }
  tmp_path
end
content() click to toggle source
# File lib/gitdocs/repository/path.rb, line 131
def content
  return nil unless File.file?(@absolute_path)
  File.read(@absolute_path)
end
directory?() click to toggle source
# File lib/gitdocs/repository/path.rb, line 97
def directory?
  File.directory?(@absolute_path)
end
exist?() click to toggle source
# File lib/gitdocs/repository/path.rb, line 93
def exist?
  File.exist?(@absolute_path)
end
file_listing() click to toggle source

@return [Array<DirEntry>] entries in the directory

* excluding any git related directories
* sorted by filename, ignoring any leading '.'s
# File lib/gitdocs/repository/path.rb, line 121
def file_listing
  return nil unless directory?

  Dir
    .glob(File.join(@absolute_path, '{*,.*}'))
    .reject  { |x| x.match(%r{/\.(\.|git|gitignore|gitmessage~)?$}) }
    .sort_by { |x| File.basename(x).sub(/^\./, '') }
    .map     { |x| DirEntry.new(File.basename(x), File.directory?(x)) }
end
join(path_fragment) click to toggle source
# File lib/gitdocs/repository/path.rb, line 26
def join(path_fragment)
  @relative_path = File.join(@relative_path, path_fragment)
  @absolute_path = File.join(@absolute_path, path_fragment)
end
meta() click to toggle source

Returns file meta data based on relative file path

@example

meta
=> { :author => "Nick", :size => 1000, :modified => ... }

@raise [RuntimeError] if the file is not found in any commits

@return [Hash<:author => String, :size => Integer, modified => Time>]

# File lib/gitdocs/repository/path.rb, line 80
def meta
  commit = @repository.last_commit_for(@relative_path)

  # FIXME: This should actually just return an empty hash
  fail("File #{@relative_path} not found") unless commit

  {
    author:   commit.author[:name],
    size:     total_size,
    modified: commit.author[:time]
  }
end
mkdir() click to toggle source

Create the path as a directory.

# File lib/gitdocs/repository/path.rb, line 47
def mkdir
  FileUtils.mkdir_p(@absolute_path)
end
mv(filename) click to toggle source

Move file to the repository path @param [String] filename

# File lib/gitdocs/repository/path.rb, line 53
def mv(filename)
  FileUtils.mkdir_p(File.dirname(@absolute_path))
  FileUtils.mv(filename, @absolute_path)
end
readme_path() click to toggle source
# File lib/gitdocs/repository/path.rb, line 111
def readme_path
  return nil unless directory?
  Dir.glob(File.join(@absolute_path, 'README.{md}')).first
end
relative_dirname() click to toggle source

@return [String]

# File lib/gitdocs/repository/path.rb, line 20
def relative_dirname
  result = File.dirname(@relative_path)
  return '' if result == '.'
  result
end
remove() click to toggle source

Remove the path, but only if it is a file.

# File lib/gitdocs/repository/path.rb, line 59
def remove
  return nil unless File.file?(@absolute_path)
  FileUtils.rm(@absolute_path)
end
revert(ref) click to toggle source

Revert file to the specified ref

@param [String] ref

# File lib/gitdocs/repository/path.rb, line 155
def revert(ref)
  return unless ref

  blob = @repository.blob_at(@relative_path, ref)
  # Silently fail if the file/ref do not existing in the repository.
  # Which is consistent with the original behaviour.
  # TODO: should consider throwing an exception on this condition
  return unless blob

  write(blob.text)
end
revisions() click to toggle source

Returns the revisions available for a particular file

@param [String] file

@return [Array<Hash>]

# File lib/gitdocs/repository/path.rb, line 141
def revisions
  @repository.commits_for(@relative_path, 100).map do |commit|
    {
      commit:  commit.oid[0, 7],
      subject: commit.message.split("\n")[0],
      author:  commit.author[:name],
      date:    commit.author[:time]
    }
  end
end
text?() click to toggle source

@return [Boolean]

# File lib/gitdocs/repository/path.rb, line 65
def text?
  return false unless File.file?(@absolute_path)
  mime_type = File.mime_type?(File.open(@absolute_path))
  !!(mime_type =~ %r{text/|x-empty}) # rubocop:disable DoubleNegation
end
touch() click to toggle source

Touch and path and create any necessary directories.

# File lib/gitdocs/repository/path.rb, line 41
def touch
  FileUtils.mkdir_p(File.dirname(@absolute_path))
  FileUtils.touch(@absolute_path)
end
write(content) click to toggle source

Write the content to the path and create any necessary directories.

@param [String] content @return [void]

# File lib/gitdocs/repository/path.rb, line 35
def write(content)
  FileUtils.mkdir_p(File.dirname(@absolute_path))
  File.open(@absolute_path, 'w') { |f| f.puts(content) }
end

Private Instance Methods

total_size() click to toggle source
# File lib/gitdocs/repository/path.rb, line 171
def total_size
  result =
    if File.directory?(@absolute_path)
      Dir[File.join(@absolute_path, '**', '*')].reduce(0) do |size, filename|
        File.symlink?(filename) ? size : size + File.size(filename)
      end
    else
      File.symlink?(@absolute_path) ? 0 : File.size(@absolute_path)
    end

  # HACK: A value of 0 breaks the table sort for some reason
  return -1 if result == 0
  result
end