Class: Metior::Adapter::Grit::Repository
- Inherits:
-
Repository
- Object
- Repository
- Metior::Adapter::Grit::Repository
- Defined in:
- lib/metior/adapter/grit/repository.rb
Overview
Represents a Git source code repository
Instance Attribute Summary
Attributes inherited from Repository
Instance Method Summary (collapse)
-
- (Object) current_branch
Returns the current branch of the repository.
-
- (String) id_for_ref(ref)
Returns the unique identifier for the commit the given reference ??? like a branch name ??? is pointing to.
-
- (Repository) initialize(path)
constructor
Creates a new Git repository based on the given path.
-
- (Hash<String, String>) load_branches
private
Loads all branches and the corresponding commit IDs of this repository.
-
- (Grit::Commit, ...) load_commits(range)
private
This method uses Grit to load all commits from the given commit range.
-
- (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.
-
- (Object) load_name_and_description
(also: #load_description, #load_name)
private
Loads both the name and description of the project contained in the repository from the description file in
GIT_DIR. -
- (Hash<String, String>) load_tags
private
Loads all tags and the corresponding commit IDs of this repository.
-
- (Grit::Commit) raw_commit(id)
Retrieves a raw commit object for the given commit ID.
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
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.
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.
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.
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 = { :timeout => false } sha = @grit_repo.git.native(:rev_parse, , "#{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
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)
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.
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 = { :pretty => 'raw', :timeout => false } output = @grit_repo.git.native :rev_list, , 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
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 = { :numstat => true, :timeout => false } output = @grit_repo.git.native :log, , range commit_stats = ::Grit::CommitStats.list_from_string @grit_repo, output else commit_stats = [] ids.each_slice(500) do |id_slice| = { :numstat => true, :timeout => false } output = @grit_repo.git.native :log, , *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.
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
170 171 172 |
# File 'lib/metior/adapter/grit/repository.rb', line 170 def Hash[@grit_repo..map { |b| [b.name, b.commit.id] }] end |
- (Grit::Commit) raw_commit(id)
Retrieves a raw commit object for the given commit ID
95 96 97 |
# File 'lib/metior/adapter/grit/repository.rb', line 95 def raw_commit(id) @grit_repo.commit(id) end |