Class: WikiMD::Repository
- Inherits:
-
Object
- Object
- WikiMD::Repository
- Defined in:
- lib/wikimd/repository.rb
Overview
Handles reading and writing of files in the repo. Also interacts with GIT.
Defined Under Namespace
Classes: FileNotFound, GitError
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #diff(path, old, new) ⇒ Object
-
#files(root = '') ⇒ Object
return an array of all files.
- #history(path) ⇒ Object
-
#initialize(path) ⇒ Repository
constructor
A new instance of Repository.
-
#list_dirs(path) ⇒ Array
List all directories in
path
. -
#list_files(path) ⇒ Array
List all files in
path
. -
#read(path, rev = nil) ⇒ String
Reads a file from the repository.
-
#tree(root = '') ⇒ Object
return a hash containing all dirs and files.
-
#update(path, content) ⇒ Object
Updates the given file with ‘content`.
Constructor Details
#initialize(path) ⇒ Repository
Returns a new instance of Repository.
16 17 18 19 |
# File 'lib/wikimd/repository.rb', line 16 def initialize(path) @path = Pathname(path).realpath @path.mkpath end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/wikimd/repository.rb', line 14 def path @path end |
Instance Method Details
#diff(path, old, new) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/wikimd/repository.rb', line 95 def diff(path, old, new) path = pathname(path) raw_diff = git :diff, "-p --no-color #{old} #{new} -- #{path.to_s.shellescape}" diff = [] raw_diff.each_line do |line| next if line =~ /^(diff|index|\+\+\+|---)/ case line[0] when '@' parts = line.chomp.split('@@') diff << { start: parts[1].gsub(/^\s|\s$/, ''), lines: [(parts[2])].compact } else diff.last[:lines] << line.chomp end end diff end |
#files(root = '') ⇒ Object
return an array of all files
82 83 84 85 86 87 88 |
# File 'lib/wikimd/repository.rb', line 82 def files(root = '') files = [] dir = @path.join(root) Dir.chdir(dir) do files = Dir.glob('**/*').select { |p| dir.join(p).file? } end end |
#history(path) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wikimd/repository.rb', line 38 def history(path) params = %(--pretty='format:%h;%cr;%s' --no-decorate --no-color -z) out = git :log, "#{params} -- #{path.shellescape}" out.split("\x0").map do |e| h, d, m = e.split(';', 3) { hash: h, date: d, message: m } end end |
#list_dirs(path) ⇒ Array
List all directories in path
.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wikimd/repository.rb', line 55 def list_dirs(path) dir = pathname(path) Dir.chdir(dir) do Dir['*/'].sort end rescue raise FileNotFound, "no such file in repo - #{path}" end |
#list_files(path) ⇒ Array
List all files in path
.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wikimd/repository.rb', line 70 def list_files(path) dir = pathname(path) Dir.chdir(dir) do Dir['*'].select { |n| File.file?(n) }.sort end rescue raise FileNotFound, "no such file in repo - #{path}" end |
#read(path, rev = nil) ⇒ String
Reads a file from the repository. path
will have its leading slashes removed and must be within the repository path.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wikimd/repository.rb', line 27 def read(path, rev=nil) if rev.nil? file = pathname(path) return file.read else git :show, %(#{rev}:"./#{path.shellescape}") end rescue raise FileNotFound, "no such file in repo - #{path}" end |
#tree(root = '') ⇒ Object
return a hash containing all dirs and files
91 92 93 |
# File 'lib/wikimd/repository.rb', line 91 def tree(root = '') build_hash(files(root), root) end |
#update(path, content) ⇒ Object
Updates the given file with ‘content`. The file must be present!
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/wikimd/repository.rb', line 120 def update(path, content) file = pathname(path) # fail if path is a dir or similar fail FileNotFound unless file.file? # get rid of CRLFs content.gsub!(/\r\n?/, "\n") # don't do anything if the contents are identical return if file.read == content # write, add, commit file.write(content) git :add, path.shellescape git :commit, "-m 'update #{path}' -- #{path.shellescape}" end |