Class: Gitdocs::Repository::Path

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(repository, relative_path) ⇒ Path

Returns a new instance of Path.

Parameters:



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

#contentObject



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

Returns:

  • (Boolean)


84
85
86
# File 'lib/gitdocs/repository/path.rb', line 84

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

#exist?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/gitdocs/repository/path.rb', line 80

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



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

#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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gitdocs/repository/path.rb', line 67

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.



41
42
43
# File 'lib/gitdocs/repository/path.rb', line 41

def mkdir
  FileUtils.mkdir_p(@absolute_path)
end

#readme_pathObject



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

#removeObject

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

Parameters:

  • ref (String)


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

#revisionsArray<Hash>

Returns the revisions available for a particular file

Parameters:

  • file (String)

Returns:

  • (Array<Hash>)


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

#text?Boolean

Returns:

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

#touchObject

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.

Parameters:

  • content (String)
  • commit_message (String)


27
28
29
30
31
32
# File 'lib/gitdocs/repository/path.rb', line 27

def write(content, commit_message)
  FileUtils.mkdir_p(File.dirname(@absolute_path))
  File.open(@absolute_path, 'w') { |f| f.puts(content) }

  @repository.write_commit_message(commit_message)
end