Class: Gitdocs::Repository::Path
- Inherits:
-
Object
- Object
- Gitdocs::Repository::Path
- Defined in:
- lib/gitdocs/repository/path.rb
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.
-
#mv(filename) ⇒ Object
Move file to the repository path.
- #readme_path ⇒ Object
- #relative_dirname ⇒ String
-
#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) ⇒ void
Write the content to the path and create any necessary directories.
Constructor Details
#initialize(repository, relative_path) ⇒ Path
Returns a new instance of Path.
12 13 14 15 16 17 18 |
# File 'lib/gitdocs/repository/path.rb', line 12 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 |
Instance Attribute Details
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
8 9 10 |
# File 'lib/gitdocs/repository/path.rb', line 8 def relative_path @relative_path end |
Instance Method Details
#absolute_path(ref = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/gitdocs/repository/path.rb', line 102 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
132 133 134 135 |
# File 'lib/gitdocs/repository/path.rb', line 132 def content return nil unless File.file?(@absolute_path) File.read(@absolute_path) end |
#directory? ⇒ Boolean
98 99 100 |
# File 'lib/gitdocs/repository/path.rb', line 98 def directory? File.directory?(@absolute_path) end |
#exist? ⇒ Boolean
94 95 96 |
# File 'lib/gitdocs/repository/path.rb', line 94 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/gitdocs/repository/path.rb', line 122 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) ⇒ Object
27 28 29 30 |
# File 'lib/gitdocs/repository/path.rb', line 27 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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gitdocs/repository/path.rb', line 81 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.
48 49 50 |
# File 'lib/gitdocs/repository/path.rb', line 48 def mkdir FileUtils.mkdir_p(@absolute_path) end |
#mv(filename) ⇒ Object
Move file to the repository path
54 55 56 57 |
# File 'lib/gitdocs/repository/path.rb', line 54 def mv(filename) FileUtils.mkdir_p(File.dirname(@absolute_path)) FileUtils.mv(filename, @absolute_path) end |
#readme_path ⇒ Object
112 113 114 115 |
# File 'lib/gitdocs/repository/path.rb', line 112 def readme_path return nil unless directory? Dir.glob(File.join(@absolute_path, 'README.{md}')).first end |
#relative_dirname ⇒ String
21 22 23 24 25 |
# File 'lib/gitdocs/repository/path.rb', line 21 def relative_dirname result = File.dirname(@relative_path) return '' if result == '.' result end |
#remove ⇒ Object
Remove the path, but only if it is a file.
60 61 62 63 |
# File 'lib/gitdocs/repository/path.rb', line 60 def remove return nil unless File.file?(@absolute_path) FileUtils.rm(@absolute_path) end |
#revert(ref) ⇒ Object
Revert file to the specified ref
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/gitdocs/repository/path.rb', line 156 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 ⇒ Array<Hash>
Returns the revisions available for a particular file
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gitdocs/repository/path.rb', line 142 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
66 67 68 69 70 |
# File 'lib/gitdocs/repository/path.rb', line 66 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 ⇒ Object
Touch and path and create any necessary directories.
42 43 44 45 |
# File 'lib/gitdocs/repository/path.rb', line 42 def touch FileUtils.mkdir_p(File.dirname(@absolute_path)) FileUtils.touch(@absolute_path) end |
#write(content) ⇒ void
This method returns an undefined value.
Write the content to the path and create any necessary directories.
36 37 38 39 |
# File 'lib/gitdocs/repository/path.rb', line 36 def write(content) FileUtils.mkdir_p(File.dirname(@absolute_path)) File.open(@absolute_path, 'w') { |f| f.puts(content) } end |