Class: GitSpelunk::FileContext

Inherits:
Object
  • Object
show all
Defined in:
lib/git_spelunk/file_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ FileContext

Returns a new instance of FileContext.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/git_spelunk/file_context.rb', line 10

def initialize(file, options = {})
  @sha = options[:sha] || 'HEAD'
  @line_number = options[:line_number] || 1

  @repo = options.fetch(:repo) do
    find_repo_from_file(file)
  end

  @file = File.expand_path(file).sub(@repo.workdir, '')
  @commit_cache = {}
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/git_spelunk/file_context.rb', line 8

def file
  @file
end

#line_numberObject

Returns the value of attribute line_number.



7
8
9
# File 'lib/git_spelunk/file_context.rb', line 7

def line_number
  @line_number
end

#repoObject (readonly)

Returns the value of attribute repo.



8
9
10
# File 'lib/git_spelunk/file_context.rb', line 8

def repo
  @repo
end

#shaObject (readonly)

Returns the value of attribute sha.



8
9
10
# File 'lib/git_spelunk/file_context.rb', line 8

def sha
  @sha
end

Instance Method Details

#clone_for_blame_line(blame_line) ⇒ Object



22
23
24
25
# File 'lib/git_spelunk/file_context.rb', line 22

def clone_for_blame_line(blame_line)
  new_sha = blame_line.sha + "~1"
  GitSpelunk::FileContext.new(blame_line.filename, {:sha => new_sha, :repo => @repo, :file => blame_line.filename})
end

#find_repo_from_file(file) ⇒ Object



32
33
34
# File 'lib/git_spelunk/file_context.rb', line 32

def find_repo_from_file(file)
  Rugged::Repository.discover(file)
end

#get_blameObject



36
37
38
39
40
41
42
# File 'lib/git_spelunk/file_context.rb', line 36

def get_blame
  @blame_data ||= begin
    @new_to_old = {}
    @line_to_sha = {}
    GitSpelunk::Blame.new(@repo, @file, @sha).lines
  end
end

#get_line_commit_info(blame_line) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/git_spelunk/file_context.rb', line 44

def get_line_commit_info(blame_line)
  get_blame
  commit = blame_line.commit
  return nil unless commit
  author = commit.author

  author_info = "%s %s" % [author[:name], author[:email]]
  short_message = commit.message.split("\n").find { |x| !x.strip.empty? } || ''
  utc = author[:time]
  {
    commit: commit.oid,
    author: author_info,
    date: author[:time].to_s,
    message: commit.message
  }
end

#get_line_for_sha_parent(blame_line) ⇒ Object



27
28
29
30
# File 'lib/git_spelunk/file_context.rb', line 27

def get_line_for_sha_parent(blame_line)
  o = GitSpelunk::Offset.new(@repo, blame_line.filename, blame_line.sha, blame_line.old_line_number)
  o.line_number_to_parent
end