Class: Gollum::GitAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/git_access.rb

Overview

Controls all access to the Git objects from Gollum. Extend this class to add custom caching for special cases.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, page_file_dir = nil, bare = nil) ⇒ GitAccess

Initializes the GitAccess instance.

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

Gollum site.

page_file_dir - String the directory in which all page files reside

Returns this instance.



13
14
15
16
17
18
# File 'lib/gollum-lib/git_access.rb', line 13

def initialize(path, page_file_dir = nil, bare = nil)
  @page_file_dir = page_file_dir
  @path          = path
  @repo = Gollum::Git::Repo.new(path, { :is_bare => bare })
  clear
end

Instance Attribute Details

#commit_mapObject (readonly)

Gets a Hash cache of commit SHAs to the Gollum::Git::Commit instance.

{"abcd123" => <Gollum::Git::Commit>}


133
134
135
# File 'lib/gollum-lib/git_access.rb', line 133

def commit_map
  @commit_map
end

#pathObject (readonly)

Gets the String path to the Git repository.



112
113
114
# File 'lib/gollum-lib/git_access.rb', line 112

def path
  @path
end

#ref_mapObject (readonly)

Gets a Hash cache of refs to commit SHAs.

{"master" => "abc123", ...}


121
122
123
# File 'lib/gollum-lib/git_access.rb', line 121

def ref_map
  @ref_map
end

#repoObject (readonly)

Gets the Gollum::Git::Repo instance for the Git repository.



115
116
117
# File 'lib/gollum-lib/git_access.rb', line 115

def repo
  @repo
end

#tree_mapObject (readonly)

Gets a Hash cache of commit SHAs to a recursive tree of blobs.

{"abc123" => [<BlobEntry>, <BlobEntry>]}


127
128
129
# File 'lib/gollum-lib/git_access.rb', line 127

def tree_map
  @tree_map
end

Instance Method Details

#blob(sha) ⇒ Object

Public: Fetches the contents of the Git blob at the given SHA.

sha - A String Git SHA.

Returns the String content of the blob.



64
65
66
# File 'lib/gollum-lib/git_access.rb', line 64

def blob(sha)
  cat_file!(sha)
end

#cat_file!(sha) ⇒ Object

Reads the content from the Git db at the given SHA.

sha - The String SHA.

Returns the String content of the Git object.



176
177
178
# File 'lib/gollum-lib/git_access.rb', line 176

def cat_file!(sha)
  @repo.git.cat_file({ :p => true }, sha)
end

#clearObject

Public: Clears all of the cached data that this GitAccess is tracking.

Returns nothing.



91
92
93
94
95
# File 'lib/gollum-lib/git_access.rb', line 91

def clear
  @ref_map    = {}
  @tree_map   = {}
  @commit_map = {}
end

#commit(ref) ⇒ Object

Public: Looks up the Git commit using the given Git SHA or ref.

ref - A String Git SHA or ref.

Returns a Gollum::Git::Commit.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gollum-lib/git_access.rb', line 73

def commit(ref)
  if sha?(ref)
    get_cache(:commit, ref) { commit!(ref) }
  else
    if (sha = get_cache(:ref, ref))
      commit(sha)
    else
      if (cm = commit!(ref))
        set_cache(:ref, ref, cm.id)
        set_cache(:commit, cm.id, cm)
      end
    end
  end
end

#commit!(sha) ⇒ Object

Reads a Git commit.

sha - The string SHA of the Git commit.

Returns a Gollum::Git::Commit.



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

def commit!(sha)
  @repo.commit(sha)
end

#exist?Boolean

Public: Determines whether the Git repository exists on disk.

Returns true if it exists, or false.

Returns:

  • (Boolean)


23
24
25
# File 'lib/gollum-lib/git_access.rb', line 23

def exist?
  @repo.git.exist?
end

#get_cache(name, key) ⇒ Object

Attempts to get the given data from a cache. If it doesn’t exist, it’ll pass the results of the yielded block to the cache for future accesses.

name - The cache prefix used in building the full cache key. key - The unique cache key suffix, usually a String Git SHA.

Yields a block to pass to the cache. Returns the cached result.



197
198
199
200
201
202
203
204
# File 'lib/gollum-lib/git_access.rb', line 197

def get_cache(name, key)
  cache = instance_variable_get("@#{name}_map")
  value = cache[key]
  if value.nil? && block_given?
    set_cache(name, key, value = yield)
  end
  value == :_nil ? nil : value
end

#ref_to_sha(ref) ⇒ Object

Public: Converts a given Git reference to a SHA, using the cache if available.

ref - a String Git reference (ex: “master”)

Returns a String, or nil if the ref isn’t found.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gollum-lib/git_access.rb', line 33

def ref_to_sha(ref)
  ref = ref.to_s
  return if ref.empty?
  sha =
      if sha?(ref)
        ref
      else
        get_cache(:ref, ref) { ref_to_sha!(ref) }
      end.to_s
  sha.empty? ? nil : sha
end

#ref_to_sha!(ref) ⇒ Object

Looks up the Git SHA for the given Git ref.

ref - String Git ref.

Returns a String SHA.



149
150
151
152
# File 'lib/gollum-lib/git_access.rb', line 149

def ref_to_sha!(ref)
  commit = @repo.commit(ref)
  commit ? commit.id : nil
end

#refreshObject

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

Returns nothing.



101
102
103
# File 'lib/gollum-lib/git_access.rb', line 101

def refresh
  @ref_map.clear
end

#set_cache(name, key, value) ⇒ Object

Writes some data to the internal cache.

name - The cache prefix used in building the full cache key. key - The unique cache key suffix, usually a String Git SHA. value - The value to write to the cache.

Returns nothing.



213
214
215
216
# File 'lib/gollum-lib/git_access.rb', line 213

def set_cache(name, key, value)
  cache      = instance_variable_get("@#{name}_map")
  cache[key] = value || :_nil
end

#sha?(str) ⇒ Boolean

Checks to see if the given String is a 40 character hex SHA.

str - Possible String SHA.

Returns true if the String is a SHA, or false.

Returns:

  • (Boolean)


140
141
142
# File 'lib/gollum-lib/git_access.rb', line 140

def sha?(str)
  !!(str =~ /^[0-9a-f]{40}$/)
end

#tree(ref) ⇒ Object

Public: Gets a recursive list of Git blobs for the whole tree at the given commit.

ref - A String Git reference or Git SHA to a commit.

Returns an Array of BlobEntry instances.



51
52
53
54
55
56
57
# File 'lib/gollum-lib/git_access.rb', line 51

def tree(ref)
  if (sha = ref_to_sha(ref))
    get_cache(:tree, sha) { tree!(sha) }
  else
    []
  end
end

#tree!(sha) ⇒ Object

Looks up the Git blobs for a given commit.

sha - String commit SHA.

Returns an Array of BlobEntry instances.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gollum-lib/git_access.rb', line 159

def tree!(sha)
  tree  = @repo.lstree(sha, { :recursive => true })
  items = []
  tree.each do |entry|
    if entry[:type] == 'blob'
      next if @page_file_dir && !entry[:path].start_with?("#{@page_file_dir}/")
      items << BlobEntry.new(entry[:sha], entry[:path], entry[:size], entry[:mode])
    end
  end
  items
end