Class: GitGoggles::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_goggles/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Repository

Returns a new instance of Repository.



3
4
5
# File 'lib/git_goggles/repository.rb', line 3

def initialize(name)
  @name = name
end

Instance Method Details

#_pathObject



69
70
71
# File 'lib/git_goggles/repository.rb', line 69

def _path
  File.join(GitGoggles.root_dir, @name)
end

#_repoObject



73
74
75
76
77
78
79
# File 'lib/git_goggles/repository.rb', line 73

def _repo
  if exists?
    @repo ||= Grit::Repo.new(_path, :is_bare => true)
  else
    nil
  end
end

#branch(branch_name) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/git_goggles/repository.rb', line 11

def branch(branch_name)
  if branch = _repo.branches.find { |branch| branch.name == branch_name }
    {
      :name => branch.name.force_encoding("ISO-8859-1").encode("UTF-8"),
      :latest_commit => branch.commit.sha.force_encoding("ISO-8859-1").encode("UTF-8")
    }
  end
end

#branchesObject



7
8
9
# File 'lib/git_goggles/repository.rb', line 7

def branches
  _repo.branches.map(&:name)
end

#commit(sha) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/git_goggles/repository.rb', line 34

def commit(sha)
  if commit = _repo.commit(sha)
    {
      :author => "#{commit.author.name.force_encoding("ISO-8859-1").encode("UTF-8")} <#{commit.author.email.force_encoding("ISO-8859-1").encode("UTF-8")}>",
      :message => commit.message.force_encoding("ISO-8859-1").encode("UTF-8"),
      :date => commit.date.to_s.force_encoding("ISO-8859-1").encode("UTF-8"),
      :diffs => commit.diffs.map { |diff| diff.diff.force_encoding("ISO-8859-1").encode("UTF-8") }
    }
  end
end

#commitsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git_goggles/repository.rb', line 20

def commits
  output = []

  _repo.commits('master', 100).each do |commit|
    output << {
      :author => "#{commit.author.name.force_encoding("ISO-8859-1").encode("UTF-8")} <#{commit.author.email.force_encoding("ISO-8859-1").encode("UTF-8")}>",
      :message => commit.message.force_encoding("ISO-8859-1").encode("UTF-8"),
      :sha => commit.id.force_encoding("ISO-8859-1").encode("UTF-8")
    }
  end

  output
end

#exists?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/git_goggles/repository.rb', line 45

def exists?
  File.exists?(_path)
end

#tag(tag_name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/git_goggles/repository.rb', line 53

def tag(tag_name)
  if tag = _repo.tags.find { |tag| tag.name == tag_name }
    {
      :name => tag.name.force_encoding("ISO-8859-1").encode("UTF-8"),
      :commit => tag.commit.sha.force_encoding("ISO-8859-1").encode("UTF-8")
    }
  end
end

#tagsObject



49
50
51
# File 'lib/git_goggles/repository.rb', line 49

def tags
  _repo.tags.map(&:name)
end

#to_jsonObject



62
63
64
65
66
67
# File 'lib/git_goggles/repository.rb', line 62

def to_json
  {
    :name => @name,
    :branches  => _repo.branches.map(&:name)
  }.to_json
end