Class: Gitdocs::Repository::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdocs/repository/path.rb

Defined Under Namespace

Classes: DirEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, relative_path) ⇒ Path

Returns a new instance of Path.

Parameters:



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_pathObject (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.expand_path(File.basename(@relative_path), Dir.tmpdir)
  File.open(tmp_path, 'w') { |f| f.puts content }
  tmp_path
end

#contentObject



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

Returns:

  • (Boolean)


98
99
100
# File 'lib/gitdocs/repository/path.rb', line 98

def directory?
  File.directory?(@absolute_path)
end

#exist?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/gitdocs/repository/path.rb', line 94

def exist?
  File.exist?(@absolute_path)
end

#file_listingArray<DirEntry>

Returns entries in the directory

  • excluding any git related directories

  • sorted by filename, ignoring any leading ‘.’s.

Returns:

  • (Array<DirEntry>)

    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

#metaHash<:author => String, :size => Integer, modified => Time>

Returns file meta data based on relative file path

Examples:

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

Returns:

  • (Hash<:author => String, :size => Integer, modified => Time>)

Raises:

  • (RuntimeError)

    if the file is not found in any commits



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gitdocs/repository/path.rb', line 81

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

#mkdirObject

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

Parameters:

  • filename (String)


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_pathObject



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_dirnameString

Returns:

  • (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

#removeObject

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

Parameters:

  • ref (String)


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

#revisionsArray<Hash>

Returns the revisions available for a particular file

Parameters:

  • file (String)

Returns:

  • (Array<Hash>)


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.message.split("\n")[0],
      author:  commit.author[:name],
      date:    commit.author[:time]
    }
  end
end

#text?Boolean

Returns:

  • (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

#touchObject

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.

Parameters:

  • content (String)


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