Class: GitSpelunk::FileContext
- Inherits:
-
Object
- Object
- GitSpelunk::FileContext
- Defined in:
- lib/git_spelunk/file_context.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#line_number ⇒ Object
Returns the value of attribute line_number.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
-
#sha ⇒ Object
readonly
Returns the value of attribute sha.
Instance Method Summary collapse
- #clone_for_blame_line(blame_line) ⇒ Object
- #find_repo_from_file(file) ⇒ Object
- #get_blame ⇒ Object
- #get_line_commit_info(blame_line) ⇒ Object
- #get_line_for_sha_parent(blame_line) ⇒ Object
-
#initialize(file, options = {}) ⇒ FileContext
constructor
A new instance of FileContext.
Constructor Details
#initialize(file, options = {}) ⇒ FileContext
Returns a new instance of FileContext.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/git_spelunk/file_context.rb', line 10 def initialize(file, = {}) @sha = [:sha] || 'HEAD' @line_number = [:line_number] || 1 @repo = .fetch(:repo) do repo_directory = find_repo_from_file(file) @file = File.(file).sub(%r{^#{repo_directory}/}, '') Grit::Repo.new(repo_directory) end @file ||= .fetch(:file) @commit_cache = {} end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/git_spelunk/file_context.rb', line 8 def file @file end |
#line_number ⇒ Object
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 |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
8 9 10 |
# File 'lib/git_spelunk/file_context.rb', line 8 def repo @repo end |
#sha ⇒ Object (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
24 25 26 27 |
# File 'lib/git_spelunk/file_context.rb', line 24 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/git_spelunk/file_context.rb', line 34 def find_repo_from_file(file) file = './' + file unless file.start_with?('/') targets = File.(file).split('/') targets.pop while !File.directory?(targets.join("/") + "/.git") targets.pop end if targets.empty? nil else targets.join("/") end end |
#get_blame ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/git_spelunk/file_context.rb', line 49 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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/git_spelunk/file_context.rb', line 57 def get_line_commit_info(blame_line) get_blame commit = blame_line.commit return nil unless commit = commit..split(" ") tz = .pop utc = Time.at(.pop.to_i) [ "commit " + commit.id, "Author: " + .join(" "), "Date: " + utc.to_s ].join("\n") + "\n\n" + " " + commit. end |
#get_line_for_sha_parent(blame_line) ⇒ Object
29 30 31 32 |
# File 'lib/git_spelunk/file_context.rb', line 29 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 |