git_timer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91a127413b1a21f96e9fd3655fd0111b9af9d406
4
+ data.tar.gz: dc5ca37250cc7bbc14e56ce8f0402f757eb90289
5
+ SHA512:
6
+ metadata.gz: 508980173f1e81c052b0d0f4b991d77d7b4a3ea1cae88ce08940d7dac193c58eabf67acf24984c380b2a6b8d34813720cb86da243204dc5d83d39a7a4536e288
7
+ data.tar.gz: 10921b70bb74793cb7f183184457e739809eb1289de08ae1ea65dc9824eb5b764960a2ecaad5a7cc77b4d139a6520b332ec1610a750ecd7a108b017e5113550b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ user-log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in git_timer.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 eagerWorks S.R.L
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # GitTimer
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/git_timer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'git_timer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install git_timer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/git_timer.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "git_timer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/git_timer ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'git_timer'
3
+
4
+ GitTimer.main
5
+ GitTimer::Timer.start(ARGV)
data/git_timer.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "git_timer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_timer"
8
+ spec.version = GitTimer::VERSION
9
+ spec.authors = ["Mauro Mottini", "Guillermo Aguirre"]
10
+ spec.email = ["hello@eagerworks.com"]
11
+
12
+ spec.summary = %q{Simple time and activity tracker for git}
13
+ spec.description = %q{Simple time and activity tracker for git}
14
+ spec.homepage = "https://github.com/eagerworks/git_timer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "thor"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,20 @@
1
+ require 'thor'
2
+
3
+ module GitTimer
4
+ class Timer < Thor
5
+ desc "work ACTION", "This will start or stop your work tracking"
6
+ long_desc <<-WORK
7
+
8
+ `work start` will insert 'Work started | 2018-01-01 00:00:00 -0300' in the git log
9
+
10
+ `work stop` will insert 'Work stopped | 2018-01-01 00:00:00 -0300' in the git log
11
+
12
+ With the timestamp being the current time with the format %yyyy-MM-dd hh:mm:ss time_zone
13
+ WORK
14
+ option :upcase
15
+ def work(action)
16
+ "#{action}".upcase! if options[:upcase]
17
+ puts action
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module GitTimer
2
+ VERSION = '0.0.1'
3
+ end
data/lib/git_timer.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'git_timer/version'
2
+ require 'git_timer/cli'
3
+
4
+ module GitTimer extend self
5
+ require 'fileutils'
6
+ require 'time'
7
+
8
+ MAIN_PATH = './user-log'
9
+ PRE_PUSH_PATH = '.git/hooks/pre-push'
10
+ POST_CHECKOUT_PATH = '.git/hooks/post-checkout'
11
+ LOG_TITLE = "# Git log \n"
12
+
13
+ def main
14
+ content = LOG_TITLE
15
+ File.open(MAIN_PATH, 'w+') do |f|
16
+ f.write content
17
+ end
18
+ File.chmod(0777, MAIN_PATH)
19
+ initialize_git_hook(POST_CHECKOUT_PATH, 'post_checkout')
20
+ initialize_git_hook(PRE_PUSH_PATH, 'pre_push')
21
+ end
22
+
23
+ def initialize_git_hook(hook_path, hook_method)
24
+ content =
25
+ "#!/usr/bin/env ruby
26
+
27
+ require 'git_timer'
28
+
29
+ GitTimer.#{hook_method}
30
+ "
31
+ File.open(hook_path, 'w+') do |f|
32
+ f.write content
33
+ end
34
+ FileUtils.chmod('+x', hook_path)
35
+ end
36
+
37
+ def post_checkout
38
+ current_branch, ticket_id = current_branch_and_ticket_id
39
+ previous_head = ARGV[0]
40
+ new_head = ARGV[1]
41
+ is_file_checkout = ARGV[2] == '0'
42
+ if is_file_checkout
43
+ puts 'This is a file checkout. Nothing to do.'
44
+ exit 0
45
+ end
46
+ if is_new_branch?(current_branch, previous_head, new_head)
47
+ register_activity({ ticket_id: ticket_id, ticket_state: 'In progress' })
48
+ end
49
+ end
50
+
51
+ def pre_push
52
+ _, ticket_id = current_branch_and_ticket_id
53
+ if failed_push?(ticket_id)
54
+ new_lines = File.readlines(MAIN_PATH)
55
+ new_lines.pop
56
+ File.open(MAIN_PATH, 'w') do |f|
57
+ new_lines.each do |line|
58
+ f.write line
59
+ end
60
+ end
61
+ end
62
+ register_activity({ ticket_id: ticket_id, ticket_state: 'Code review' })
63
+ end
64
+
65
+ def is_new_branch?(current_branch, previous_head, new_head)
66
+ # if the refs of the previous and new heads are the same
67
+ # and the number of checkouts equals one, a new branch has been created
68
+ num_checkouts = `git reflog --date=local | grep -o #{current_branch} | wc -l`.strip.to_i
69
+ previous_head == new_head && num_checkouts == 1
70
+ end
71
+
72
+ def current_branch_and_ticket_id
73
+ list_of_branches = `git branch`.split("\n")
74
+ current_branch = list_of_branches.find { |string| string.match(/^\* /) }.sub('* ', '')
75
+ ticket_id = current_branch.match(/^([^_]*)_?/)
76
+ ticket_id = ticket_id.captures.first if ticket_id != nil
77
+ [current_branch, ticket_id]
78
+ end
79
+
80
+ def failed_push?(ticket_id)
81
+ last_entry = `tail -n 1 #{MAIN_PATH}`
82
+ return false if last_entry == LOG_TITLE
83
+ last_entry = last_entry.match(/^(\w*) \| (\w* \w*) \| (.*)/).captures
84
+ time_diff = (Time.now - Time.parse(last_entry[2])) / 60
85
+ last_entry[0] == ticket_id && last_entry[1] == 'Code review' && time_diff < 11
86
+ end
87
+
88
+ def register_activity(ticket_info)
89
+ ticket_id = ticket_info[:ticket_id]
90
+ ticket_state = ticket_info[:ticket_state]
91
+ File.open(MAIN_PATH, 'a') do |f|
92
+ f.puts "#{ticket_id} | #{ticket_state} | #{Time.now}"
93
+ end
94
+ end
95
+ end
96
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mauro Mottini
8
+ - Guillermo Aguirre
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.15'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.15'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ description: Simple time and activity tracker for git
57
+ email:
58
+ - hello@eagerworks.com
59
+ executables:
60
+ - git_timer
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - exe/git_timer
72
+ - git_timer.gemspec
73
+ - lib/git_timer.rb
74
+ - lib/git_timer/cli.rb
75
+ - lib/git_timer/version.rb
76
+ homepage: https://github.com/eagerworks/git_timer
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple time and activity tracker for git
100
+ test_files: []