Class: Metior::Adapter::Grit::Repository

Inherits:
Repository show all
Defined in:
lib/metior/adapter/grit/repository.rb

Overview

Represents a Git source code repository

Author:

Instance Attribute Summary

Attributes inherited from Repository

#path

Instance Method Summary (collapse)

Methods inherited from Repository

#actor, #authors, #branches, #build_commits, #cached_commits, #commits, #committers, #description, #file_stats, #line_history, #name, #parse_range, #report, #significant_authors, #significant_commits, #tags, #top_authors

Methods included from Metior::AutoIncludeAdapter

included

Constructor Details

- (Repository) initialize(path)

Creates a new Git repository based on the given path

This creates a new Grit::Repo instance to interface with the repository.

Parameters:

  • path (String)

    The file system path of the repository



23
24
25
26
27
# File 'lib/metior/adapter/grit/repository.rb', line 23

def initialize(path)
  super path

  @grit_repo = ::Grit::Repo.new(path)
end

Instance Method Details

- (Object) current_branch

Returns the current branch of the repository

This is the target ref of Git's HEAD, i.e. the currently checked out branch. For a detached HEAD this may also be the commit ID of the checked out commit.

See Also:

  • Grit::Repo#head


36
37
38
39
40
41
# File 'lib/metior/adapter/grit/repository.rb', line 36

def current_branch
  branch = @grit_repo.head
  return branch.name unless branch.nil?
  commit = @grit_repo.commit('HEAD')
  commit.id unless commit.nil?
end

- (String) id_for_ref(ref)

Returns the unique identifier for the commit the given reference ??? like a branch name ??? is pointing to

Returns the given ref name immediately if it is a full SHA1 commit ID.

Parameters:

  • ref (String)

    A symbolic reference name

Returns:

  • (String)

    The SHA1 ID of the commit the reference is pointing to



50
51
52
53
54
55
56
57
58
# File 'lib/metior/adapter/grit/repository.rb', line 50

def id_for_ref(ref)
  return ref if ref.match(/[0-9a-f]{40}/)
  unless @refs.key? ref
    options = { :timeout => false }
    sha = @grit_repo.git.native(:rev_parse, options, "#{ref}^{}")
    @refs[ref] = sha.rstrip
  end
  @refs[ref]
end

- (Hash<String, String>) load_branches (private)

Loads all branches and the corresponding commit IDs of this repository

Returns:

  • (Hash<String, String>)

    The names of all branches and the corresponding commit IDs

See Also:

  • Grit::Repo#branches


106
107
108
# File 'lib/metior/adapter/grit/repository.rb', line 106

def load_branches
  Hash[@grit_repo.branches.map { |b| [b.name, b.commit.id] }]
end

- (Grit::Commit, ...) load_commits(range) (private)

Note:

Grit will choke on huge repositories, like Homebrew or the Linux kernel. You will have to raise the timeout limit using Grit.git_timeout=.

This method uses Grit to load all commits from the given commit range

Because of some Grit internal limitations, the commits have to be loaded in batches of up to 300 commits.

Parameters:

  • range (String, Range)

    The range of commits for which the commits should be loaded. This may be given as a string ('master..development'), a range ('master'..'development') or as a single ref ('master'). A single ref name means all commits reachable from that ref.

Returns:

  • (Grit::Commit, nil)

    The base commit of the requested range or nil if the the range starts at the beginning of the history

  • (Array<Grit::Commit>)

    All commits in the given commit range

See Also:

  • Grit::Repo#commits


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/metior/adapter/grit/repository.rb', line 127

def load_commits(range)
  if range.first == ''
    base_commit = nil
    range = range.last
  else
    base_commit = @grit_repo.commit(range.first)
    range = '%s..%s' % [range.first, range.last]
  end

  options = { :pretty => 'raw', :timeout => false }
  output = @grit_repo.git.native :rev_list, options, range
  commits = ::Grit::Commit.list_from_string @grit_repo, output

  [base_commit, commits]
end

- (Hash<String, Array<Fixnum>] An array of two number (line additions and deletions) for each of the given commit IDs) load_line_stats(ids)

Loads the line stats for the commits given by a set of commit IDs

Parameters:

  • ids (Array<String>)

    The IDs of the commits to load line stats for

Returns:

  • (Hash<String, Array<Fixnum>] An array of two number (line additions and deletions) for each of the given commit IDs)

    Hash] An array of two number (line additions and deletions) for each of the given commit IDs



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/metior/adapter/grit/repository.rb', line 65

def load_line_stats(ids)
  if ids.is_a? Range
    if ids.first == ''
      range = ids.last
    else
      range = '%s..%s' % [ids.first, ids.last]
    end

    options = { :numstat => true, :timeout => false }
    output = @grit_repo.git.native :log, options, range
    commit_stats = ::Grit::CommitStats.list_from_string @grit_repo, output
  else
    commit_stats = []
    ids.each_slice(500) do |id_slice|
      options = { :numstat => true, :timeout => false }
      output = @grit_repo.git.native :log, options, *id_slice
      commit_stats += ::Grit::CommitStats.list_from_string @grit_repo, output
    end
  end

  Hash[commit_stats.map do |stats|
    [stats.first, [stats.last.additions, stats.last.deletions]]
  end]
end

- (Object) load_name_and_description (private) Also known as: load_description, load_name

Loads both the name and description of the project contained in the repository from the description file in GIT_DIR. The first line of that file is used as the project's name, the remaining text is used as a description of the project.

See Also:



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/metior/adapter/grit/repository.rb', line 151

def load_name_and_description
  description = @grit_repo.description
  if description.start_with? 'Unnamed repository'
    @name        = ''
    @description = ''
  else
    description  = description.lines.to_a
    @name        = description.shift.strip
    @description = description.join("\n").strip
  end
end

- (Hash<String, String>) load_tags (private)

Loads all tags and the corresponding commit IDs of this repository

Returns:

  • (Hash<String, String>)

    The names of all tags and the corresponding commit IDs

See Also:

  • Grit::Repo#tags


170
171
172
# File 'lib/metior/adapter/grit/repository.rb', line 170

def load_tags
  Hash[@grit_repo.tags.map { |b| [b.name, b.commit.id] }]
end

- (Grit::Commit) raw_commit(id)

Retrieves a raw commit object for the given commit ID

Parameters:

  • id (String)

    The ID of the commit

Returns:

See Also:

  • Grit::Repo#commit


95
96
97
# File 'lib/metior/adapter/grit/repository.rb', line 95

def raw_commit(id)
  @grit_repo.commit(id)
end