Class: Metior::Git::Repository
- Inherits:
-
Repository
- Object
- Repository
- Metior::Git::Repository
- Defined in:
- lib/metior/git/repository.rb
Overview
Represents a Git source code repository
Instance Attribute Summary
Attributes inherited from Repository
Instance Method Summary collapse
-
#id_for_ref(ref) ⇒ String
private
Returns the unique identifier for the commit the given reference – like a branch name – is pointing to.
-
#initialize(path) ⇒ Repository
constructor
Creates a new Git repository based on the given path.
-
#load_branches ⇒ Hash<String, String>
private
Loads all branches and the corresponding commit IDs of this repository.
-
#load_commits(range) ⇒ Grit::Commit, ...
private
This method uses Grit to load all commits from the given commit range.
-
#load_name_and_description ⇒ Object
(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
. -
#load_tags ⇒ Hash<String, String>
private
Loads all tags and the corresponding commit IDs of this repository.
-
#raw_commit(id) ⇒ Grit::Commit
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, #significant_authors, #significant_commits, #tags, #top_authors
Methods included from AutoIncludeVCS
Constructor Details
#initialize(path) ⇒ Repository
Creates a new Git repository based on the given path
This creates a new Grit::Repo
instance to interface with the
repository.
25 26 27 28 29 |
# File 'lib/metior/git/repository.rb', line 25 def initialize(path) super path @grit_repo = Grit::Repo.new(path) end |
Instance Method Details
#id_for_ref(ref) ⇒ String (private)
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.
49 50 51 52 53 54 55 |
# File 'lib/metior/git/repository.rb', line 49 def id_for_ref(ref) return ref if ref.match(/[0-9a-f]{40}/) unless @refs.key? ref @refs[ref] = @grit_repo.git.rev_parse({}, "#{ref}^{}") end @refs[ref] end |
#load_branches ⇒ Hash<String, String> (private)
Loads all branches and the corresponding commit IDs of this repository
62 63 64 |
# File 'lib/metior/git/repository.rb', line 62 def load_branches Hash[@grit_repo.branches.map { |b| [b.name, b.commit.id] }] end |
#load_commits(range) ⇒ Grit::Commit, ... (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 500 commits.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/metior/git/repository.rb', line 83 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 commits = [] skip = 0 begin commits += @grit_repo.commits(range, 500, skip) skip += 500 end while commits.size == skip [base_commit, commits] end |
#load_name_and_description ⇒ Object (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.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/metior/git/repository.rb', line 110 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 |
#load_tags ⇒ Hash<String, String> (private)
Loads all tags and the corresponding commit IDs of this repository
129 130 131 |
# File 'lib/metior/git/repository.rb', line 129 def Hash[@grit_repo..map { |b| [b.name, b.commit.id] }] end |
#raw_commit(id) ⇒ Grit::Commit
Retrieves a raw commit object for the given commit ID
36 37 38 |
# File 'lib/metior/git/repository.rb', line 36 def raw_commit(id) @grit_repo.commit(id) end |