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.



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

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

  @repo = options.fetch(:repo) do
    repo_directory = find_repo_from_file(file)
    @file = file.sub(%r{^#{repo_directory}/}, '')
    Grit::Repo.new(repo_directory)
  end

  @file ||= options.fetch(:file)
  @commit_cache = {}
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#line_numberObject

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

#shaObject (readonly)

Returns the value of attribute sha.



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

def sha
  @sha
end

Instance Method Details

#clone_for_parent_sha(line_number) ⇒ Object



24
25
26
27
# File 'lib/git_spelunk/file_context.rb', line 24

def clone_for_parent_sha(line_number)
  new_sha = sha_for_line(line_number) + "~1"
  GitSpelunk::FileContext.new(@file, {:sha => new_sha, :repo => @repo, :file => @file})
end

#find_repo_from_file(file) ⇒ Object



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

def find_repo_from_file(file)
  file = './' + file unless file.start_with?('/')
  targets = file.split('/')
  targets.pop
  while !File.directory?(targets.join("/") + "/.git")
    targets.pop
  end

  if targets.empty?
    nil
  else
    targets.join("/")
  end
end

#get_blameObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/git_spelunk/file_context.rb', line 58

def get_blame
  @blame_data ||= begin
    @new_to_old = {}
    @line_to_sha = {}
    blame = Grit::Blame.new(@repo, @file, @sha)
    blame.lines.map do |line|
      @new_to_old[line.lineno] = line.oldlineno
      [line.commit.id_abbrev, line.line]
    end
  end
  @blame_data
end

#get_line_commit_info(line) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/git_spelunk/file_context.rb', line 75

def get_line_commit_info(line)
  get_blame
  abbrev = sha_for_line(line)
  commit = (@commit_cache[abbrev] ||= @repo.commit(abbrev))
  return nil unless commit

  author_info = commit.author_string.split(" ")
  tz = author_info.pop
  utc = Time.at(author_info.pop.to_i)
  [
    "commit " + commit.id,
    "Author: " + author_info.join(" "),
    "Date: " + utc.to_s
  ].join("\n") + "\n\n" + "     " + commit.short_message
end

#get_line_for_sha_parent(line_number) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/git_spelunk/file_context.rb', line 29

def get_line_for_sha_parent(line_number)
  o = GitSpelunk::Offset.new(@repo, @file, sha_for_line(line_number), @new_to_old[line_number])
  line = o.line_number_to_parent
  if line
    line
  else
    if o.at_beginning_of_time?
      :at_beginning_of_time
    elsif o.unable_to_trace_lineage?
      :unable_to_trace
    end
  end
end

#sha_for_line(line) ⇒ Object



71
72
73
# File 'lib/git_spelunk/file_context.rb', line 71

def sha_for_line(line)
  @blame_data[line - 1][0]
end