class Gollum::File

Constants

Does the filesystem support reading symlinks?

Attributes

on_disk[RW]

Public: Whether the file can be read from disk.

path[R]

Public: The path of the page within the repo.

Returns the String path.

version[RW]

Public: The Gollum::Git::Commit version of the file.

Public Class Methods

find(wiki, path, version, try_on_disk = false, global_match = false) click to toggle source

Find a file in the given Gollum wiki.

wiki - The wiki. path - The full String path. version - The String version ID to find. try_on_disk - If true, try to return just a reference to a file

that exists on the disk.

global_match - If true, find a File matching path's filename, but not it's directory (so anywhere in the repo)

Returns a Gollum::File or nil if the file could not be found. Note that if you specify try_on_disk=true, you may or may not get a file for which on_disk? is actually true.

# File lib/gollum-lib/file.rb, line 49
def self.find(wiki, path, version, try_on_disk = false, global_match = false)
  map = wiki.tree_map_for(version.to_s)

  query_path = Pathname.new(::File.join(['/', wiki.page_file_dir, path].compact)).cleanpath.to_s
  query_path.sub!(/^\/\//, '/') if Gem.win_platform? # On Windows, Pathname#cleanpath will leave double slashes at the start of a path intact, so sub them out.

  begin
    entry = map.detect do |entry|
      path_match(query_path, entry, global_match, wiki.hyphened_tag_lookup, wiki.case_insensitive_tag_lookup)
    end
    entry ? self.new(wiki, entry.blob(wiki.repo), entry.dir, version, try_on_disk) : nil
  rescue Gollum::Git::NoSuchShaFound
    nil
  end
end
new(wiki, blob, path, version, try_on_disk = false) click to toggle source

Public: Initialize a file.

wiki - The Gollum::Wiki blob - The Gollum::Git::Blob path - The String path version - The String SHA or Gollum::Git::Commit version try_on_disk - If true, try to get an on disk reference for this file.

Returns a newly initialized Gollum::File.

# File lib/gollum-lib/file.rb, line 74
def initialize(wiki, blob, path, version, try_on_disk = false)
  @wiki         = wiki
  @blob         = blob
  @path         = "#{path}/#{blob.name}"[1..-1]
  @version      = version.is_a?(Gollum::Git::Commit) ? version : @wiki.commit_for(version)
  get_disk_reference if try_on_disk
end
path_compare(query, match_path, hyphened_tags, case_insensitive) click to toggle source

For use with self.path_match: returns true if 'query' and 'match_path' match, strictly or taking account of the following parameters: hyphened_tags - If true, replace spaces in match_path with hyphens. case_insensitive - If true, compare query and match_path case-insensitively

# File lib/gollum-lib/file.rb, line 25
def path_compare(query, match_path, hyphened_tags, case_insensitive)
  if hyphened_tags
    final_query = query.gsub(' ', '-')
    final_match = match_path.gsub(' ', '-')
  else
    final_query = query
    final_match = match_path
  end
  final_match.send(case_insensitive ? :casecmp? : :==, final_query)
end
path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false) click to toggle source

For use with self.find: returns true if the given query corresponds to the in-repo path of the BlobEntry.

query - The String path to match. entry - The BlobEntry to check against. global_match - (Not implemented for File, see Page.path_match) hyphened_tags - If true, replace spaces in match_path with hyphens. case_insensitive - If true, compare query and match_path case-insensitively

# File lib/gollum-lib/file.rb, line 18
def path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false)
  path_compare(query, ::File.join('/', entry.path), hyphened_tags, case_insensitive)
end
protected_files() click to toggle source
# File lib/gollum-lib/file.rb, line 157
def self.protected_files
  ['custom.css', 'custom.js', '.redirects.gollum']
end

Public Instance Methods

escaped_url_path() click to toggle source

Public: The url_path, but URL encoded.

Returns the String url_path

# File lib/gollum-lib/file.rb, line 119
def escaped_url_path
  ERB::Util.url_encode(self.url_path).gsub('%2F', '/').force_encoding('utf-8')
end
filename() click to toggle source

Public: The on-disk filename of the page including extension.

Returns the String name.

# File lib/gollum-lib/file.rb, line 103
def filename
  @blob && @blob.name
end
Also aliased as: name
mime_type() click to toggle source

Public: The String mime type of the file.

# File lib/gollum-lib/file.rb, line 153
def mime_type
  @blob && @blob.mime_type
end
name()
Alias for: filename
on_disk?() click to toggle source

Public: Is this an on-disk file reference?

Returns true if this is a pointer to an on-disk file

# File lib/gollum-lib/file.rb, line 141
def on_disk?
  !!@on_disk
end
on_disk_path() click to toggle source

Public: The path to this file on disk

Returns nil if on_disk? is false.

# File lib/gollum-lib/file.rb, line 148
def on_disk_path
  @on_disk_path
end
raw_data() click to toggle source

Public: The raw contents of the file.

Returns the String data.

# File lib/gollum-lib/file.rb, line 126
def raw_data
  return IO.read(@on_disk_path) if on_disk?
  return nil unless @blob

  if !@wiki.repo.bare && @blob.is_symlink
    new_path = @blob.symlink_target(::File.join(@wiki.repo.path, '..', self.path))
    return IO.read(new_path) if new_path
  end

  @blob.data
end
sha() click to toggle source

Public: The SHA hash identifying this page

Returns the String SHA.

# File lib/gollum-lib/file.rb, line 96
def sha
  @blob && @blob.id
end
url_path() click to toggle source

Public: The url path required to reach this file within the repo.

Returns the String url_path

# File lib/gollum-lib/file.rb, line 111
def url_path
  # Chop off the page_file_dir and first slash if necessary
  @wiki.page_file_dir ? self.path[@wiki.page_file_dir.length+1..-1] : self.path
end

Private Instance Methods

get_disk_reference() click to toggle source

Return the file path to this file on disk, if available.

Returns nil if the file isn't available on disk. This can occur if the repo is bare, if the commit isn't the HEAD, or if there are problems resolving symbolic links.

# File lib/gollum-lib/file.rb, line 168
def get_disk_reference
  return false if @wiki.repo.bare
  return false if @version.sha != @wiki.repo.head.commit.sha
  return false if @blob.is_symlink && !FS_SUPPORT_SYMLINKS

  # This will try to resolve symbolic links, as well
  pathname = Pathname.new(::File.join(@wiki.repo.path, '..', BlobEntry.normalize_dir(@path)))
  if pathname.symlink?
    source   = ::File.readlink(pathname.to_path)
    realpath = ::File.join(::File.dirname(pathname.to_path), source)
    return false unless realpath && ::File.exist?(realpath)
    @on_disk_path = realpath.to_s
  else
    @on_disk_path = pathname.to_path
  end
  @on_disk = true
end