git_repository.rb revision 82943dcc7f878756736a3f21a8100b389b94ba46
0N/A# Wrapper for access to the local Git repository
20N/Aclass GitRepository
20N/A
20N/A include \
20N/A GetCommit,
20N/A GetObject,
20N/A GetDiff,
20N/A GetFolderContents,
20N/A GetInfoList,
20N/A Commit
20N/A
20N/A def initialize(path)
20N/A if File.exists?(path)
20N/A @repo = Rugged::Repository.new(path)
20N/A else
20N/A @repo = Rugged::Repository.init_at(path, true)
20N/A end
20N/A end
20N/A
20N/A def destroy
20N/A FileUtils.rmtree(@repo.path)
260N/A end
20N/A
20N/A def empty?
20N/A @repo.empty?
22N/A end
0N/A
50N/A def path_exists?(commit_oid=nil, url='')
50N/A rugged_commit = get_commit(commit_oid)
50N/A if !rugged_commit && url.empty?
50N/A true
50N/A else
50N/A path_exists_rugged?(rugged_commit, url)
50N/A end
50N/A end
50N/A
50N/A def get_current_file(commit_oid=nil, url='')
50N/A rugged_commit = get_commit(commit_oid)
50N/A if !rugged_commit && url.empty?
50N/A nil
50N/A else
50N/A get_current_file_rugged(rugged_commit, url)
50N/A end
0N/A end
55N/A
217N/A def get_url(oid=nil, url=nil)
54N/A url ||= ''
26N/A url = url[0..-2] if(url[-1] == '/')
0N/A raise URLNotFoundError.new unless path_exists?(oid, url)
0N/A
52N/A url
22N/A end
119N/A
119N/A def self.directory(path)
145N/A path.split("/")[0..-2].join("/")
260N/A end
0N/A
22N/A def get_branches
46N/A @repo.refs.map do |r|
258N/A {
22N/A refname: r.name,
114N/A name: r.name.split('/')[-1],
26N/A oid: r.target
23N/A }
23N/A end
26N/A end
26N/A
157N/A def build_target_path(url, file_name)
135N/A file_path = url.dup
157N/A file_path << '/' if file_path[-1] != '/' && !file_path.empty?
26N/A file_path << file_name
135N/A
14N/A file_path
145N/A end
260N/A
145N/A def is_head?(commit_oid=nil)
145N/A commit_oid == nil || (!@repo.empty? && commit_oid == head_oid)
145N/A end
145N/A
145N/A def head_oid
145N/A @repo.head.target
145N/A end
145N/A
204N/A
204N/A protected
204N/A
204N/A def path_exists_rugged?(rugged_commit, url='')
260N/A if url.empty?
204N/A true
204N/A else
204N/A tree = rugged_commit.tree
260N/A nil != get_object(rugged_commit, url)
204N/A end
204N/A rescue Rugged::OdbError
204N/A false
260N/A end
204N/A
204N/A def get_current_file_rugged(rugged_commit, url='')
204N/A return nil unless path_exists_rugged?(rugged_commit, url)
204N/A
204N/A object = get_object(rugged_commit, url)
260N/A
204N/A if object.type == :blob
204N/A filename = url.split('/')[-1]
260N/A mime_info = mime_info(filename)
204N/A {
204N/A name: filename,
204N/A size: object.size,
204N/A content: object.content,
204N/A mime_type: mime_info[:mime_type],
145N/A mime_category: mime_info[:mime_category]
30N/A }
14N/A else
174N/A nil
174N/A end
174N/A end
260N/A
260N/A def mime_info(filename)
174N/A ext = File.extname(filename)[1..-1]
215N/A mime_type = Mime::Type.lookup_by_extension(ext) || Mime::TEXT
174N/A mime_category = mime_type.to_s.split('/')[0]
174N/A
174N/A {
26N/A mime_type: mime_type,
145N/A mime_category: mime_category
30N/A }
50N/A end
50N/A
50N/A def head
30N/A @repo.lookup(head_oid)
30N/A end
30N/Aend
30N/A