lib/gollum-lib/git_access.rb in gitlab-gollum-lib-1.1.0 vs lib/gollum-lib/git_access.rb in gitlab-gollum-lib-4.2.7

- old
+ new

@@ -8,14 +8,14 @@ # 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. - def initialize(path, page_file_dir = nil, bare = false) + def initialize(path, page_file_dir = nil, bare = nil) @page_file_dir = page_file_dir - @path = path - @repo = Grit::Repo.new(path, { :is_bare => bare }) + @path = path + @repo = Gollum::Git::Repo.new(path, { :is_bare => bare }) clear end # Public: Determines whether the Git repository exists on disk. # @@ -32,26 +32,26 @@ # Returns a String, or nil if the ref isn't found. 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 + if sha?(ref) + ref + else + get_cache(:ref, ref) { ref_to_sha!(ref) } + end.to_s sha.empty? ? nil : sha end # 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. def tree(ref) - if sha = ref_to_sha(ref) + if (sha = ref_to_sha(ref)) get_cache(:tree, sha) { tree!(sha) } else [] end end @@ -67,20 +67,20 @@ # Public: Looks up the Git commit using the given Git SHA or ref. # # ref - A String Git SHA or ref. # - # Returns a Grit::Commit. + # Returns a Gollum::Git::Commit. def commit(ref) if sha?(ref) get_cache(:commit, ref) { commit!(ref) } else - if sha = get_cache(:ref, ref) + if (sha = get_cache(:ref, ref)) commit(sha) else - if cm = commit!(ref) - set_cache(:ref, ref, cm.id) + if (cm = commit!(ref)) + set_cache(:ref, ref, cm.id) set_cache(:commit, cm.id, cm) end end end end @@ -109,11 +109,11 @@ ######################################################################### # Gets the String path to the Git repository. attr_reader :path - # Gets the Grit::Repo instance for the Git repository. + # Gets the Gollum::Git::Repo instance for the Git repository. attr_reader :repo # Gets a Hash cache of refs to commit SHAs. # # {"master" => "abc123", ...} @@ -124,13 +124,13 @@ # # {"abc123" => [<BlobEntry>, <BlobEntry>]} # attr_reader :tree_map - # Gets a Hash cache of commit SHAs to the Grit::Commit instance. + # Gets a Hash cache of commit SHAs to the Gollum::Git::Commit instance. # - # {"abcd123" => <Grit::Commit>} + # {"abcd123" => <Gollum::Git::Commit>} # attr_reader :commit_map # Checks to see if the given String is a 40 character hex SHA. # @@ -145,30 +145,28 @@ # # ref - String Git ref. # # Returns a String SHA. def ref_to_sha!(ref) - @repo.git.rev_list({:max_count=>1}, ref) - rescue Grit::GitRuby::Repository::NoSuchShaFound + commit = @repo.commit(ref) + commit ? commit.id : nil end # Looks up the Git blobs for a given commit. # # sha - String commit SHA. # # Returns an Array of BlobEntry instances. def tree!(sha) - tree = @repo.git.native(:ls_tree, - {:r => true, :l => true, :z => true}, sha) - if tree.respond_to?(:force_encoding) - tree.force_encoding("UTF-8") + tree = @repo.lstree(sha, { :recursive => true }) + items = [] + tree.each do |entry| + if entry[:type] == 'blob' + items << BlobEntry.new(entry[:sha], entry[:path], entry[:size], entry[:mode].to_i(8)) + end end - items = tree.split("\0").inject([]) do |memo, line| - memo << parse_tree_line(line) - end - - if dir = @page_file_dir + if (dir = @page_file_dir) regex = /^#{dir}\// items.select { |i| i.path =~ regex } else items end @@ -178,18 +176,18 @@ # # sha - The String SHA. # # Returns the String content of the Git object. def cat_file!(sha) - @repo.git.cat_file({:p => true}, sha) + @repo.git.cat_file({ :p => true }, sha) end # Reads a Git commit. # # sha - The string SHA of the Git commit. # - # Returns a Grit::Commit. + # Returns a Gollum::Git::Commit. def commit!(sha) @repo.commit(sha) end # Attempts to get the given data from a cache. If it doesn't exist, it'll @@ -226,11 +224,11 @@ # line - A String line of output: # "100644 blob 839c2291b30495b9a882c17d08254d3c90d8fb53 Home.md" # # Returns an Array of BlobEntry instances. def parse_tree_line(line) - mode, type, sha, size, *name = line.split(/\s+/) + mode, _type, sha, size, *name = line.split(/\s+/) BlobEntry.new(sha, name.join(' '), size.to_i, mode.to_i(8)) end # Decode octal sequences (\NNN) in tree path names. # @@ -238,12 +236,12 @@ # # Returns a decoded String. def decode_git_path(path) if path[0] == ?" && path[-1] == ?" path = path[1...-1] - path.gsub!(/\\\d{3}/) { |m| m[1..-1].to_i(8).chr } + path.gsub!(/\\\d{3}/) { |m| m[1..-1].to_i(8).chr } end - path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m.to_s}")) } + path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m}")) } path end end end