Class: Ginatra::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/ginatra/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Ginatra::Repo

Create a new repository, and sort out clever stuff including assigning the param, the name and the description.

Parameters:

  • path (String)

    a path to the repository you want created



12
13
14
15
16
17
18
19
20
21
# File 'lib/ginatra/repo.rb', line 12

def initialize(path)
  @repo = Rugged::Repository.new(path)
  @param = File.split(path).last
  @name = @param
  @description = ''
  if File.exists?("#{@repo.path}description")
    @description = File.read("#{@repo.path}description").strip
    @description = '' if @description.match(/\AUnnamed repository;/)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

TODO:

update respond_to? method

Catch all



105
106
107
108
109
110
111
# File 'lib/ginatra/repo.rb', line 105

def method_missing(sym, *args, &block)
  if @repo.respond_to?(sym)
    @repo.send(sym, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/ginatra/repo.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ginatra/repo.rb', line 5

def name
  @name
end

#paramObject (readonly)

Returns the value of attribute param.



5
6
7
# File 'lib/ginatra/repo.rb', line 5

def param
  @param
end

Instance Method Details

#branch_exists?(branch_name) ⇒ Boolean

Checks existence of branch by name

Returns:

  • (Boolean)


78
79
80
# File 'lib/ginatra/repo.rb', line 78

def branch_exists?(branch_name)
  @repo.branches.exists?(branch_name)
end

#branchesObject

Returns list of branches sorted by name alphabetically



61
62
63
# File 'lib/ginatra/repo.rb', line 61

def branches
  @repo.branches.each(:local).sort_by {|b| b.name }
end

#branches_with(commit) ⇒ Object

Returns list of branches containing the commit



66
67
68
69
70
71
72
73
74
75
# File 'lib/ginatra/repo.rb', line 66

def branches_with(commit)
  b = []
  branches.each do |branch|
    walker = Rugged::Walker.new(@repo)
    walker.sorting(Rugged::SORT_TOPO)
    walker.push(@repo.ref("refs/heads/#{branch.name}").target)
    walker.collect { |c| b << branch if c.oid == commit }
  end
  b
end

#commit(sha) ⇒ Rugged::Commit

Return a commit corresponding to sha in the repo.

Parameters:

  • sha (String)

    the commit id or tag name

Returns:

  • (Rugged::Commit)

    the commit object



27
28
29
# File 'lib/ginatra/repo.rb', line 27

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

#commit_by_tag(name) ⇒ Object

Return a commit corresponding to tag in the repo.



32
33
34
35
36
37
38
39
40
# File 'lib/ginatra/repo.rb', line 32

def commit_by_tag(name)
  target = @repo.ref("refs/tags/#{name}").target

  if target.is_a? Rugged::Tag::Annotation
    target = target.target
  end

  target
end

#commits(branch = 'master', max_count = 10, skip = 0) ⇒ Array<Rugged::Commit>

Return a list of commits in a certain branch, including pagination options and all the refs.

Parameters:

  • start (String)

    the branch to look for commits in

  • max_count (Integer) (defaults to: 10)

    the maximum count of commits

  • skip (Integer) (defaults to: 0)

    the number of commits in the branch to skip before taking the count.

Returns:

  • (Array<Rugged::Commit>)

    the array of commits.

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/ginatra/repo.rb', line 49

def commits(branch='master', max_count=10, skip=0)
  raise Ginatra::InvalidRef unless branch_exists?(branch)

  walker = Rugged::Walker.new(@repo)
  walker.sorting(Rugged::SORT_TOPO)
  walker.push(@repo.ref("refs/heads/#{branch}").target)

  commits = walker.collect {|commit| commit }
  commits[skip, max_count]
end

#find_blob(oid) ⇒ Object

Find blob by oid



83
84
85
# File 'lib/ginatra/repo.rb', line 83

def find_blob(oid)
  Rugged::Blob.new @repo, oid
end

#find_tree(oid) ⇒ Object

Find tree by tree oid or branch name



88
89
90
91
92
93
94
95
# File 'lib/ginatra/repo.rb', line 88

def find_tree(oid)
  if branch_exists?(oid)
    last_commit_sha = @repo.ref("refs/heads/#{oid}").target.oid
    lookup(last_commit_sha).tree
  else
    lookup(oid)
  end
end

#respond_to?(sym) ⇒ Boolean

to correspond to the #method_missing definition

Returns:

  • (Boolean)


114
115
116
# File 'lib/ginatra/repo.rb', line 114

def respond_to?(sym)
  @repo.respond_to?(sym) || super
end

#to_ruggedObject

Returns Rugged::Repository instance



98
99
100
# File 'lib/ginatra/repo.rb', line 98

def to_rugged
  @repo
end