Class: Gitdocs::Repository::Path
- Inherits:
-
Object
- Object
- Gitdocs::Repository::Path
- Defined in:
- lib/gitdocs/repository/path.rb
Overview
Class for executing File and Git operations on a specific path in the repository.
Defined Under Namespace
Classes: DirEntry
Instance Attribute Summary collapse
-
#relative_path ⇒ Object
readonly
Returns the value of attribute relative_path.
Instance Method Summary collapse
- #absolute_path(ref = nil) ⇒ Object
- #content ⇒ Object
- #directory? ⇒ Boolean
- #exist? ⇒ Boolean
-
#file_listing ⇒ Array<DirEntry>
Entries in the directory * excluding any git related directories * sorted by filename, ignoring any leading ‘.’s.
-
#initialize(repository, relative_path) ⇒ Path
constructor
A new instance of Path.
- #join(path_fragment) ⇒ Object
-
#meta ⇒ Hash<:author => String, :size => Integer, modified => Time>
Returns file meta data based on relative file path.
-
#mkdir ⇒ Object
Create the path as a directory.
- #readme_path ⇒ Object
-
#remove ⇒ Object
Remove the path, but only if it is a file.
-
#revert(ref) ⇒ Object
Revert file to the specified ref.
-
#revisions ⇒ Array<Hash>
Returns the revisions available for a particular file.
- #text? ⇒ Boolean
-
#touch ⇒ Object
Touch and path and create any necessary directories.
-
#write(content, commit_message) ⇒ Object
Write the content to the path and create any necessary directories.
Constructor Details
#initialize(repository, relative_path) ⇒ Path
Returns a new instance of Path.
10 11 12 13 14 15 16 |
# File 'lib/gitdocs/repository/path.rb', line 10 def initialize(repository, relative_path) @repository = repository @relative_path = relative_path.gsub(/^\//, '') @absolute_path = File.join( File.absolute_path(@repository.root), @relative_path ) end |
Instance Attribute Details
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
6 7 8 |
# File 'lib/gitdocs/repository/path.rb', line 6 def relative_path @relative_path end |
Instance Method Details
#absolute_path(ref = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/gitdocs/repository/path.rb', line 88 def absolute_path(ref = nil) return @absolute_path unless ref blob = @repository.blob_at(@relative_path, ref) content = blob ? blob.text : '' tmp_path = File.(File.basename(@relative_path), Dir.tmpdir) File.open(tmp_path, 'w') { |f| f.puts content } tmp_path end |
#content ⇒ Object
117 118 119 120 |
# File 'lib/gitdocs/repository/path.rb', line 117 def content return nil unless File.file?(@absolute_path) File.read(@absolute_path) end |
#directory? ⇒ Boolean
84 85 86 |
# File 'lib/gitdocs/repository/path.rb', line 84 def directory? File.directory?(@absolute_path) end |
#exist? ⇒ Boolean
80 81 82 |
# File 'lib/gitdocs/repository/path.rb', line 80 def exist? File.exist?(@absolute_path) end |
#file_listing ⇒ Array<DirEntry>
Returns entries in the directory
-
excluding any git related directories
-
sorted by filename, ignoring any leading ‘.’s.
108 109 110 111 112 113 114 115 |
# File 'lib/gitdocs/repository/path.rb', line 108 def file_listing return nil unless directory? Dir.glob(File.join(@absolute_path, '{*,.*}')) .reject { |x| x.match(/\/\.(\.|git|gitignore|gitmessage~)?$/) } .sort_by { |x| File.basename(x).sub(/^\./, '') } .map { |x| DirEntry.new(File.basename(x), File.directory?(x)) } end |
#join(path_fragment) ⇒ Object
18 19 20 21 |
# File 'lib/gitdocs/repository/path.rb', line 18 def join(path_fragment) @relative_path = File.join(@relative_path, path_fragment) @absolute_path = File.join(@absolute_path, path_fragment) end |
#meta ⇒ Hash<:author => String, :size => Integer, modified => Time>
Returns file meta data based on relative file path
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gitdocs/repository/path.rb', line 67 def 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.[:name], size: total_size, modified: commit.[:time] } end |
#mkdir ⇒ Object
Create the path as a directory.
41 42 43 |
# File 'lib/gitdocs/repository/path.rb', line 41 def mkdir FileUtils.mkdir_p(@absolute_path) end |
#readme_path ⇒ Object
98 99 100 101 |
# File 'lib/gitdocs/repository/path.rb', line 98 def readme_path return nil unless directory? Dir.glob(File.join(@absolute_path, 'README.{md}')).first end |
#remove ⇒ Object
Remove the path, but only if it is a file.
46 47 48 49 |
# File 'lib/gitdocs/repository/path.rb', line 46 def remove return nil unless File.file?(@absolute_path) FileUtils.rm(@absolute_path) end |
#revert(ref) ⇒ Object
Revert file to the specified ref
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gitdocs/repository/path.rb', line 141 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, "Reverting '#{@relative_path}' to #{ref}") end |
#revisions ⇒ Array<Hash>
Returns the revisions available for a particular file
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/gitdocs/repository/path.rb', line 127 def revisions @repository.commits_for(@relative_path, 100).map do |commit| { commit: commit.oid[0, 7], subject: commit..split("\n")[0], author: commit.[:name], date: commit.[:time] } end end |
#text? ⇒ Boolean
52 53 54 55 56 |
# File 'lib/gitdocs/repository/path.rb', line 52 def text? return false unless File.file?(@absolute_path) mime_type = File.mime_type?(File.open(@absolute_path)) !!(mime_type =~ /text\/|x-empty/) # rubocop:disable DoubleNegation end |
#touch ⇒ Object
Touch and path and create any necessary directories.
35 36 37 38 |
# File 'lib/gitdocs/repository/path.rb', line 35 def touch FileUtils.mkdir_p(File.dirname(@absolute_path)) FileUtils.touch(@absolute_path) end |
#write(content, commit_message) ⇒ Object
Write the content to the path and create any necessary directories.
27 28 29 30 31 32 |
# File 'lib/gitdocs/repository/path.rb', line 27 def write(content, ) FileUtils.mkdir_p(File.dirname(@absolute_path)) File.open(@absolute_path, 'w') { |f| f.puts(content) } @repository.() end |