Class: Waddup::Source::Git

Inherits:
Waddup::Source show all
Extended by:
Extension::System
Includes:
Extension::System
Defined in:
lib/waddup/sources/git.rb

Constant Summary collapse

ALIAS =
'git'
ICON =
"\xE2\x9C\x8F\xEF\xB8\x8F "
GIT_FORMAT =
'%h %ai %s'
EXTRACT_PATTERN =

Pattern to extract from the above format

/([0-9a-f]{7}) ([-0-9: ]+\+\d{4}) (.+)/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extension::System

os, osx?, run

Methods inherited from Waddup::Source

usable, #usable?

Methods included from Registry

#inherited, #registry

Constructor Details

#initialize(base_path = Dir.pwd) ⇒ Git

Retrieves author and repositories on initialization

Arguments

:base_path (defaults to current working directory)


20
21
22
# File 'lib/waddup/sources/git.rb', line 20

def initialize(base_path = Dir.pwd)
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



12
13
14
# File 'lib/waddup/sources/git.rb', line 12

def base_path
  @base_path
end

Class Method Details

.label_for_repo(repo) ⇒ Object

Generates label for given repo path



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/waddup/sources/git.rb', line 76

def self.label_for_repo(repo)
  path = Pathname.new(repo)
  parent = path.parent

  case parent.basename.to_s
  when 'code', 'design'
    parent = parent.parent
  end

  return nil if parent.root?

  parent.basename.to_s
end

.usable?Boolean

Requires Git to be installed successfully

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/waddup/sources/git.rb', line 91

def self.usable?
  run 'git --version', quietly: true
  $?.success?
end

Instance Method Details

#authorObject

Obtains author from git-config



25
26
27
# File 'lib/waddup/sources/git.rb', line 25

def author
  @author ||= run 'git config --get user.name'
end

#events(from, to) ⇒ Object

Aggregates events from all repositories

Arguments:

:from (datetime)
:to   (datetime)


41
42
43
44
45
46
# File 'lib/waddup/sources/git.rb', line 41

def events(from, to)
  events = repos.map do |repo|
    events_for_repo from, to, repo
  end
  events.flatten!
end

#events_for_repo(from, to, repo) ⇒ Object

Aggregates events for given repository

Arguments:

:from (datetime)
:to   (datetime)
:repo (path)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/waddup/sources/git.rb', line 62

def events_for_repo(from, to, repo)
  repo_label = self.class.label_for_repo repo

  results = run "git --git-dir='#{repo}' log --all --no-merges --author='#{author}' --since='#{from.iso8601}' --until='#{to.iso8601}' --format='format:#{GIT_FORMAT}'"
  results.scan(EXTRACT_PATTERN).map do |hash, datetime, subject|
    Waddup::Event.new do |e|
      e.label  = "[#{repo_label}] #{subject}"
      e.at     = Time.parse(datetime)
      e.source = self
    end
  end
end

#reposObject

Collects repositories under base-path



30
31
32
# File 'lib/waddup/sources/git.rb', line 30

def repos
  @repos ||= Dir["#{base_path}/**/.git"]
end