Module: GitTimer
- Extended by:
- GitTimer
- Included in:
- GitTimer
- Defined in:
- lib/git_timer.rb,
lib/git_timer/cli.rb,
lib/git_timer/version.rb
Defined Under Namespace
Classes: Timer
Constant Summary collapse
- MAIN_PATH =
'./user-log'- PRE_PUSH_PATH =
'.git/hooks/pre-push'- POST_CHECKOUT_PATH =
'.git/hooks/post-checkout'- LOG_TITLE =
"# Git log \n"- VERSION =
'0.0.1'
Instance Method Summary collapse
- #current_branch_and_ticket_id ⇒ Object
- #failed_push?(ticket_id) ⇒ Boolean
- #initialize_git_hook(hook_path, hook_method) ⇒ Object
- #is_new_branch?(current_branch, previous_head, new_head) ⇒ Boolean
- #main ⇒ Object
- #post_checkout ⇒ Object
- #pre_push ⇒ Object
- #register_activity(ticket_info) ⇒ Object
Instance Method Details
#current_branch_and_ticket_id ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/git_timer.rb', line 72 def current_branch_and_ticket_id list_of_branches = `git branch`.split("\n") current_branch = list_of_branches.find { |string| string.match(/^\* /) }.sub('* ', '') ticket_id = current_branch.match(/^([^_]*)_?/) ticket_id = ticket_id.captures.first if ticket_id != nil [current_branch, ticket_id] end |
#failed_push?(ticket_id) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/git_timer.rb', line 80 def failed_push?(ticket_id) last_entry = `tail -n 1 #{MAIN_PATH}` return false if last_entry == LOG_TITLE last_entry = last_entry.match(/^(\w*) \| (\w* \w*) \| (.*)/).captures time_diff = (Time.now - Time.parse(last_entry[2])) / 60 last_entry[0] == ticket_id && last_entry[1] == 'Code review' && time_diff < 11 end |
#initialize_git_hook(hook_path, hook_method) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/git_timer.rb', line 23 def initialize_git_hook(hook_path, hook_method) content = "#!/usr/bin/env ruby require 'git_timer' GitTimer.#{hook_method} " File.open(hook_path, 'w+') do |f| f.write content end FileUtils.chmod('+x', hook_path) end |
#is_new_branch?(current_branch, previous_head, new_head) ⇒ Boolean
65 66 67 68 69 70 |
# File 'lib/git_timer.rb', line 65 def is_new_branch?(current_branch, previous_head, new_head) # if the refs of the previous and new heads are the same # and the number of checkouts equals one, a new branch has been created num_checkouts = `git reflog --date=local | grep -o #{current_branch} | wc -l`.strip.to_i previous_head == new_head && num_checkouts == 1 end |
#main ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/git_timer.rb', line 13 def main content = LOG_TITLE File.open(MAIN_PATH, 'w+') do |f| f.write content end File.chmod(0777, MAIN_PATH) initialize_git_hook(POST_CHECKOUT_PATH, 'post_checkout') initialize_git_hook(PRE_PUSH_PATH, 'pre_push') end |
#post_checkout ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/git_timer.rb', line 37 def post_checkout current_branch, ticket_id = current_branch_and_ticket_id previous_head = ARGV[0] new_head = ARGV[1] is_file_checkout = ARGV[2] == '0' if is_file_checkout puts 'This is a file checkout. Nothing to do.' exit 0 end if is_new_branch?(current_branch, previous_head, new_head) register_activity({ ticket_id: ticket_id, ticket_state: 'In progress' }) end end |
#pre_push ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/git_timer.rb', line 51 def pre_push _, ticket_id = current_branch_and_ticket_id if failed_push?(ticket_id) new_lines = File.readlines(MAIN_PATH) new_lines.pop File.open(MAIN_PATH, 'w') do |f| new_lines.each do |line| f.write line end end end register_activity({ ticket_id: ticket_id, ticket_state: 'Code review' }) end |
#register_activity(ticket_info) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/git_timer.rb', line 88 def register_activity(ticket_info) ticket_id = ticket_info[:ticket_id] ticket_state = ticket_info[:ticket_state] File.open(MAIN_PATH, 'a') do |f| f.puts "#{ticket_id} | #{ticket_state} | #{Time.now}" end end |