Class: IssueBeaver::Models::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/issue_beaver/models/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, repo_name) ⇒ Git

Returns a new instance of Git.



7
8
9
10
11
# File 'lib/issue_beaver/models/git.rb', line 7

def initialize(dir, repo_name)
  @root_dir = discover_root_dir(dir)
  @git = Grit::Repo.new(@root_dir)
  @repo_name = repo_name || @git.config['issuebeaver.repository'] || 'remote.origin'
end

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir.



13
14
15
# File 'lib/issue_beaver/models/git.rb', line 13

def root_dir
  @root_dir
end

Instance Method Details

#discover_github_repo(repo_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/issue_beaver/models/git.rb', line 30

def discover_github_repo(repo_name)
  github_repo = nil
  if repo_name.match(/[^\.]+\/[^\.]+/)
    github_repo = repo_name
  else
    url = @git.config["#{repo_name}.url"]
    match = url.match(/git@github\.com:([^\.]+)\.git/) ||
            url.match(/https?:\/\/github\.com\/([^\.]+)\/?/)
    github_repo = match[1] if url
  end
  github_repo
end

#files(dir) ⇒ Object



44
45
46
47
# File 'lib/issue_beaver/models/git.rb', line 44

def files(dir)
  IO.popen(%Q{cd "#{@root_dir}" && git ls-files "#{dir}"}).lazy.memoizing.
    map(&:chomp).map{|file| File.absolute_path(file, @root_dir) }.lazy
end

#github_userObject



21
22
23
# File 'lib/issue_beaver/models/git.rb', line 21

def github_user
  @github_user ||= @git.config['github.user']
end

#head_commitObject



50
51
52
# File 'lib/issue_beaver/models/git.rb', line 50

def head_commit
  @head_commit ||= `git rev-parse HEAD`.chomp
end

#is_ahead?(commit) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/issue_beaver/models/git.rb', line 55

def is_ahead?(commit)
  return false unless commit
  !`git rev-list "#{head_commit}" | grep $(git rev-parse "#{commit}")`.chomp.empty?
end

#labelsObject



25
26
27
# File 'lib/issue_beaver/models/git.rb', line 25

def labels
  @labels ||= (@git.config['issuebeaver.labels'] || "").split(',')
end

#slugObject



16
17
18
# File 'lib/issue_beaver/models/git.rb', line 16

def slug
  @slug ||= discover_github_repo(@repo_name)
end