Class: GitBro::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git-bro/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path, opts = nil) ⇒ Repository

Returns a new instance of Repository.



7
8
9
10
11
12
13
14
15
16
# File 'lib/git-bro/repository.rb', line 7

def initialize(repo_path, opts = nil)
  @repo = Grit::Repo.new(repo_path, opts)
  @name = begin
            dirs = @repo.path.split('/')
            dirs.delete_at(dirs.size - 1) if dirs[-1] == '.git'
            dirs[-1]
          end

  @cache = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/git-bro/repository.rb', line 5

def name
  @name
end

Instance Method Details

#branchesObject



85
86
87
# File 'lib/git-bro/repository.rb', line 85

def branches
  @repo.heads
end

#commit(sha) ⇒ Object



93
94
95
# File 'lib/git-bro/repository.rb', line 93

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

#commits_info(branch, path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/git-bro/repository.rb', line 49

def commits_info(branch, path)
  commits = {}

  cur_time = Time.now
  paths = path.empty? ? [] : [].push(path + '/')
  tree = @repo.tree(branch, paths)

  tree.trees.each do |t|
    object_id = t.id.to_sym
    commits[object_id] = get_commit_info(branch, object_id, t.name, cur_time)
  end

  tree.blobs.each do |b|
    object_id = b.id.to_sym
    commits[object_id] = get_commit_info(branch, object_id, b.name, cur_time)
  end

  return commits
end

#default_branchObject



97
98
99
100
101
102
# File 'lib/git-bro/repository.rb', line 97

def default_branch
  branches = @repo.heads.collect!{|h| h.name }
  return "master" if branches.include?("master")

  branches.first
end

#file_content(branch, filepath) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/git-bro/repository.rb', line 22

def file_content(branch, filepath)
  blobs = @repo.tree(branch, [].push(filepath)).blobs
  if blobs.size != 1
    return 'FILE NOT FOUND'
  end

  blobs.first.data
end

#get_commit_info(branch, object_id, object_name, current_time) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/git-bro/repository.rb', line 31

def get_commit_info(branch, object_id, object_name, current_time)
  if @cache.has_key?(object_id)
    commit_info = @cache[object_id]
  else
    lc = last_commit(branch, object_name)
    commit_info = {
      :author => lc.author.name,
      :commit_time => lc.date,
      :message => lc.message.shortify
    }
    @cache[object_id] = commit_info
  end

  commit_info[:age] = commit_info[:commit_time].relative_to(current_time)

  return commit_info
end

#log(branch, page, per_page = 20) ⇒ Object



89
90
91
# File 'lib/git-bro/repository.rb', line 89

def log(branch, page, per_page = 20)
  @repo.log(branch, nil, {:skip => page * per_page, :n => per_page})
end

#pathObject



18
19
20
# File 'lib/git-bro/repository.rb', line 18

def path
  @repo.path.gsub('.git','')
end

#top_commit(branch) ⇒ Object



104
105
106
# File 'lib/git-bro/repository.rb', line 104

def top_commit(branch)
  @repo.commits(branch, 1).first
end

#tree(branch, paths) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/git-bro/repository.rb', line 69

def tree(branch, paths)
  objects = []
  tree = @repo.tree(branch, paths)

  tree.trees.each do |t|
    object_info = { :type => 'dir', :name => t.basename + '/', :sha => t.id, :commit_info => @cache[t.id.to_sym] }
    objects.push(object_info)
  end
  tree.blobs.each do |b|
    object_info = { :type => 'file', :name => b.basename, :sha => b.id, :commit_info => @cache[b.id.to_sym] }
    objects.push(object_info)
  end

  return objects
end