Class: Metior::Git::Repository

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

Overview

Represents a Git source code repository

Author:

  • Sebastian Staudt

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, #significant_authors, #significant_commits, #tags, #top_authors

Methods included from AutoIncludeVCS

included

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.

Parameters:

  • path (String)

    The file system path of 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.

Parameters:

  • ref (String)

    A symbolic reference name

Returns:

  • (String)

    The SHA1 ID of the commit the reference is pointing to



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_branchesHash<String, String> (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


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)

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 500 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


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_descriptionObject (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:



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_tagsHash<String, String> (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


129
130
131
# File 'lib/metior/git/repository.rb', line 129

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

#raw_commit(id) ⇒ Grit::Commit

Retrieves a raw commit object for the given commit ID

Parameters:

  • id (String)

    The ID of the commit

Returns:

  • (Grit::Commit)

    The commit object

See Also:

  • Grit::Repo#commit


36
37
38
# File 'lib/metior/git/repository.rb', line 36

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