git_diff_map 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ed2eb190188362d25d103a92f9795bfc63f6adc
4
+ data.tar.gz: 308b6cfb4fc8b1f6a4b939c18b4b075cb9faf9a5
5
+ SHA512:
6
+ metadata.gz: ccde2d761de0939dd6db2e8e3767cb13e79ae5abb061684cecd46039a512065f461b273b4aad5d100827fb5ebe01f2c82caf44917af7ec9441f2b403d569b3d7
7
+ data.tar.gz: 2865ee95276ca6878cb2d66ffff3c69c38944dd0169779c1c19108348135074f239f60a3af8f05b74bb0c88e70513c8b7e97de325e4e33e9464695cfe0b17152
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Soutaro Matsumoto
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # GitDiffMap
2
+
3
+ Line number mapping through `git diff` output.
4
+
5
+ ## What is *mapping*?
6
+
7
+ If the line is not changed in diff, there should be pair of numbers of:
8
+
9
+ * A number to identify where the line was located in *original* file
10
+ * A number to identify where the line is located in *new* file
11
+
12
+ This library is to compute the relation.
13
+
14
+ This is an example from GitDiffParser.
15
+
16
+ ```
17
+ @@ -2,6 +2,7 @@ module Saddler
18
+ module Reporter
19
+ module Github
20
+ class CommitComment
21
+ + include Support
22
+ include Helper
23
+
24
+ # https://developer.github.com/v3/repos/comments/#create-a-commit-comment
25
+ @@ -11,7 +12,7 @@ def report(messages, options)
26
+ data = parse(messages)
27
+
28
+ # build comment
29
+ - body = build_body(data)
30
+ + body = concat_body(data)
31
+ return if body.empty?
32
+ comment = Comment.new(sha1, body, nil, nil)
33
+
34
+ @@ -25,20 +26,6 @@ def report(messages, options)
35
+ # create commit_comment
36
+ client.create_commit_comment(comment)
37
+ end
38
+ -
39
+ - def build_body(data)
40
+ - buffer = []
41
+ - files = data['checkstyle']['file'] ||= []
42
+ - files.each do |file|
43
+ - errors = file['error'] ||= []
44
+ - errors.each do |error|
45
+ - severity = error['@severity'] && error['@severity'].upcase
46
+ - message = error['@message']
47
+ - buffer << [severity, message].compact.join(': ')
48
+ - end
49
+ - end
50
+ - buffer.join("\n")
51
+ - end
52
+ end
53
+ end
54
+ end
55
+ ```
56
+
57
+ We can find some relations between lines:
58
+
59
+ * Line 4 `class CommitComment` in *new* file was at line 4 in *original* file too
60
+ * Line 5 `include Support` in *new* file is newly inserted
61
+ * Line 6 `include Helper` in *new* file was at line 5 in *original* file
62
+ * Line 14 `body = build_body(data)` in *original* file is not included in *new* file
63
+ * Line 15 `body = concat_body(data)` in *new* file is not included in *original* file
64
+
65
+ These relations can be calculated by this library as:
66
+
67
+ ```rb
68
+ diff = "..."
69
+ map = GitDiffMap.parse(diff)
70
+
71
+ map.translate_new_to_original(4) # => 4
72
+ map.translate_new_to_original(5) # => nil
73
+ map.translate_new_to_original(6) # => 6
74
+ map.translate_original_to_new(14) # => nil
75
+ map.translate_new_to_original(15) # => nil
76
+ ```
77
+
78
+ ## API
79
+
80
+ * `GitDiffMap#translate_new_to_original(line)`
81
+ * `GitDiffMap#translate_original_to_new(line)`
82
+ * `GitDiffMap#translate_new_lines_to_original_lines([lines])` `lines` must be sorted in ascending order
83
+ * `GitDiffMap#translate_original_lines_to_new_lines([lines])` `lines` must be sorted in ascending order
84
+
85
+ # Install
86
+
87
+ ```
88
+ $ gem install git_diff_map
89
+ ```
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ task default: [:test]
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_diff_map/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_diff_map"
8
+ spec.version = GitDiffMap::VERSION
9
+ spec.authors = ["Soutaro Matsumoto"]
10
+ spec.email = ["matsumoto@soutaro.com"]
11
+
12
+ spec.summary = %q{Maps line from old version to new version.}
13
+ spec.description = %q{Maps line from old version to new version.}
14
+ spec.homepage = "https://github.com/soutaro/git_diff_map"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.licenses = ['MIT']
21
+
22
+ spec.add_runtime_dependency 'git_diff_parser', '~> 2.3'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.9"
27
+ end
@@ -0,0 +1,112 @@
1
+ require 'git_diff_parser'
2
+ require 'git_diff_map/line'
3
+ require 'git_diff_map/patch'
4
+
5
+ class GitDiffMap
6
+ attr_reader :patch
7
+
8
+ def initialize(patch:)
9
+ @patch = patch
10
+ end
11
+
12
+ def self.parse(diff)
13
+ patch = GitDiffMap::Patch.new(GitDiffParser::Patch.new(diff))
14
+ self.new(patch: patch)
15
+ end
16
+
17
+ def translate_new_to_original(new_line)
18
+ translate_new_lines_to_original_lines([new_line]).first
19
+ end
20
+
21
+ def translate_new_lines_to_original_lines(lines)
22
+ results = []
23
+
24
+ all_changes = patch.changed_lines.dup
25
+ offset = 0
26
+
27
+ lines.each do |new_line|
28
+ while true
29
+ if all_changes.empty?
30
+ results << new_line + offset
31
+ break
32
+ else
33
+ change = all_changes.first
34
+
35
+ case change.type
36
+ when '-'
37
+ if new_line + offset < change.number
38
+ results << new_line + offset
39
+ break
40
+ end
41
+
42
+ offset += 1
43
+ when '+'
44
+ if new_line < change.number
45
+ results << new_line + offset
46
+ break
47
+ end
48
+
49
+ if new_line == change.number
50
+ results << nil
51
+ break
52
+ end
53
+
54
+ offset -= 1
55
+ end
56
+
57
+ all_changes.shift
58
+ end
59
+ end
60
+ end
61
+
62
+ results
63
+ end
64
+
65
+ def translate_original_to_new(original_line)
66
+ translate_original_lines_to_new_lines([original_line]).first
67
+ end
68
+
69
+ def translate_original_lines_to_new_lines(lines)
70
+ results = []
71
+
72
+ all_changes = patch.changed_lines.dup
73
+ offset = 0
74
+
75
+ lines.each do |original_line|
76
+ while true
77
+ if all_changes.empty?
78
+ results << original_line + offset
79
+ break
80
+ else
81
+ change = all_changes.first
82
+
83
+ case change.type
84
+ when '+'
85
+ if original_line + offset < change.number
86
+ results << original_line + offset
87
+ break
88
+ end
89
+
90
+ offset += 1
91
+ when '-'
92
+ if original_line < change.number
93
+ results << original_line + offset
94
+ break
95
+ end
96
+
97
+ if original_line == change.number
98
+ results << nil
99
+ break
100
+ end
101
+
102
+ offset -= 1
103
+ end
104
+
105
+ all_changes.shift
106
+ end
107
+ end
108
+ end
109
+
110
+ results
111
+ end
112
+ end
@@ -0,0 +1,22 @@
1
+ #
2
+ # Originally from rubocop-definition_validator
3
+ #
4
+ # Encoding of `line` is changed:
5
+ # `line` in added line is the number of line in old file (not changed).
6
+ # `line` in deleted line is the number of line in new file (changed).
7
+ #
8
+ class GitDiffMap
9
+ class Line < GitDiffParser::Line
10
+ attr_reader :content
11
+
12
+ # trim `+` or `-` prefix
13
+ def body
14
+ content[1..-1]
15
+ end
16
+
17
+ # @return [String] + or -
18
+ def type
19
+ content[0]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,74 @@
1
+ #
2
+ # Originally from rubocop-definition_validator
3
+ #
4
+ # This class override `changed_lines` method.
5
+ # The original method retuns only added lines.
6
+ # However we want added and removed lines.
7
+ #
8
+
9
+ class GitDiffMap
10
+ class Patch < GitDiffParser::Patch
11
+ RANGE_INFORMATION_LINE = /^@@ -(?<original_base_line>\d+),\d+ \+(?<new_base_line>\d+),/
12
+ ADDED_LINE = -> (line) { line.start_with?('+') && line !~ /^\+\+\+/ }
13
+ REMOVED_LINE = -> (line) { line.start_with?('-') && line !~ /^\-\-\-/ }
14
+
15
+ def initialize(original_patch)
16
+ @body = original_patch.body
17
+ @file = original_patch.file
18
+ @secure_hash = original_patch.secure_hash
19
+ end
20
+
21
+ # @return [Array<Line>] changed lines
22
+ def changed_lines
23
+ original_line = 0
24
+ new_line = 0
25
+
26
+ lines.each_with_index.inject([]) do |lines, (content, patch_position)|
27
+ case content
28
+ when RANGE_INFORMATION_LINE
29
+ original_line = Regexp.last_match[:original_base_line].to_i
30
+ new_line = Regexp.last_match[:new_base_line].to_i
31
+ when ADDED_LINE
32
+ line = Line.new(
33
+ content: content,
34
+ number: new_line,
35
+ patch_position: patch_position
36
+ )
37
+ lines << line
38
+ new_line += 1
39
+ when REMOVED_LINE
40
+ line = Line.new(
41
+ content: content,
42
+ number: original_line,
43
+ patch_position: patch_position
44
+ )
45
+ lines << line
46
+ original_line += 1
47
+ when NOT_REMOVED_LINE
48
+ original_line += 1
49
+ new_line += 1
50
+ end
51
+
52
+ lines
53
+ end
54
+ end
55
+
56
+ def changed_methods
57
+ lines = changed_lines
58
+ lines
59
+ .group_by{|l| l.number}
60
+ .select{|_, v| v.size == 2}
61
+ .select{|_, v| t = v.map(&:type); t.include?('-') && t.include?('+')}
62
+ .select{|_, v| v.all?{|x| x.content =~ /def\s+\w+/}}
63
+ .map{|line, v|
64
+ sorted = v.sort_by(&:type)
65
+ begin
66
+ ChangedMethod.new(sorted.first, sorted.last, line, @file)
67
+ rescue Method::InvalidAST => ex
68
+ warn "Warning: #{ex}\n#{ex.backtrace.join("\n")}"if RuboCop::ConfigLoader.debug
69
+ end
70
+ }
71
+ .compact
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ class GitDiffMap
2
+ VERSION = "0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_diff_map
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Soutaro Matsumoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git_diff_parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.9'
69
+ description: Maps line from old version to new version.
70
+ email:
71
+ - matsumoto@soutaro.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - git_diff_map.gemspec
82
+ - lib/git_diff_map.rb
83
+ - lib/git_diff_map/line.rb
84
+ - lib/git_diff_map/patch.rb
85
+ - lib/git_diff_map/version.rb
86
+ homepage: https://github.com/soutaro/git_diff_map
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Maps line from old version to new version.
110
+ test_files: []