git-blame-color 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3048c136092c1b8b76809199b551db23db220b21
4
+ data.tar.gz: a467034168fb27686bfd7edd89fab55e9d868778
5
+ SHA512:
6
+ metadata.gz: 6895f64d48e82f40a66cc39cc024e7a8e01075fd8e0d5e2118bc8943e86ea64bbfbd2104a157312ec74414de9923e7e2a732122abe988bdf61847a0d4cff6409
7
+ data.tar.gz: 4473ab1b316f068c366d2b195c0d5c06fa068f84b67ab4232ca11e4c349dbce73f7076c1979bf5ecb7ec3bcba98e1cb22c26e18d6a21e40b990c92a2c592367f
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Michael Dippery <michael@monkey-robot.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'git-blame-color/version'
4
+
5
+ GEMSPEC = `git ls-files | grep gemspec`.chomp
6
+ GEM = "git-blame-color-#{Git::BlameColor::VERSION}.gem"
7
+
8
+ desc "Build gem"
9
+ task :build => :perms do
10
+ system "gem", "build", GEMSPEC
11
+ end
12
+
13
+ desc "Ensure correct permissions for gem"
14
+ task :perms do
15
+ system "chmod", "-R", "a+rX", *`git ls-files`.chomp.split("\n")
16
+ end
17
+
18
+ desc "Tag the latest version of gem"
19
+ task :tag do
20
+ system "git", "tag", "-s", "-m", "git-blame-color v#{Git::BlameColor::VERSION}", "v#{Git::BlameColor::VERSION}"
21
+ end
22
+
23
+ desc "Install gem"
24
+ task :install => :build do
25
+ system "gem", "install", GEM
26
+ end
27
+
28
+ desc "Push gem to RubyGems"
29
+ task :release => [:tag, :build] do
30
+ fail 'Cannot release a dev version' if Git::BlameColor::VERSION.end_with?('dev')
31
+ system "gem", "push", GEM
32
+ end
33
+
34
+ desc "Clean built products"
35
+ task :clean do
36
+ rm Dir.glob("*.gem"), :verbose => true
37
+ end
38
+
39
+ task :default => :build
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git-blame-color'
4
+
5
+ Git::BlameColor.application.run(ARGV)
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'git-blame-color/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'git-blame-color'
9
+ spec.version = Git::BlameColor::VERSION
10
+ spec.licenses = ['MIT']
11
+ spec.authors = ['Michael Dippery']
12
+ spec.email = ['michael@monkey-robot.com']
13
+
14
+ spec.summary = 'Provides colorized output for git blame'
15
+ spec.description = 'An alternative to git blame that colors output for easier reading'
16
+ spec.homepage = 'https://github.com/git-blame-color'
17
+
18
+ spec.metadata = {
19
+ 'build_date' => Time.now.strftime('%Y-%m-%d %H:%M:%S %Z'),
20
+ 'commit' => `git describe 2>/dev/null`.chomp,
21
+ 'commit_hash' => `git rev-parse HEAD`.chomp,
22
+ }
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.required_ruby_version = '>= 1.9.3'
30
+
31
+ spec.add_runtime_dependency('rainbow', '~> 2.1')
32
+
33
+ spec.add_development_dependency('aruba', '~> 0.14')
34
+ spec.add_development_dependency('cucumber', '~> 2.3')
35
+ spec.add_development_dependency('rspec', '~> 3.4')
36
+ end
@@ -0,0 +1,4 @@
1
+ require 'git-blame-color/application'
2
+ require 'git-blame-color/commit'
3
+ require 'git-blame-color/formatter'
4
+ require 'git-blame-color/version'
@@ -0,0 +1,73 @@
1
+ require 'open3'
2
+ require 'shellwords'
3
+ require 'rainbow'
4
+ require 'git-blame-color/pager'
5
+ require 'git-blame-color/ext/argv'
6
+ require 'git-blame-color/ext/array'
7
+
8
+
9
+ module Git
10
+ module BlameColor
11
+ class << self
12
+ def application
13
+ @application || Git::BlameColor::Application.new
14
+ end
15
+ end
16
+
17
+ class Application
18
+ include Pager
19
+
20
+ def run(argv)
21
+ Rainbow.enabled = true
22
+ die 1, 'git blame-color does not support the --incremental option' if ARGV.incremental?
23
+ stdout, stderr, status = Open3.capture3('git', 'blame', '--line-porcelain', *ARGV)
24
+ die status.exitstatus, stderr unless status.success?
25
+ lines = stdout.split($/)
26
+ stats = nil
27
+ if ARGV.show_stats?
28
+ stats = lines.last(3)
29
+ lines = lines.drop_last(3)
30
+ end
31
+ commits = parse_commits(lines, [])
32
+ author_width = longest_author_name_width(commits)
33
+ line_width = max_line_number_width(commits)
34
+ opts = {
35
+ :show_boundary_commits => !ARGV.blank_boundary_commits?,
36
+ :long_hashes => ARGV.long_hash?,
37
+ :raw_timestamps => ARGV.raw_timestamps?,
38
+ }
39
+ formatter = Git::BlameColor::Formatter.new(author_width, line_width, opts)
40
+ page
41
+ commits.each do |c|
42
+ puts formatter.format(c)
43
+ end
44
+ puts stats.join("\n") if stats
45
+ end
46
+
47
+ private
48
+
49
+ def die(status, msg)
50
+ STDERR.puts msg
51
+ exit status
52
+ end
53
+
54
+ def parse_commits(commits, acc)
55
+ return acc if commits.empty?
56
+ lines = commits.take_while { |line| line =~ /^[^\t]/ }
57
+ lines << commits.find { |line| line =~ /^\t/ }
58
+ commit = Git::BlameColor::Commit.new(lines)
59
+ acc << commit
60
+ commits = commits.drop(lines.count)
61
+ parse_commits(commits, acc)
62
+ end
63
+
64
+ def longest_author_name_width(commits)
65
+ commits.longest_string_length_by_key(:author)
66
+ end
67
+
68
+ def max_line_number_width(commits)
69
+ commits.longest_string_length_by_key(:line_number)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,32 @@
1
+ require 'git-blame-color/ext/string'
2
+
3
+
4
+ module Git
5
+ module BlameColor
6
+ class Commit
7
+ attr_reader :hash, :line_number, :author, :email, :timestamp, :timezone, :summary, :edited_line
8
+
9
+ def initialize(block)
10
+ @hash = block[0].split(' ')[0]
11
+ @line_number = block[0].split(' ')[2].to_i
12
+ @author = find_key(block, 'author').value
13
+ @email = find_key(block, 'author-mail').value.gsub(/[<>]/, '')
14
+ @timestamp = find_key(block, 'author-time').value.to_i
15
+ @timezone = find_key(block, 'author-tz').value
16
+ @summary = find_key(block, 'summary').value
17
+ @edited_line = block.last.gsub(/^\t/, '')
18
+ @is_boundary = !find_key(block, 'boundary').nil?
19
+ end
20
+
21
+ def boundary?
22
+ @is_boundary
23
+ end
24
+
25
+ private
26
+
27
+ def find_key(block, key)
28
+ block.find { |line| line.key == key }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ class Array
2
+ OPTION_FLAGS = %W{--incremental --show-stats -b -l -t}
3
+ OPTION_NAMES = %W{incremental show_stats blank_boundary_commits long_hash raw_timestamps}
4
+ OPTIONS = OPTION_FLAGS.zip(OPTION_NAMES)
5
+
6
+ OPTIONS.each do |o|
7
+ define_method("#{o[1]}?") do
8
+ include?(o[0])
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ class Array
2
+ def drop_last(n)
3
+ first(length - n)
4
+ end
5
+
6
+ def longest_string_by_key(key)
7
+ map(&key).longest_string
8
+ end
9
+
10
+ def longest_string
11
+ map(&:to_s).max_by(&:length)
12
+ end
13
+
14
+ def longest_string_length
15
+ longest_string.length
16
+ end
17
+
18
+ def longest_string_length_by_key(key)
19
+ longest_string_by_key(key).length
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def key
3
+ split(' ', 2)[0]
4
+ end
5
+
6
+ def value
7
+ split(' ', 2)[1]
8
+ end
9
+ end
@@ -0,0 +1,86 @@
1
+ require 'rainbow'
2
+
3
+
4
+ module Git
5
+ module BlameColor
6
+ class Formatter
7
+ attr_reader :author_name_width, :line_number_width
8
+
9
+ def initialize(author_name_width, line_number_width, options={})
10
+ @author_name_width = author_name_width
11
+ @line_number_width = line_number_width
12
+ @options = options
13
+ end
14
+
15
+ def long_hashes?
16
+ !!@options[:long_hashes]
17
+ end
18
+
19
+ def show_boundary_commits?
20
+ @options.include?(:show_boundary_commits) ? !!@options[:show_boundary_commits] : true
21
+ end
22
+
23
+ def raw_timestamps?
24
+ !!@options[:raw_timestamps]
25
+ end
26
+
27
+ def format(commit)
28
+ parts = [
29
+ color_coded_hash(commit),
30
+ Rainbow(author_to_s(commit)).blue,
31
+ Rainbow(timestamp_to_s(commit)).green,
32
+ Rainbow(line_number_to_s(commit)).black.bright,
33
+ commit.edited_line
34
+ ]
35
+ parts.join(' ')
36
+ end
37
+
38
+ private
39
+
40
+ def hash_length
41
+ long_hashes? ? 40 : 8
42
+ end
43
+
44
+ def hash_to_s(commit)
45
+ if commit.boundary?
46
+ "^#{commit.hash[0,hash_length-1]}"
47
+ else
48
+ commit.hash[0,hash_length]
49
+ end
50
+ end
51
+
52
+ def color_coded_hash(commit)
53
+ if show_boundary_commits?
54
+ Rainbow(hash_to_s(commit)).yellow
55
+ else
56
+ ' ' * 8
57
+ end
58
+ end
59
+
60
+ def author_to_s(commit)
61
+ sprintf('%-*s', author_name_width, commit.author)
62
+ end
63
+
64
+ def line_number_to_s(commit)
65
+ sprintf('%*s', line_number_width, commit.line_number.to_s)
66
+ end
67
+
68
+ def timestamp_to_s(commit)
69
+ if raw_timestamps?
70
+ commit.timestamp
71
+ else
72
+ timestamp_to_time(commit).strftime('%Y-%m-%d')
73
+ end
74
+ end
75
+
76
+ def timestamp_to_time(commit)
77
+ stamp = commit.timestamp
78
+ zone = commit.timezone
79
+ front_zone, back_zone = zone[0..-3], zone[-2,2]
80
+ zone = "#{front_zone}:#{back_zone}"
81
+ t = Time.at(stamp.to_i)
82
+ Time.new(t.year, t.month, t.day, t.hour, t.min, t.sec, zone)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,28 @@
1
+ module Pager
2
+ def page
3
+ return if RUBY_PLATFORM =~ /win32/
4
+ return unless STDOUT.tty?
5
+
6
+ read, write = IO.pipe
7
+
8
+ unless Kernel.fork # Child process
9
+ STDOUT.reopen(write)
10
+ STDERR.reopen(write) if STDERR.tty?
11
+ read.close
12
+ write.close
13
+ return
14
+ end
15
+
16
+ # Parent process, become pager
17
+ STDIN.reopen(read)
18
+ read.close
19
+ write.close
20
+
21
+ ENV['LESS'] = 'RS' # Sane less defaults
22
+
23
+ Kernel.select [STDIN] # Wait until we have input before we start the pager
24
+ pager = ENV['PAGER'] || 'less'
25
+ exec pager rescue exec "/bin/sh", "-c", pager
26
+ rescue StandardError
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module Git
2
+ module BlameColor
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-blame-color
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Dippery
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rainbow
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aruba
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cucumber
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ description: An alternative to git blame that colors output for easier reading
70
+ email:
71
+ - michael@monkey-robot.com
72
+ executables:
73
+ - git-blame-color
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - Rakefile
81
+ - exe/git-blame-color
82
+ - git-blame-color.gemspec
83
+ - lib/git-blame-color.rb
84
+ - lib/git-blame-color/application.rb
85
+ - lib/git-blame-color/commit.rb
86
+ - lib/git-blame-color/ext/argv.rb
87
+ - lib/git-blame-color/ext/array.rb
88
+ - lib/git-blame-color/ext/string.rb
89
+ - lib/git-blame-color/formatter.rb
90
+ - lib/git-blame-color/pager.rb
91
+ - lib/git-blame-color/version.rb
92
+ homepage: https://github.com/git-blame-color
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ build_date: 2016-04-15 21:19:13 PDT
97
+ commit: v0.1.0
98
+ commit_hash: 2f401ab1f8187ffc9fba3001e71d2cd50da53bd9
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 1.9.3
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Provides colorized output for git blame
119
+ test_files: []