Class: Contributions::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/contributions/git.rb

Constant Summary collapse

ENDING =
" CONTRIBUTIONS_ENDING "
KEYS =
[:sha, :date, :subject, :body]
SEPARATOR =
" CONTRIBUTIONS_SEPARATOR "

Class Method Summary collapse

Class Method Details

.clone(repository, &block) ⇒ Object

Public: Clone a repository and run the block passed inside the newly cloned repository.

repository - a ‘username/repository_name’ string. block - a block to be executed inside the newly cloned

directory.

Returns the return value of the block.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/contributions/git.rb', line 33

def self.clone(repository, &block)
  value = ''
  repo_name = repository.match(/([^\/]*$)/)[1]
  Dir.mktmpdir(repo_name) do |dir|
    cloned_repo_name = dir + '/' + repo_name
    system "git clone -q https://github.com/#{repository} #{cloned_repo_name}"
    Dir.chdir(cloned_repo_name) do
      value = yield
    end

    FileUtils.rm_rf(cloned_repo_name)
  end

  value
end

.contributions(user, repository) ⇒ Object

Public: Get all the contributions in a repository by a user (contributions for which the user is the author).

user - a user’s name (the name that shows up as the committer or

author---e.g., 'John Smith'

repository - a ‘username/repository_name’ string.

Returns an Array of Hashes with keys for :sha, :date, :subject, :body



20
21
22
23
# File 'lib/contributions/git.rb', line 20

def self.contributions(user, repository)
  log = self.clone(repository) { self.read_log(user) }
  StringUtils.string_to_hash(log, KEYS, SEPARATOR, ENDING)
end

.log_formatObject



60
61
62
# File 'lib/contributions/git.rb', line 60

def self.log_format
  ["%h", "%ci", "%s", "%b"].join(SEPARATOR) << ENDING
end

.read_log(user) ⇒ Object

Internal: The command to read the git log.

user - the user’s name.

Returns nothing.



54
55
56
57
58
# File 'lib/contributions/git.rb', line 54

def self.read_log(user)
  # We want a string returned (for parsing); so use read over
  # readlines.
  IO.popen("git log --author='#{user}' --format='#{self.log_format}' --no-color") { |io| io.read }
end