Class: SocialSnippet::Repository::Drivers::GitRepository

Inherits:
BaseRepository
  • Object
show all
Defined in:
lib/social_snippet/repository/drivers/git_repository.rb

Instance Attribute Summary collapse

Attributes inherited from BaseRepository

#cache_path, #dependencies, #desc, #main, #name, #path, #ref, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRepository

#create_cache, #glob, #has_versions?, #is_cached?, #latest_version, #load_cache, #load_snippet_json, #real_path, #set_url, #short_commit_id, #to_snippet_json, #versions

Constructor Details

#initialize(repo_path, new_ref = nil) ⇒ GitRepository

Returns a new instance of GitRepository


9
10
11
12
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 9

def initialize(repo_path, new_ref = nil)
  @rugged_repo = ::Rugged::Repository.new(repo_path)
  super(repo_path)
end

Instance Attribute Details

#rugged_repoObject (readonly)

Returns the value of attribute rugged_repo


7
8
9
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 7

def rugged_repo
  @rugged_repo
end

Class Method Details

.download(uri, dest_path = nil) ⇒ Object

Returns path

Returns:

  • path


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 54

def download(uri, dest_path = nil)
  if dest_path.nil?
    dest_path = Dir.mktmpdir
  end

  # git clone
  cloned_repo = ::Rugged::Repository.clone_at uri.to_s, dest_path
  cloned_repo_files = cloned_repo.head.target.tree.map {|item| item[:name] }

  # check snippet.json
  unless cloned_repo_files.include?("snippet.json")
    raise "ERROR: Not found snippet.json in the repository"
  end

  cloned_repo.close
  dest_path
end

Instance Method Details

#checkout(ref_name) ⇒ Object


28
29
30
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 28

def checkout(ref_name)
  rugged_repo.checkout ref_name, :strategy => [:force]
end

#commit_idObject


24
25
26
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 24

def commit_id
  rugged_repo.head.target_id
end

#current_refObject


14
15
16
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 14

def current_ref
  rugged_repo.head.name.gsub /^refs\/heads\//, ""
end

#refsObject


32
33
34
35
36
37
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 32

def refs
  refs = []
  refs.concat remote_refs
  refs.concat tags
  return refs
end

#remote_refsObject


39
40
41
42
43
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 39

def remote_refs
  rugged_repo.references
    .select {|ref| /^refs\/remotes\/origin\// === ref.name }
    .map {|ref| /^refs\/remotes\/origin\/(.*)/.match(ref.name)[1] }
end

#tagsObject


45
46
47
48
49
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 45

def tags
  rugged_repo.references
    .select {|ref| /^refs\/tags\// === ref.name }
    .map {|ref| /^refs\/tags\/(.*)/.match(ref.name)[1] }
end

#updateObject


18
19
20
21
22
# File 'lib/social_snippet/repository/drivers/git_repository.rb', line 18

def update
  # TODO: write tests
  fetch_status = rugged_repo.fetch("origin")
  fetch_status[:received_bytes]
end