Module: HappyCommit::OnCommit::ClassMethods

Defined in:
lib/happy-commit/on_commit.rb

Instance Method Summary collapse

Instance Method Details

#commitsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/happy-commit/on_commit.rb', line 35

def commits
  commits = {}
  order = []
  count = 0
  `git shortlog  -s -n -m HEAD`.each_line do |line|
    if line =~ /\s+(\d+)\s+(.+)$/
      name = $2.strip
      commits[name] = $1.to_i
      order << name
      count += 1

      break if count >= 5
    end
  end

  [order, commits]
end

#commits_todayObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/happy-commit/on_commit.rb', line 53

def commits_today
  hours = Time.now.strftime("%k").to_i-5
  order = []
  count = 0
  commits = {}
  `git shortlog  -s -n -m --since="#{hours} hours ago" HEAD`.each_line do |line|
    if line =~ /\s+(\d+)\s+(.+)$/
      name = $2.strip
      commits[name] = $1.to_i
      order << name
      count += 1

      break if count >= 5
    end
  end

  [order, commits]
end

#current_nameObject



31
32
33
# File 'lib/happy-commit/on_commit.rb', line 31

def current_name
  @current_name||=`git config user.name`.strip
end

#on_commitObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/happy-commit/on_commit.rb', line 10

def on_commit
  order, commits = self.commits
  pos = order.index(current_name)+1

  if pos == 1
    puts ">> Congrats!! you are the most committer of all time with #{commits[current_name]} commits!"
  elsif pos == 2
    puts ">> You need #{commits[order.first] - commits[current_name]} commits to reach #{order.first}(the champion)"
  end

  order, commits = self.commits_today
  pos = order.index(current_name)+1
  if pos == 1
    puts ">> Congrats!! you are the most committer today with #{commits[current_name]} commits!"
  elsif pos == 2
    puts ">> You need #{commits[order.first] - commits[current_name]} commits to reach #{order.first}(the champion)"
  elsif pos
    puts ">> You have committed #{commits[current_name]} times today, congrats!"
  end
end