Class: JekyllLastCommit::RepoMan

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-last-commit/repo_man.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_site) ⇒ RepoMan

Returns a new instance of RepoMan.



8
9
10
# File 'lib/jekyll-last-commit/repo_man.rb', line 8

def initialize(path_site)
  @path_site = Pathname.new(path_site)
end

Instance Method Details

#discover_commits(relative_file_paths) ⇒ Object



27
28
29
30
31
32
33
34
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
72
73
74
75
76
# File 'lib/jekyll-last-commit/repo_man.rb', line 27

def discover_commits(relative_file_paths)
  return if @repo.nil?

  document_paths = relative_file_paths.map {|rfp| generate_relative_path(rfp) }.to_set

  # gets updated with renames pointing to original path
  paths_lookup = document_paths.reduce({}) do |memo, original_path|
    memo[original_path] = original_path
    memo
  end

  # starting with the most recent commit, look for data on paths_undetermined
  walker = Rugged::Walker.new(@repo)
  walker.push(@repo.head.target.oid) # start with most recent commit

  find_similar_opts = {rename_limit: 1000}

  @paths_determined ||= {}

  walker.each_with_index do |commit, i|
    diff = commit.diff

    # ignore renames
    diff.find_similar!(**find_similar_opts)

    diff.deltas.each do |delta|
      new_file_path = delta.new_file[:path]

      original_path = paths_lookup[new_file_path]

      if document_paths.include?(new_file_path)
        if delta.status == :renamed
          # special case
          old_file_path = delta.old_file[:path]

          paths_lookup[old_file_path] = original_path

          # no longer looking for the new file, looking for the old file
          document_paths.delete(new_file_path)
          document_paths.add(old_file_path)
        else
          @paths_determined[original_path] = commit
          document_paths.delete(new_file_path)
        end
      end
    end

    break if document_paths.empty?
  end
end

#discover_repoObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll-last-commit/repo_man.rb', line 12

def discover_repo
  begin
    @repo = Rugged::Repository.discover(@path_site)

    path_dot_git = Pathname.new(@repo.path)
    path_repo = path_dot_git.parent

    # necessary to handle situations where site is not top level in repo
    @path_relative = @path_site.relative_path_from(path_repo)

  rescue
    Jekyll.logger.warn "JekyllLastCommit: unable to find git repository at #{@path_site}"
  end
end

#find_commit(relative_path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-last-commit/repo_man.rb', line 78

def find_commit(relative_path)
  return nil if @repo.nil?

  commit = @paths_determined[generate_relative_path(relative_path)]

  return nil if commit.nil?

  commit_hash = commit.to_hash
                  .tap {|h| h.delete(:tree) }    # just reduce noise; can always add back later
                  .tap {|h| h.delete(:parents) }
                  .transform_keys(&:to_s)        # transform symbols to keys so liquid will render
                  .transform_values {|v| v.is_a?(Hash) ? v.transform_keys(&:to_s) : v.strip! }
  commit_hash['sha'] = commit.oid
  commit_hash['time'] = commit.time
  commit_hash['time_epoch'] = commit.time.to_i

  commit_hash
end