githat 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+ group :development do
3
+ gem "rspec"
4
+ gem "bundler"
5
+ gem "jeweler", "~> 1.8.3"
6
+ gem "pygments.rb"
7
+ end
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ posix-spawn (0.3.6)
13
+ pygments.rb (0.3.1)
14
+ posix-spawn (~> 0.3.6)
15
+ yajl-ruby (~> 1.1.0)
16
+ rake (0.9.2.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ rspec (2.11.0)
20
+ rspec-core (~> 2.11.0)
21
+ rspec-expectations (~> 2.11.0)
22
+ rspec-mocks (~> 2.11.0)
23
+ rspec-core (2.11.1)
24
+ rspec-expectations (2.11.3)
25
+ diff-lcs (~> 1.1.3)
26
+ rspec-mocks (2.11.2)
27
+ yajl-ruby (1.1.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler
34
+ jeweler (~> 1.8.3)
35
+ pygments.rb
36
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bernardo B. Marques
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,27 @@
1
+ githat
2
+ ======
3
+
4
+ Git diff with code syntax highlight
5
+
6
+ Example with ruby code:
7
+
8
+ ![githat](https://raw.github.com/bernardofire/githat/master/githat.png)
9
+
10
+
11
+ Contributing to githat
12
+ ----------------------
13
+
14
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
15
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
16
+ * Fork the project.
17
+ * Start a feature/bugfix branch.
18
+ * Commit and push until you are happy with your contribution.
19
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
20
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
21
+
22
+ Copyright
23
+ ---------
24
+
25
+ Copyright (c) 2012 Bernardo B. Marques. See LICENSE.txt for
26
+ further details.
27
+
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "githat"
18
+ gem.homepage = "http://github.com/bernardofire/githat"
19
+ gem.license = "MIT"
20
+ gem.description = %Q{git diff with code syntax highlight}
21
+ gem.email = "bernardo.fire@gmail.com"
22
+ gem.authors = ["Bernardo B. Marques"]
23
+ gem.executables = ["git-hat"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "githat #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.6
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
4
+ require "githat"
5
+
6
+ include Diff
7
+
8
+ puts main(ARGV)
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "githat"
8
+ s.version = "0.0.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bernardo B. Marques"]
12
+ s.date = "2012-10-24"
13
+ s.description = "git diff with code syntax highlight"
14
+ s.email = "bernardo.fire@gmail.com"
15
+ s.executables = ["git-hat"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.mkd"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.mkd",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/git-hat",
30
+ "githat.gemspec",
31
+ "githat.png",
32
+ "lib/githat.rb",
33
+ "spec/githat_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = "http://github.com/bernardofire/githat"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "1.8.24"
40
+ s.summary = nil
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec>, [">= 0"])
47
+ s.add_development_dependency(%q<bundler>, [">= 0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
49
+ s.add_development_dependency(%q<pygments.rb>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
54
+ s.add_dependency(%q<pygments.rb>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
60
+ s.add_dependency(%q<pygments.rb>, [">= 0"])
61
+ end
62
+ end
63
+
Binary file
@@ -0,0 +1,97 @@
1
+ require 'pygments.rb'
2
+
3
+ module Diff
4
+ def main(argv=nil)
5
+ output = ''
6
+ infos = files_infos(argv)
7
+ infos.each do |file|
8
+ output << parse_diff(file)
9
+ end
10
+ output
11
+ end
12
+
13
+ def files_infos(argv)
14
+ files = []
15
+ names = argv == [] ? files_names : argv
16
+ names.map do |name|
17
+ files << { name: name, diff: file_diff(name) }
18
+ end
19
+ files
20
+ end
21
+
22
+ def parse_diff(file_info)
23
+ extension = file_extension(file_info[:name])
24
+ splited = split_diff(file_info[:diff])
25
+ heads, codes = splited[:heads], splited[:codes]
26
+ complete_file_diff = ''
27
+
28
+ (0...heads.size).each do |i|
29
+ parsed_head = parse_with_diff(heads[i])
30
+ parsed_code = parse_with_lang(codes[i], extension)
31
+ parsed_code = parse_with_diff(parsed_code)
32
+ complete_file_diff << (parsed_head + parsed_code + "\n")
33
+ end
34
+
35
+ complete_file_diff
36
+ end
37
+
38
+ def file_extension(file_name)
39
+ extension = file_name.gsub /(.+\.)/, ''
40
+ if extension.empty? || file_name == extension
41
+ extension = files_with_no_extension[file_name]
42
+ extension ||= 'text'
43
+ end
44
+ extension.to_sym
45
+ end
46
+
47
+ def split_diff(diff)
48
+ { heads: split_heads(diff),
49
+ codes: split_codes(diff) }
50
+ end
51
+
52
+ def split_codes(diff)
53
+ codes = diff.split(/^@@ .* @@/)
54
+ codes[1..codes.size]
55
+ end
56
+
57
+ def split_heads(diff)
58
+ heads = diff.scan(/^diff --git(?:.*\n){4}@@ .* @@/)
59
+ heads << diff.scan(/^@@ .* @@/).tap(&:shift)
60
+ heads.flatten
61
+ end
62
+
63
+ def file_diff(file)
64
+ `git diff #{file}`
65
+ end
66
+
67
+ def files_names
68
+ git_status.scan(/modified: .*/).map { |n| n.gsub(/modified: */, '') }
69
+ end
70
+
71
+ def git_status
72
+ `git status`
73
+ end
74
+
75
+ def parse_with_lang(code, lang)
76
+ process(code, lang)
77
+ end
78
+
79
+ def parse_with_diff(code)
80
+ process(code, :diff)
81
+ end
82
+
83
+ def files_with_no_extension
84
+ {
85
+ 'Gemfile' => :rb,
86
+ 'Gemfile.lock' => :rb,
87
+ 'Rakefile' => :rb,
88
+ 'Makefile' => :makefile
89
+ }
90
+ # TODO: support it: .*rake == rb
91
+ end
92
+
93
+ def process(code, lexer)
94
+ Pygments.highlight code, formatter: 'terminal', lexer: lexer
95
+ end
96
+ end
97
+
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Githat" do
4
+ before(:each) { extend Diff }
5
+
6
+ describe Diff do
7
+ describe "ensure some methods are mocked" do
8
+ it "files_names" do
9
+ files_names.should == ['.gitignore', 'lib/githat.rb']
10
+ end
11
+
12
+ it "file_diff" do
13
+ file_diff('githat_spec.rb').should == _1_file_2_heads
14
+ end
15
+ end
16
+
17
+ it "file_extension" do
18
+ file_extension('Makefile').should == :makefile
19
+ file_extension('.gitignore').should == :text
20
+ file_extension('foo.rb').should == :rb
21
+ file_extension('bar.js').should == :js
22
+ file_extension('foobar').should == :text
23
+ end
24
+
25
+ it "files_infos (with argv)" do
26
+ gh_spec = files_infos(['spec/githat_spec.rb']).should == _1_file_2_heads_diff
27
+ end
28
+
29
+ it "files_names" do
30
+ files_names.should == ['.gitignore', 'lib/githat.rb']
31
+ end
32
+
33
+ it "split_heads" do
34
+ split_heads(_1_file_2_heads).should == _1_file_2_heads_splited[:heads]
35
+ end
36
+
37
+ it "split_codes" do
38
+ split_codes(_1_file_2_heads).should == _1_file_2_heads_splited[:codes]
39
+ end
40
+
41
+ it "split_diff" do
42
+ split_diff(_1_file_2_heads).should == _1_file_2_heads_splited
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,131 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'githat'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ module Diff
11
+ def file_diff(name)
12
+ _1_file_2_heads
13
+ end
14
+
15
+ def git_status
16
+ git_status_modified_files
17
+ end
18
+ end
19
+
20
+ def git_status_modified_files
21
+ "# modified: .gitignore\n" +
22
+ "# deleted: foo\n" +
23
+ "# modified: lib/githat.rb"
24
+ end
25
+
26
+ def _1_file_2_heads_diff
27
+ [{:name=>"spec/githat_spec.rb",
28
+ :diff=>
29
+ "diff --git a/spec/githat_spec.rb b/spec/githat_spec.rb\n" +
30
+ "index 0c55061..217f540 100644\n--- a/spec/githat_spec.rb\n" +
31
+ "+++ b/spec/githat_spec.rb\n@@ -1,11 +1,5 @@\nrequire " +
32
+ "File.expand_path(File.dirname(__FILE__) + '/spec_helper')\n\n" +
33
+ "-module Diff\n- def files_names\n- ['bar.rb', 'foo.rb']\n" +
34
+ "- end\n-end\n-\ndescribe \"Githat\" do\n before(:each) " +
35
+ "{ extend Diff }\n\n@@ -19,12 +13,12 @@ describe \"Githat\" do\n " +
36
+ "end\n\n it \"files_names\" do\n+ files_names.should == " +
37
+ "['bar.rb', 'foo.rb'] # mocked\n end\n\n- it \"files_info\" do\n" +
38
+ "- end\n-\n- it \"file_diff\" do\n+ it \"split_heads\" do\n+ " +
39
+ "puts _1_file_2_heads\n+ split_heads(_1_file_2_heads).should == []\n " +
40
+ "end\n end\nend"}]
41
+ end
42
+
43
+ def _1_file_2_heads_splited
44
+ { codes: ["\nrequire File.expand_path(File.dirname(__FILE__) + '/spec_helper')" +
45
+ "\n\n-module Diff\n- def files_names\n- ['bar.rb', 'foo.rb']\n" +
46
+ "- end\n-end\n-\ndescribe \"Githat\" do\n before(:each) { extend Diff }\n\n",
47
+ " describe \"Githat\" do\n end\n\n it \"files_names\" do\n+ " +
48
+ "files_names.should == ['bar.rb', 'foo.rb'] # mocked\n end\n\n- " +
49
+ "it \"files_info\" do\n- end\n-\n- it \"file_diff\" do\n+ " +
50
+ "it \"split_heads\" do\n+ puts _1_file_2_heads\n+ " +
51
+ "split_heads(_1_file_2_heads).should == []\n end\n end\nend"],
52
+
53
+ heads: ["diff --git a/spec/githat_spec.rb b/spec/githat_spec.rb\n" +
54
+ "index 0c55061..217f540 100644\n--- a/spec/githat_spec.rb\n" +
55
+ "+++ b/spec/githat_spec.rb\n@@ -1,11 +1,5 @@",
56
+ "@@ -19,12 +13,12 @@"] }
57
+ end
58
+
59
+ def _2_files
60
+ %{diff --git a/lib/githat.rb b/lib/githat.rb
61
+ index fdc7d30..c912bca 100644
62
+ --- a/lib/githat.rb
63
+ +++ b/lib/githat.rb
64
+ @@ -26,7 +26,7 @@ module Diff
65
+ (0...heads.size).each do |i|
66
+ parsed_head = parse_with_diff(prepare_head_for_output(heads[i]))
67
+ parsed_code = parse_with_lang(codes[i], extension)
68
+ - parsed_code = parse_with_diff(parsed_code)
69
+ + foo
70
+ complete_file_diff << (parsed_head + parsed_code)
71
+ end
72
+
73
+ diff --git a/spec/githat_spec.rb b/spec/githat_spec.rb
74
+ index 13edd0f..2c6b9d1 100644
75
+ --- a/spec/githat_spec.rb
76
+ +++ b/spec/githat_spec.rb
77
+ @@ -1,5 +1,5 @@
78
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
79
+ -
80
+ +foo
81
+ describe "Githat" do
82
+ before(:each) { extend Diff }}
83
+ end
84
+
85
+ def _2_heads_file
86
+ ["diff --git a/spec/githat_spec.rb b/spec/githat_spec.rb" +
87
+ "index 0c55061..217f540 100644" +
88
+ "--- a/spec/githat_spec.rb" +
89
+ "+++ b/spec/githat_spec.rb" +
90
+ "@@ -1,11 +1,5 @@",
91
+ "@@ -19,12 +13,12 @@"]
92
+ end
93
+
94
+ def _1_file_2_heads
95
+ %{diff --git a/spec/githat_spec.rb b/spec/githat_spec.rb
96
+ index 0c55061..217f540 100644
97
+ --- a/spec/githat_spec.rb
98
+ +++ b/spec/githat_spec.rb
99
+ @@ -1,11 +1,5 @@
100
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
101
+
102
+ -module Diff
103
+ - def files_names
104
+ - ['bar.rb', 'foo.rb']
105
+ - end
106
+ -end
107
+ -
108
+ describe "Githat" do
109
+ before(:each) { extend Diff }
110
+
111
+ @@ -19,12 +13,12 @@ describe "Githat" do
112
+ end
113
+
114
+ it "files_names" do
115
+ + files_names.should == ['bar.rb', 'foo.rb'] # mocked
116
+ end
117
+
118
+ - it "files_info" do
119
+ - end
120
+ -
121
+ - it "file_diff" do
122
+ + it "split_heads" do
123
+ + puts _1_file_2_heads
124
+ + split_heads(_1_file_2_heads).should == []
125
+ end
126
+ end
127
+ end}
128
+ end
129
+
130
+ RSpec.configure do |config|
131
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: githat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bernardo B. Marques
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: pygments.rb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: git diff with code syntax highlight
79
+ email: bernardo.fire@gmail.com
80
+ executables:
81
+ - git-hat
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - LICENSE.txt
85
+ - README.mkd
86
+ files:
87
+ - .document
88
+ - .rspec
89
+ - Gemfile
90
+ - Gemfile.lock
91
+ - LICENSE.txt
92
+ - README.mkd
93
+ - Rakefile
94
+ - VERSION
95
+ - bin/git-hat
96
+ - githat.gemspec
97
+ - githat.png
98
+ - lib/githat.rb
99
+ - spec/githat_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: http://github.com/bernardofire/githat
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 3780487490032562861
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: ''
129
+ test_files: []