tax_code 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tax_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eugene Kalenkovich
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # TaxCode
2
+
3
+ Each time we need to modify a file for a new feature or a bug fix we increase a
4
+ maintenance cost of this file and the whole code base.
5
+ TaxCode calculates cumulative maintenance 'taxes' introduced by all changes in the git repository.
6
+
7
+ Files with higher taxes are most probably ones that carry too many responsibilities and/or
8
+ are too complex - they should be considered for refactoring.
9
+
10
+ You can see the tax value for particular file as a 'perceptual size increase' - e.g. if the file
11
+ size is 500 lines and its tax is 400, this file maintenance costs the same as for untouched
12
+ 900 lines long one (please note that there is no science behind tax calculation, this is author's opinionated taks on the problem)
13
+
14
+ TaxCode differences from other similar scripts and gems:
15
+ - TaxCode takes into account that maintenance cost decreases over time - if you do not
16
+ touch some files for a long time, they may be stable enogh and not have as high cost as recently modified ones.
17
+ - It gives you a 'tax credit' for changes that reduce the size of your files
18
+ - It tracks files that are renamed and/or moed around
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'tax_code'
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install tax_code
33
+
34
+ ## Usage
35
+
36
+ Main intent of this gem is to be used from command line. The number of options and commands will be increased in future releases
37
+
38
+ To get repository or directory aggregated tax or a file tax:
39
+
40
+ $ cd test/repo
41
+ $ taxes
42
+ 19
43
+
44
+ $ cd ../..
45
+ $ taxes test/repo
46
+ 19
47
+
48
+ $ taxes test/repo/multiple_commits
49
+ 14
50
+
51
+ Too narrow path specification will not take into account file renames that happened outside of this path
52
+
53
+ $ taxes test/repo/renamed
54
+ 0
55
+
56
+ To get the list of top (up to) 25 expensive files in your repository (non-taxed files are not included in the report):
57
+
58
+ $ cd test/repo
59
+ $ worst_taxed
60
+ 14 test/repo/multiple_commits
61
+ 5 test/repo/renamed
62
+
63
+ $ cd ../..
64
+ $ worst_taxed test/repo
65
+ 14 test/repo/multiple_commits
66
+ 5 test/repo/renamed
67
+
68
+ Please note that limiting to particular directory or file may miss data about renamed files. E.g:
69
+
70
+ $ worst_taxed test/repo/multiple_commits
71
+ 14 test/repo/multiple_commits
72
+
73
+ $ worst_taxed test/repo/renamed
74
+ No taxed files found
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
83
+
84
+
85
+ ## TODO
86
+
87
+ * Add more options and commands to CLI
88
+ * Output partial path (instead of full path from git root)
89
+ * Do full repo log on runs limited to directory/file
90
+ * Calculate tax on a single commit (last or specified)
91
+ * Historical snapshot - taxes up to particular commit or date
92
+ * Taxes accumulated after particular commit or date
93
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
data/bin/taxes ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+ require_relative '../lib/tax_code'
3
+
4
+ target = ARGV[0] || '.'
5
+
6
+ puts TaxCode.taxes(target).map(&:last).inject(0, &:+)
data/bin/worst_taxed ADDED
@@ -0,0 +1,8 @@
1
+ #! /usr/bin/env ruby
2
+ require_relative '../lib/tax_code'
3
+
4
+ worst = TaxCode.worst(ARGV[0] || '.')
5
+
6
+ puts 'No taxed files found' and exit if worst.empty?
7
+
8
+ puts worst.map{ |f, t| "#{t}\t#{f}" }
@@ -0,0 +1,3 @@
1
+ class TaxCode
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tax_code.rb ADDED
@@ -0,0 +1,89 @@
1
+ require_relative "tax_code/version"
2
+ require 'date'
3
+
4
+ class TaxCode
5
+ class << self
6
+ def taxes(path = '.')
7
+ TaxCode.new(path).taxes
8
+ end
9
+
10
+ def worst(path = '.', num = 25)
11
+ TaxCode.new(path).worst(num)
12
+ end
13
+
14
+ def taxed_only(path = '.')
15
+ TaxCode.new(path).taxed_only
16
+ end
17
+ end
18
+
19
+ def initialize(path = '.')
20
+ dir = path
21
+ file = '.'
22
+ unless File.directory? path
23
+ dir = File.dirname path
24
+ file = File.basename path
25
+ end
26
+ Dir.chdir(dir) do
27
+ log = `git log --numstat -M #{file}`.encode('UTF-8', 'UTF-8', :invalid => :replace)
28
+ raise 'Path has to be inside your git repository' if log == ""
29
+ compute_history(log)
30
+ end
31
+ end
32
+
33
+ def taxes
34
+ @hist.inject({}) do |res, (f, changes)|
35
+ size, tax = changes[0][1], 0
36
+ changes[1..-1].each do |c|
37
+ tax += score(size, *c)
38
+ size += c[1] - c[2]
39
+ end
40
+ size > 0 ? res.merge(f => [tax.to_i, 0].max) : res
41
+ end
42
+ end
43
+
44
+ def taxed_only
45
+ taxes.select{ |_, t| t > 0 }
46
+ end
47
+
48
+ def worst(num = 25)
49
+ taxed_only.sort_by(&:last).last(num).reverse
50
+ end
51
+
52
+ private
53
+
54
+ def score(s, age, a, d)
55
+ hpr = 2 ** ( - age / 180.0)
56
+ (s/40.0 + a * 2 - d ) * hpr
57
+ end
58
+
59
+ def commits(log)
60
+ log.split(/^Date:\s+(.*)$/)[1..-1].each_slice(2).map do |(d,all)|
61
+ age = (Date.today - Date.parse(d)).to_i
62
+ all.split(/\n/).grep(/^\d/).inject({}) do |h, s|
63
+ a, d, f = s.split(/\t/)
64
+ h.merge f => [age, a.to_i, d.to_i]
65
+ end
66
+ end
67
+ end
68
+
69
+ def compute_history(log)
70
+ @hist = Hash.new{|h,k| h[k] = []}
71
+ commits(log).reverse.each do |cmt|
72
+ cmt.each do |f, ar|
73
+ fn = f
74
+ if p = moved(f)
75
+ fo, fn = p
76
+ @hist[fn] = @hist[fo]
77
+ @hist.delete(fo)
78
+ end
79
+ @hist[fn] << ar
80
+ end
81
+ end
82
+ end
83
+
84
+ def moved(f)
85
+ if f =~ /^(.*){([^\s]*) => ([^\s}]*)}(.*)$/
86
+ [$1 + $2 + $4, $1 + $3 + $4].map{|fn| fn.sub('//','/')}
87
+ end
88
+ end
89
+ end
data/tax_code.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tax_code/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'tax_code'
8
+ gem.version = TaxCode::VERSION
9
+ gem.authors = ['Eugene Kalenkovich']
10
+ gem.email = ['rubify@softover.com']
11
+ gem.description = 'TaxCode scans git commit history and calculates maintenance tax for each file'
12
+ gem.summary = 'Calculates maintenance taxes for your git repo'
13
+ gem.homepage = 'https://github.com/kalenkov/tax_code'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,2 @@
1
+ This file is supposed to be committed only once
2
+ Committed once files are supposed to have no tax
@@ -0,0 +1,8 @@
1
+ Line for commit number 1
2
+ More lines
3
+ added
4
+ ...
5
+ And
6
+ even
7
+ more
8
+ ...
@@ -0,0 +1,10 @@
1
+ To
2
+ refactor
3
+ this
4
+ file
5
+ we
6
+ changed it a little
7
+ and removed significant part of the content.
8
+ .......
9
+ We already made
10
+ this a little more expensive
data/test/repo/renamed ADDED
@@ -0,0 +1,3 @@
1
+ This file was renamed and modified
2
+ For now we will add some content
3
+ And some more
@@ -0,0 +1,17 @@
1
+ require 'minitest/autorun'
2
+ require 'tax_code'
3
+
4
+ class TaxCodeTest < MiniTest::Unit::TestCase
5
+ def test_prebaked_files
6
+ res = TaxCode.new('test/repo').taxes
7
+ assert res['test/repo/renamed'] > 0
8
+ assert res['test/repo/multiple_commits'] > res['test/repo/renamed']
9
+ assert res['test/repo/refactored'] == 0
10
+ assert res['test/repo/committed_once'] == 0
11
+ assert res['test/repo/commited_once'] == nil # File removed from repo
12
+ end
13
+
14
+ def test_taxed_only
15
+ assert TaxCode.taxed_only('test/repo').size == 2
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tax_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eugene Kalenkovich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: TaxCode scans git commit history and calculates maintenance tax for each
15
+ file
16
+ email:
17
+ - rubify@softover.com
18
+ executables:
19
+ - taxes
20
+ - worst_taxed
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - .gitignore
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/taxes
30
+ - bin/worst_taxed
31
+ - lib/tax_code.rb
32
+ - lib/tax_code/version.rb
33
+ - tax_code.gemspec
34
+ - test/repo/committed_once
35
+ - test/repo/multiple_commits
36
+ - test/repo/refactored
37
+ - test/repo/renamed
38
+ - test/tax_code_test.rb
39
+ homepage: https://github.com/kalenkov/tax_code
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.24
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Calculates maintenance taxes for your git repo
63
+ test_files:
64
+ - test/repo/committed_once
65
+ - test/repo/multiple_commits
66
+ - test/repo/refactored
67
+ - test/repo/renamed
68
+ - test/tax_code_test.rb