Class: GitSme::CommitLoader
- Inherits:
-
Object
- Object
- GitSme::CommitLoader
- Defined in:
- lib/git_sme/commit_loader.rb
Instance Attribute Summary collapse
-
#branch ⇒ Object
readonly
Returns the value of attribute branch.
-
#commits ⇒ Object
readonly
Returns the value of attribute commits.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
-
#valid ⇒ Object
(also: #valid?)
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
-
#initialize(path_to_repo, branch: 'master', enable_cache: true) ⇒ CommitLoader
constructor
A new instance of CommitLoader.
- #load(force: false) ⇒ Object
- #load! ⇒ Object (also: #reload!)
- #new_commits ⇒ Object
- #new_commits? ⇒ Boolean
Constructor Details
#initialize(path_to_repo, branch: 'master', enable_cache: true) ⇒ CommitLoader
Returns a new instance of CommitLoader.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/git_sme/commit_loader.rb', line 15 def initialize(path_to_repo, branch: 'master', enable_cache: true) @branch = branch @enable_cache = enable_cache @commits = [] @loaded = false @valid = true begin @repo = Rugged::Repository.new(File.(path_to_repo)) @branch = 'master' if @repo.branches[@branch].nil? @cache = GitSme::Cache.new(@repo.path.gsub('/.git/', ''), enabled: @enable_cache, file_suffix: "#{@branch}-commits" ) rescue Rugged::RepositoryError => e @valid = false = e. end end |
Instance Attribute Details
#branch ⇒ Object (readonly)
Returns the value of attribute branch.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def branch @branch end |
#commits ⇒ Object (readonly)
Returns the value of attribute commits.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def commits @commits end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def end |
#loaded ⇒ Object (readonly) Also known as: loaded?
Returns the value of attribute loaded.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def loaded @loaded end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def repo @repo end |
#valid ⇒ Object (readonly) Also known as: valid?
Returns the value of attribute valid.
10 11 12 |
# File 'lib/git_sme/commit_loader.rb', line 10 def valid @valid end |
Instance Method Details
#load(force: false) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/git_sme/commit_loader.rb', line 35 def load(force: false) return unless valid? return if loaded? && !force @commits = @cache.load @last_commit_idx = @commits.size - 1 @commit_count = `cd #{@repo.path.gsub('/.git/', '')} && git rev-list --count #{@branch}`.to_i walker = Rugged::Walker.new(@repo) walker.push(@repo.branches[@branch].target_id) if @enable_cache && @commits.size > 0 appending_to_cache = true oldest_cached_commit = @commits[-1] oldest_cached_commit_sha = oldest_cached_commit ? oldest_cached_commit[:sha1] : nil walker.sorting(Rugged::SORT_REVERSE) end new_commits = [] walker.each do |commit| break if appending_to_cache && commit.oid == oldest_cached_commit_sha process_commit(appending_to_cache, commit, new_commits) do |new_commit_count, processed_commit_count, all_commit_count| yield(new_commit_count, processed_commit_count, all_commit_count) end end walker.reset @commits.concat(new_commits.reverse) if new_commits.any? @cache.save(@commits) @loaded = true end |
#load! ⇒ Object Also known as: reload!
73 74 75 |
# File 'lib/git_sme/commit_loader.rb', line 73 def load! load(force: true) end |
#new_commits ⇒ Object
84 85 86 87 88 |
# File 'lib/git_sme/commit_loader.rb', line 84 def new_commits return [] unless new_commits? @commits.slice(@last_commit_idx, @commits.size) end |
#new_commits? ⇒ Boolean
78 79 80 81 82 |
# File 'lib/git_sme/commit_loader.rb', line 78 def new_commits? return false if @last_commit_idx.nil? @last_commit_idx > 0 && @last_commit_idx < (@commits.size - 1) end |