Class: Gollum::Wiki

Inherits:
Object
  • Object
show all
Includes:
Pagination
Defined in:
lib/gollum-lib/wiki.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pagination

included, #log_pagination_options, #page_to_skip

Constructor Details

#initialize(path, options = {}) ⇒ Wiki

Public: Initialize a new Gollum Repo.

path - The String path to the Git repository that holds the Gollum

site.

options - Optional Hash:

:universal_toc - Table of contents on all pages.  Default: false
:base_path     - String base path for all Wiki links.
                 Default: "/"
:page_file_dir - String the directory in which all page files reside
:ref - String the repository ref to retrieve pages from
:mathjax       - Set to false to disable mathjax.
:user_icons    - Enable user icons on the history page. [gravatar, identicon, none].
                 Default: none
:global_tag_lookup        - Enable 4.x compatibility behavior for links
:hyphened_tag_lookup - Spaces in tag paths are treated as dashes (-)
:case_insensitive_tag_lookup - Paths in tags are compared case_insensitively
:css           - Include the custom.css file from the repo.
:emoji         - Parse and interpret emoji tags (e.g. :heart:).
:h1_title      - Concatenate all h1's on a page to form the
                 page title.
:display_metadata - Whether or not to render a page's metadata on the page. Default: true
:index_page    - The default page to retrieve or create if the
                 a directory is accessed.
:bar_side      - Where the sidebar should be displayed, may be:
                  - :left
                  - :right
:allow_uploads - Set to true to allow file uploads.
:per_page_uploads - Whether uploads should be stored in a central
                 'uploads' directory, or in a directory named for
                 the page they were uploaded to.
:filter_chain  - Override the default filter chain with your own.

Returns a fresh Gollum::Repo.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gollum-lib/wiki.rb', line 116

def initialize(path, options = {})
  options = self.class.default_options.merge(options)
  if path.is_a?(GitAccess)
    options[:access] = path
    path             = path.path
  end

  @path                 = path
  @repo_is_bare         = options.fetch :repo_is_bare, nil
  @page_file_dir        = options.fetch :page_file_dir, nil
  @page_file_dir        = Pathname.new("/#{@page_file_dir}").cleanpath.to_s[1..-1] if @page_file_dir
  @access               = options.fetch :access, GitAccess.new(path, @page_file_dir, @repo_is_bare)
  @base_path            = options.fetch :base_path, "/"
  @repo                 = @access.repo
  @ref                  = options.fetch :ref, self.class.default_ref
  @universal_toc        = options.fetch :universal_toc, false
  @mathjax              = options.fetch :mathjax, false
  @global_tag_lookup    = options.fetch :global_tag_lookup, false
  @hyphened_tag_lookup  = options.fetch :hyphened_tag_lookup, false
  @case_insensitive_tag_lookup = options.fetch :case_insensitive_tag_lookup, false
  @css                  = options.fetch :css, false
  @emoji                = options.fetch :emoji, false
  @critic_markup        = options.fetch :critic_markup, false
  @h1_title             = options.fetch :h1_title, false
  @display_metadata     = options.fetch :display_metadata, true
  @index_page           = options.fetch :index_page, 'Home'
  @bar_side             = options.fetch :sidebar, :right
  @user_icons           = ['gravatar', 'identicon'].include?(options[:user_icons]) ?
      options[:user_icons] : 'none'
  @allow_uploads        = options.fetch :allow_uploads, false
  @per_page_uploads     = options.fetch :per_page_uploads, false
  @metadata             = options.fetch :metadata, {}
  @filter_chain         = options.fetch :filter_chain,
                                        [:YAML, :BibTeX, :PlainText, :CriticMarkup, :TOC, :RemoteCode, :Code, :Macro, :Emoji, :Sanitize, :PlantUML, :Tags, :PandocBib, :Render]
  @filter_chain.delete(:Emoji) unless options.fetch :emoji, false
  @filter_chain.delete(:PandocBib) unless ::Gollum::MarkupRegisterUtils.using_pandoc?
  @filter_chain.delete(:CriticMarkup) unless options.fetch :critic_markup, false

  Hook.execute(:post_wiki_initialize, self)
end

Class Attribute Details

.default_committer_emailObject



30
31
32
# File 'lib/gollum-lib/wiki.rb', line 30

def default_committer_email
  @default_committer_email || '[email protected]'
end

.default_committer_nameObject



26
27
28
# File 'lib/gollum-lib/wiki.rb', line 26

def default_committer_name
  @default_committer_name || 'Anonymous'
end

.default_optionsObject



34
35
36
# File 'lib/gollum-lib/wiki.rb', line 34

def default_options
  @default_options || {}
end

.default_refObject



22
23
24
# File 'lib/gollum-lib/wiki.rb', line 22

def default_ref
  @default_ref || 'master'
end

Instance Attribute Details

#allow_uploadsObject (readonly)

Toggles file upload functionality.



605
606
607
# File 'lib/gollum-lib/wiki.rb', line 605

def allow_uploads
  @allow_uploads
end

#bar_sideObject (readonly)

Gets side on which the sidebar should be shown



72
73
74
# File 'lib/gollum-lib/wiki.rb', line 72

def bar_side
  @bar_side
end

#base_pathObject (readonly)

The String base path to prefix to internal links. For example, when set to “/wiki”, the page “Hobbit” will be linked as “/wiki/Hobbit”. Defaults to “/”.



48
49
50
# File 'lib/gollum-lib/wiki.rb', line 48

def base_path
  @base_path
end

#case_insensitive_tag_lookupObject (readonly)

Enable 4.x compatibility for case-case_insensitive links



599
600
601
# File 'lib/gollum-lib/wiki.rb', line 599

def case_insensitive_tag_lookup
  @case_insensitive_tag_lookup
end

#cssObject (readonly)

Injects custom css from custom.css in root repo. Defaults to false



58
59
60
# File 'lib/gollum-lib/wiki.rb', line 58

def css
  @css
end

#display_metadataObject (readonly)

Whether or not to render a page’s metadata on the page Defaults to true



66
67
68
# File 'lib/gollum-lib/wiki.rb', line 66

def 
  @display_metadata
end

#filter_chainObject (readonly)

An array of symbols which refer to classes under Gollum::Filter, each of which is an element in the “filtering chain”. See the documentation for Gollum::Filter for more on how this chain works, and what filter classes need to implement.



78
79
80
# File 'lib/gollum-lib/wiki.rb', line 78

def filter_chain
  @filter_chain
end

#global_tag_lookupObject (readonly)

Enable 4.x compatibility behavior for links



596
597
598
# File 'lib/gollum-lib/wiki.rb', line 596

def global_tag_lookup
  @global_tag_lookup
end

#h1_titleObject (readonly)

Sets page title to value of first h1 Defaults to false



62
63
64
# File 'lib/gollum-lib/wiki.rb', line 62

def h1_title
  @h1_title
end

#hyphened_tag_lookupObject (readonly)

Spaces in tag paths are treated as dashes (-)



602
603
604
# File 'lib/gollum-lib/wiki.rb', line 602

def hyphened_tag_lookup
  @hyphened_tag_lookup
end

#index_pageObject (readonly)

Gets the custom index page for / and subdirs (e.g. foo/)



69
70
71
# File 'lib/gollum-lib/wiki.rb', line 69

def index_page
  @index_page
end

#mathjaxObject (readonly)

Toggles mathjax.



586
587
588
# File 'lib/gollum-lib/wiki.rb', line 586

def mathjax
  @mathjax
end

#metadataObject (readonly)

Global metadata to be merged into the metadata for each page



81
82
83
# File 'lib/gollum-lib/wiki.rb', line 81

def 
  @metadata
end

#page_file_dirObject (readonly)

Gets the String directory in which all page files reside.



54
55
56
# File 'lib/gollum-lib/wiki.rb', line 54

def page_file_dir
  @page_file_dir
end

#pathObject (readonly)

The String path to the Git repository that holds the Gollum site.

Returns the String path.



43
44
45
# File 'lib/gollum-lib/wiki.rb', line 43

def path
  @path
end

#per_page_uploadsObject (readonly)

Toggles whether uploaded files go into ‘uploads’, or a directory named after the page they were uploaded to.



609
610
611
# File 'lib/gollum-lib/wiki.rb', line 609

def per_page_uploads
  @per_page_uploads
end

#refObject (readonly)

Gets the String ref in which all page files reside.



51
52
53
# File 'lib/gollum-lib/wiki.rb', line 51

def ref
  @ref
end

#repoObject (readonly)

The Gollum::Git::Repo associated with the wiki.

Returns the Gollum::Git::Repo.



575
576
577
# File 'lib/gollum-lib/wiki.rb', line 575

def repo
  @repo
end

#repo_is_bareObject (readonly)

Whether or not the wiki’s repository is bare (doesn’t have a working directory)



40
41
42
# File 'lib/gollum-lib/wiki.rb', line 40

def repo_is_bare
  @repo_is_bare
end

#show_allObject (readonly)

Toggles showing all files in files view. Default is false. When false, only valid pages in the git repo are displayed.



593
594
595
# File 'lib/gollum-lib/wiki.rb', line 593

def show_all
  @show_all
end

#universal_tocObject (readonly)

Toggles display of universal table of contents



583
584
585
# File 'lib/gollum-lib/wiki.rb', line 583

def universal_toc
  @universal_toc
end

#user_iconsObject (readonly)

Toggles user icons. Default: ‘none’



589
590
591
# File 'lib/gollum-lib/wiki.rb', line 589

def user_icons
  @user_icons
end

Instance Method Details

#add_redirect(old_path, new_path, commit = nil) ⇒ Object



556
557
558
559
# File 'lib/gollum-lib/wiki.rb', line 556

def add_redirect(old_path, new_path, commit=nil)
  redirects[old_path] = new_path
  redirects.dump(commit)
end

#clear_cacheObject

Public: Refreshes just the cached Git reference data. This should be called after every Gollum update.

Returns nothing.



543
544
545
# File 'lib/gollum-lib/wiki.rb', line 543

def clear_cache
  @access.refresh
end

#commit_for(ref) ⇒ Object

Gets the commit object for the given ref or sha.

ref - A string ref or SHA pointing to a valid commit.

Returns a Gollum::Git::Commit instance.



673
674
675
676
# File 'lib/gollum-lib/wiki.rb', line 673

def commit_for(ref)
  @access.commit(ref)
rescue Gollum::Git::NoSuchShaFound
end

#default_committer_emailObject

Gets the default email for commits.

Returns the String email address.



662
663
664
665
666
# File 'lib/gollum-lib/wiki.rb', line 662

def default_committer_email
  email = @repo.config['user.email']
  email = email.delete('<>') if email
  @default_committer_email ||= email || self.class.default_committer_email
end

#default_committer_nameObject

Gets the default name for commits.

Returns the String name.



654
655
656
657
# File 'lib/gollum-lib/wiki.rb', line 654

def default_committer_name
  @default_committer_name ||= \
    @repo.config['user.name'] || self.class.default_committer_name
end

#delete_file(path, commit) ⇒ Object

Public: Delete a file.

path - The path to the file to delete commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/gollum-lib/wiki.rb', line 399

def delete_file(path, commit)
  fullpath     = ::File.join([page_file_dir, path].compact)
  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  committer.delete(fullpath)

  committer.after_commit do |index, _sha|
    dir = '' if dir == '.'
    @access.refresh
    index.update_working_dir(fullpath)
  end

  multi_commit ? committer : committer.commit
end

#delete_page(page, commit) ⇒ Object

Public: Delete a page.

page - The Gollum::Page to delete. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



379
380
381
# File 'lib/gollum-lib/wiki.rb', line 379

def delete_page(page, commit)
  delete_file(page.url_path, commit)
end

#exist?Boolean

Public: check whether the wiki’s git repo exists on the filesystem.

Returns true if the repo exists, and false if it does not.

Returns:

  • (Boolean)


160
161
162
# File 'lib/gollum-lib/wiki.rb', line 160

def exist?
  @access.exist?
end

#file(name, version = nil, try_on_disk = false) ⇒ Object

Public: Get the static file for a given name.

name - The full String pathname to the file. version - The String version ID to find (default: @ref). try_on_disk - If true, try to return just a reference to a file

that exists on the disk.

Returns a Gollum::File or nil if no matching file was found. Note that if you specify try_on_disk=true, you may or may not get a file for which on_disk? is actually true.



185
186
187
# File 'lib/gollum-lib/wiki.rb', line 185

def file(name, version = nil, try_on_disk = false)
  ::Gollum::File.find(self, name, version.nil? ? @ref : version, try_on_disk)
end

#files(treeish = nil) ⇒ Object

Public: Lists all non-page files for this wiki.

treeish - The String commit ID or ref to find (default: @ref)

Returns an Array of Gollum::File instances.



470
471
472
# File 'lib/gollum-lib/wiki.rb', line 470

def files(treeish = nil)
  tree_list(treeish || @ref, false, true)
end

#inspectObject



696
697
698
# File 'lib/gollum-lib/wiki.rb', line 696

def inspect
  %(#<#{self.class.name}:#{object_id} #{@repo.path}>)
end

#latest_changes(options = {}) ⇒ Object

Returns the latest changes in the wiki (globally)

options - The options Hash:

:max_count  - The Integer number of items to return.

Returns an Array of Gollum::Git::Commit.



534
535
536
537
# File 'lib/gollum-lib/wiki.rb', line 534

def latest_changes(options={})
  options[:max_count] = 10 unless options[:max_count]
  @repo.log(@ref, page_file_dir, options)
end

#log(options = {}) ⇒ Object

Public: All of the versions that have touched the Page.

options - The options Hash:

:page_num  - The Integer page number (default: 1).
:per_page  - The Integer max count of items to return.

Returns an Array of Gollum::Git::Commit.



524
525
526
# File 'lib/gollum-lib/wiki.rb', line 524

def log(options = {})
  @repo.log(@ref, nil, log_pagination_options(options))
end

#normalize(data) ⇒ Object

Normalize the data.

data - The String data to be normalized.

Returns the normalized data String.



616
617
618
# File 'lib/gollum-lib/wiki.rb', line 616

def normalize(data)
  data.gsub(/\r/, '')
end

#overwrite_file(name, data, commit = {}) ⇒ Object

Public: Write a file to the Gollum repo regardless of existing versions.

path - The String path where the file will be written. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update



259
260
261
# File 'lib/gollum-lib/wiki.rb', line 259

def overwrite_file(name, data, commit = {})
  write(merge_path_elements(nil, name, nil), data, commit, force_overwrite = true)
end

#page(path, version = nil, global_match = false) ⇒ Object

Public: Get the formatted page for a given page name, version, and dir.

path - The String path to the the wiki page (may or may not include file extension). version - The String version ID to find (default: @ref). global_match - If true, find a File matching path’s filename, but not it’s directory (so anywhere in the repo)

Returns a Gollum::Page or nil if no matching page was found.



171
172
173
# File 'lib/gollum-lib/wiki.rb', line 171

def page(path, version = nil, global_match = false)
  ::Gollum::Page.find(self, path, version.nil? ? @ref : version, false, global_match)
end

#page_file_name(name, format) ⇒ Object

Assemble a Page’s filename from its name and format.

name - The String name of the page (should be pre-canonicalized). format - The Symbol format of the page.

Returns the String filename.



626
627
628
# File 'lib/gollum-lib/wiki.rb', line 626

def page_file_name(name, format)
  format.nil? ? name : "#{name}.#{::Gollum::Page.format_to_ext(format)}"
end

#pages(treeish = nil) ⇒ Object

Public: Lists all pages for this wiki.

treeish - The String commit ID or ref to find (default: @ref)

Returns an Array of Gollum::Page instances.



461
462
463
# File 'lib/gollum-lib/wiki.rb', line 461

def pages(treeish = nil)
  tree_list(treeish || @ref, true, false)
end

#preview_page(name, data, format) ⇒ Object

Public: Create an in-memory Page with the given data and format. This is useful for previewing what content will look like before committing it to the repository.

name - The String name of the page. format - The Symbol format of the page. data - The new String contents of the page.

Returns the in-memory Gollum::Page.



198
199
200
# File 'lib/gollum-lib/wiki.rb', line 198

def preview_page(name, data, format)
  ::Gollum::PreviewPage.new(self, "#{name}.#{::Gollum::Page.format_to_ext(format.to_sym)}", data, @access.commit(@ref))
end

#redirectsObject



547
548
549
550
551
552
553
554
# File 'lib/gollum-lib/wiki.rb', line 547

def redirects
  if @redirects.nil? || @redirects.stale?
    @redirects = {}.extend(::Gollum::Redirects)
    @redirects.init(self)
    @redirects.load
  end
  @redirects
end

#remove_redirect(path, commit = nil) ⇒ Object



561
562
563
564
# File 'lib/gollum-lib/wiki.rb', line 561

def remove_redirect(path, commit=nil)
  redirects.tap{|k| k.delete(path)}
  redirects.dump(commit)
end

#rename_page(page, rename, commit = {}) ⇒ Object

Public: Rename an existing page without altering content.

page - The Gollum::Page to update. rename - The String extension-less full path of the page (leading ‘/’ is ignored). commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update. Returns false if the operation is a NOOP.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/gollum-lib/wiki.rb', line 281

def rename_page(page, rename, commit = {})
  return false if page.nil?
  return false if rename.nil? or rename.empty?

  (target_dir, target_name) = ::File.split(rename)
  (source_dir, source_name) = ::File.split(page.path)
  source_name               = page.filename_stripped

  # File.split gives us relative paths with ".", commiter.add_to_index doesn't like that.
  target_dir                = '' if target_dir == '.'
  source_dir                = '' if source_dir == '.'
  target_dir                = target_dir.gsub(/^\//, '')

  # if the rename is a NOOP, abort
  if source_dir == target_dir and source_name == target_name
    return false
  end

  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  committer.delete(page.path)
  committer.add_to_index(merge_path_elements(target_dir, target_name, page.format), page.raw_data)

  committer.after_commit do |index, _sha|
    @access.refresh
    index.update_working_dir(merge_path_elements(source_dir, source_name, page.format))
    index.update_working_dir(merge_path_elements(target_dir, target_name, page.format))
  end

  multi_commit ? committer : committer.commit
end

#revert_commit(sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff to the repo. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



450
451
452
453
454
# File 'lib/gollum-lib/wiki.rb', line 450

def revert_commit(sha1, sha2 = nil, commit = {})
  left, right, options = parse_revert_options(sha1, sha2, commit)
  tree, files = repo.git.revert_commit(left, right)
  commit_and_update_paths(tree, files, options)
end

#revert_page(page, sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff for a given page. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

page - The Gollum::Page to delete. sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.
:parent  - Optional Gollum::Git::Commit parent to this update.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



430
431
432
433
434
# File 'lib/gollum-lib/wiki.rb', line 430

def revert_page(page, sha1, sha2 = nil, commit = {})
  return false unless page
  left, right, options = parse_revert_options(sha1, sha2, commit)
  commit_and_update_paths(@repo.git.revert_path(page.path, left, right), [page.path], options)
end

#sanitizerObject

Public: Creates a Sanitize instance

Returns a Sanitize instance.



703
704
705
# File 'lib/gollum-lib/wiki.rb', line 703

def sanitizer
  @sanitizer ||= Gollum::Sanitization.new(Gollum::Markup.to_xml_opts)
end

#search(query) ⇒ Object

Public: Search all pages for this wiki.

query - The string to search for

Returns an Array with Objects of page name and count of matches



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/gollum-lib/wiki.rb', line 492

def search(query)
  options = {:path => page_file_dir, :ref => ref}
  search_terms = query.scan(/"([^"]+)"|(\S+)/).flatten.compact.map {|term| Regexp.escape(term)}
  search_terms_regex = search_terms.join('|')
  query = /^(.*(?:#{search_terms_regex}).*)$/i
  results = @repo.git.grep(search_terms, options) do |name, data|
    result = {:count => 0}
    result[:name] = extract_page_file_dir(name)
    result[:filename_count] = result[:name].scan(/#{search_terms_regex}/i).size
    result[:context] = []
    if data
      begin
        data.scan(query) do |match|
          result[:context] << match.first
          result[:count] += match.first.scan(/#{search_terms_regex}/i).size
        end
      rescue ArgumentError # https://github.com/gollum/gollum/issues/1491
        next
      end
    end
    ((result[:count] + result[:filename_count]) == 0) ? nil : result
  end
  [results, search_terms]
end

#size(ref = nil) ⇒ Object

Public: Returns the number of pages accessible from a commit

ref - A String ref that is either a commit SHA or references one.

Returns a Fixnum



479
480
481
482
483
484
485
# File 'lib/gollum-lib/wiki.rb', line 479

def size(ref = nil)
  tree_map_for(ref || @ref).inject(0) do |num, entry|
    num + (::Gollum::Page.valid_page_name?(entry.name) ? 1 : 0)
  end
rescue Gollum::Git::NoSuchShaFound
  0
end

#tree_list(ref = @ref, pages = true, files = true) ⇒ Object

Fill an array with a list of pages and files in the wiki.

ref - A String ref that is either a commit SHA or references one.

Returns a flat Array of Gollum::Page and Gollum::File instances.



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/gollum-lib/wiki.rb', line 635

def tree_list(ref = @ref, pages=true, files=true)
  if (sha = @access.ref_to_sha(ref))
    commit = @access.commit(sha)
    tree_map_for(sha).inject([]) do |list, entry|
      if ::Gollum::Page.valid_page_name?(entry.name)
        list << entry.page(self, commit) if pages
      elsif files && !entry.name.start_with?('_') && !::Gollum::Page.protected_files.include?(entry.name)
        list << entry.file(self, commit)
      end
      list
    end
  else
    []
  end
end

#tree_map_for(ref, ignore_page_file_dir = false) ⇒ Object

Finds a full listing of files and their blob SHA for a given ref. Each listing is cached based on its actual commit SHA.

ref - A String ref that is either a commit SHA or references one. ignore_page_file_dir - Boolean, if true, searches all files within the git repo, regardless of dir/subdir

Returns an Array of BlobEntry instances.



685
686
687
688
689
690
691
692
693
694
# File 'lib/gollum-lib/wiki.rb', line 685

def tree_map_for(ref, ignore_page_file_dir = false)
  if ignore_page_file_dir && !@page_file_dir.nil?
    @root_access ||= GitAccess.new(path, nil, @repo_is_bare)
    @root_access.tree(ref)
  else
    @access.tree(ref)
  end
rescue Gollum::Git::NoSuchShaFound
  []
end

#update_page(page, name, format, data, commit = {}) ⇒ Object

Public: Update an existing page with new content. The location of the page inside the repository will not change. If the given format is different than the current format of the page, the filename will be changed to reflect the new format.

page - The Gollum::Page to update. name - The String extension-less name of the page. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/gollum-lib/wiki.rb', line 336

def update_page(page, name, format, data, commit = {})
  name     ||= page.name
  format   ||= page.format
  dir      = ::File.dirname(page.path)
  dir      = nil if dir == '.'
  rename   = (page.name != name || page.format != format)
  new_path = ::File.join([dir, self.page_file_name(name, format)].compact) if rename

  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  if !rename
    committer.add(page.path, normalize(data))
  else
    committer.delete(page.path)
    committer.add_to_index(new_path, data)
  end

  committer.after_commit do |index, _sha|
    @access.refresh
    index.update_working_dir(page.path)
    index.update_working_dir(new_path) if rename
  end

  multi_commit ? committer : committer.commit
end

#write_file(name, data, commit = {}) ⇒ Object

Public: Write a new version of a file to the Gollum repo.

path - The String path where the file will be written. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update



239
240
241
# File 'lib/gollum-lib/wiki.rb', line 239

def write_file(name, data, commit = {})
  write(merge_path_elements(nil, name, nil), data, commit)
end

#write_page(path, format, data, commit = {}) ⇒ Object

Public: Write a new version of a page to the Gollum repo root.

path - The String path where the page will be written. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



219
220
221
# File 'lib/gollum-lib/wiki.rb', line 219

def write_page(path, format, data, commit = {})
 write(merge_path_elements(nil, path, format), data, commit)
end