Class: GitSme::CommitLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/git_sme/commit_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(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
    @error_message = e.message
  end
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



10
11
12
# File 'lib/git_sme/commit_loader.rb', line 10

def branch
  @branch
end

#commitsObject (readonly)

Returns the value of attribute commits.



10
11
12
# File 'lib/git_sme/commit_loader.rb', line 10

def commits
  @commits
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



10
11
12
# File 'lib/git_sme/commit_loader.rb', line 10

def error_message
  @error_message
end

#loadedObject (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

#repoObject (readonly)

Returns the value of attribute repo.



10
11
12
# File 'lib/git_sme/commit_loader.rb', line 10

def repo
  @repo
end

#validObject (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_commitsObject



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

Returns:

  • (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