method_log 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +0 -2
- data/bin/method_log +11 -3
- data/lib/method_log/api.rb +8 -6
- data/lib/method_log/method_diff.rb +3 -3
- data/lib/method_log/method_finder.rb +2 -1
- data/lib/method_log/repository.rb +11 -7
- data/lib/method_log/scope.rb +12 -0
- data/lib/method_log/version.rb +1 -1
- data/method_log.gemspec +1 -0
- data/spec/api_spec.rb +2 -2
- data/spec/method_diff_spec.rb +5 -5
- data/spec/method_finder_spec.rb +23 -0
- data/spec/scope_spec.rb +17 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e31f34e363bbbac21719ef061d0fa0351a3fb5ea
|
4
|
+
data.tar.gz: 89fdf48679161e222f0ca463e6f4421479f6ba9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4d4a549bc0d49b7ebce947cb1c561a43d4d187e4f19a6065cb214b6fd7dde8199f9db4f627808c254c9fcd4cae8287495b325efb9865b9adf44f79d8527c70c
|
7
|
+
data.tar.gz: 6391d7f9a0670ebc319c4f5a314613352b14a6110c813623247ce407c12ec66a2a6be0f94f11709e5f01721fd9d485614097638cdabfed535eb8386e49492dd5
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -21,14 +21,12 @@ This is a work-in-progress and nowhere near production-ready.
|
|
21
21
|
### Todo
|
22
22
|
|
23
23
|
* Support earlier versions of Ruby (it ought to be possible to support down to v1.9.3 fairly easily)
|
24
|
-
* Support absolute namespaces e.g. `class ::Foo::Bar`
|
25
24
|
* Support for Rspec tests
|
26
25
|
* Default to looking for commits in current git branch
|
27
26
|
* Maybe add as new git command or extension to existing command e.g. `git log`
|
28
27
|
* Optimise search for method definitions:
|
29
28
|
* Only consider commits where file that last contained method has changed
|
30
29
|
* First look in file where method was last defined
|
31
|
-
* Simple text search for files containing method name to narrow files that need to be parsed
|
32
30
|
* Find "similar" method implementations e.g. by comparing ASTs of implementations
|
33
31
|
|
34
32
|
### Credits
|
data/bin/method_log
CHANGED
@@ -4,16 +4,24 @@ require 'bundler/setup'
|
|
4
4
|
require 'method_log'
|
5
5
|
require 'method_log/repository'
|
6
6
|
require 'method_log/api'
|
7
|
+
require 'trollop'
|
8
|
+
|
9
|
+
options = Trollop::options do
|
10
|
+
opt :patch, 'Generate patch.', short: 'p'
|
11
|
+
opt :max_count, 'Limit the number of commits to output.', type: :integer, short: 'n'
|
12
|
+
end
|
7
13
|
|
8
14
|
repository = MethodLog::Repository.new(path: Dir.pwd)
|
9
15
|
api = MethodLog::API.new(repository: repository)
|
10
|
-
api.diffs(ARGV[0]).each do |method_commit, method_diff|
|
16
|
+
api.diffs(ARGV[0], max_count: options[:max_count]).each do |method_commit, method_diff|
|
11
17
|
puts "commit #{method_commit.sha}"
|
12
18
|
puts "Author: #{method_commit.author[:name]} <#{method_commit.author[:email]}>"
|
13
19
|
puts "Date: #{method_commit.author[:time].strftime('%a %b %-e %T %Y %z')}"
|
14
20
|
puts
|
15
21
|
puts method_commit.message
|
16
22
|
puts
|
17
|
-
|
18
|
-
|
23
|
+
if options[:patch]
|
24
|
+
puts method_diff.to_s(:color)
|
25
|
+
puts
|
26
|
+
end
|
19
27
|
end
|
data/lib/method_log/api.rb
CHANGED
@@ -8,10 +8,12 @@ module MethodLog
|
|
8
8
|
@repository = repository
|
9
9
|
end
|
10
10
|
|
11
|
-
def history(method_identifier)
|
11
|
+
def history(method_identifier, max_count: nil)
|
12
|
+
method_name = method_identifier.split(Regexp.union('#', '.')).last
|
12
13
|
Enumerator.new do |yielder|
|
13
|
-
@repository.commits.each do |commit|
|
14
|
-
|
14
|
+
@repository.commits(max_count: max_count).each do |commit|
|
15
|
+
source_files = commit.source_files.select { |sf| sf.source[Regexp.new(method_name)] }
|
16
|
+
method_definitions = source_files.inject([]) do |definitions, source_file|
|
15
17
|
method_finder = MethodFinder.new(source_file: source_file)
|
16
18
|
definitions += Array(method_finder.find(method_identifier))
|
17
19
|
end
|
@@ -20,10 +22,10 @@ module MethodLog
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
|
-
def diffs(method_identifier)
|
25
|
+
def diffs(method_identifier, max_count: nil)
|
24
26
|
Enumerator.new do |yielder|
|
25
|
-
history(method_identifier).each_cons(2) do |(commit, parent)|
|
26
|
-
diff = MethodDiff.new(
|
27
|
+
history(method_identifier, max_count: max_count).each_cons(2) do |(commit, parent)|
|
28
|
+
diff = MethodDiff.new(first_commit: parent, second_commit: commit)
|
27
29
|
unless diff.empty?
|
28
30
|
yielder << [commit, diff]
|
29
31
|
end
|
@@ -2,12 +2,12 @@ require 'diffy'
|
|
2
2
|
|
3
3
|
module MethodLog
|
4
4
|
class MethodDiff
|
5
|
-
def initialize(
|
6
|
-
@
|
5
|
+
def initialize(first_commit: nil, second_commit: nil)
|
6
|
+
@first_commit, @second_commit = first_commit, second_commit
|
7
7
|
end
|
8
8
|
|
9
9
|
def to_s(mode = :text)
|
10
|
-
Diffy::Diff.new(@
|
10
|
+
Diffy::Diff.new(@first_commit.method_source, @second_commit.method_source).to_s(mode)
|
11
11
|
end
|
12
12
|
|
13
13
|
def empty?
|
@@ -60,7 +60,8 @@ module MethodLog
|
|
60
60
|
|
61
61
|
def process_const(node, namespaces = [])
|
62
62
|
scope_node, name = *node
|
63
|
-
|
63
|
+
namespace = (node.type == :cbase) ? :root : name
|
64
|
+
namespaces.unshift(namespace)
|
64
65
|
if scope_node
|
65
66
|
process_const(scope_node, namespaces)
|
66
67
|
end
|
@@ -4,16 +4,9 @@ require 'method_log/commit'
|
|
4
4
|
|
5
5
|
module MethodLog
|
6
6
|
class Repository
|
7
|
-
attr_reader :commits
|
8
|
-
|
9
7
|
def initialize(path: nil)
|
10
8
|
@repository = Rugged::Repository.new(path)
|
11
9
|
@commits = []
|
12
|
-
if @repository.ref('refs/heads/master')
|
13
|
-
@repository.walk(@repository.last_commit) do |commit|
|
14
|
-
@commits << build_commit(sha: commit.oid)
|
15
|
-
end
|
16
|
-
end
|
17
10
|
end
|
18
11
|
|
19
12
|
def build_commit(sha: nil)
|
@@ -24,5 +17,16 @@ module MethodLog
|
|
24
17
|
commit.apply
|
25
18
|
@commits << commit
|
26
19
|
end
|
20
|
+
|
21
|
+
def commits(max_count: nil)
|
22
|
+
Enumerator.new do |yielder|
|
23
|
+
if @repository.ref('refs/heads/master')
|
24
|
+
@repository.walk(@repository.last_commit).with_index do |commit, index|
|
25
|
+
break if max_count && index >= max_count - 1
|
26
|
+
yielder << build_commit(sha: commit.oid)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
27
31
|
end
|
28
32
|
end
|
data/lib/method_log/scope.rb
CHANGED
@@ -18,6 +18,10 @@ module MethodLog
|
|
18
18
|
def names
|
19
19
|
[]
|
20
20
|
end
|
21
|
+
|
22
|
+
def root
|
23
|
+
self
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
attr_reader :parent
|
@@ -31,6 +35,10 @@ module MethodLog
|
|
31
35
|
|
32
36
|
def for(modules)
|
33
37
|
scope = self
|
38
|
+
if modules.first == :root
|
39
|
+
scope = root
|
40
|
+
modules.unshift
|
41
|
+
end
|
34
42
|
modules.each do |mod|
|
35
43
|
scope = scope.lookup(mod) || scope.define(mod)
|
36
44
|
end
|
@@ -53,6 +61,10 @@ module MethodLog
|
|
53
61
|
[names.join('::'), separator, name].join
|
54
62
|
end
|
55
63
|
|
64
|
+
def root
|
65
|
+
parent.root
|
66
|
+
end
|
67
|
+
|
56
68
|
protected
|
57
69
|
|
58
70
|
def names
|
data/lib/method_log/version.rb
CHANGED
data/method_log.gemspec
CHANGED
data/spec/api_spec.rb
CHANGED
data/spec/method_diff_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'method_log/method_diff'
|
2
2
|
|
3
3
|
describe MethodLog::MethodDiff do
|
4
|
-
let(:
|
5
|
-
let(:
|
6
|
-
let(:diff) { MethodLog::MethodDiff.new(
|
4
|
+
let(:first_commit) { double(:first_commit) }
|
5
|
+
let(:second_commit) { double(:second_commit) }
|
6
|
+
let(:diff) { MethodLog::MethodDiff.new(first_commit: first_commit, second_commit: second_commit) }
|
7
7
|
|
8
8
|
it 'generates text diff of the method source for two commits' do
|
9
|
-
|
10
|
-
|
9
|
+
first_commit.stub(:method_source).and_return(%{line 1\nline 2\n})
|
10
|
+
second_commit.stub(:method_source).and_return(%{line 2\nline 3\n})
|
11
11
|
expect(diff.to_s).to eq(%{-line 1\n line 2\n+line 3\n})
|
12
12
|
end
|
13
13
|
end
|
data/spec/method_finder_spec.rb
CHANGED
@@ -225,4 +225,27 @@ end
|
|
225
225
|
|
226
226
|
expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 4..6))
|
227
227
|
end
|
228
|
+
|
229
|
+
it 'finds definition of class method on ambiguous module referenced via top-level module' do
|
230
|
+
foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
|
231
|
+
module Foo
|
232
|
+
class Bar; end
|
233
|
+
end
|
234
|
+
|
235
|
+
module Baz
|
236
|
+
module Foo
|
237
|
+
class Bar; end
|
238
|
+
end
|
239
|
+
|
240
|
+
def (::Foo::Bar).foo
|
241
|
+
# implementation
|
242
|
+
end
|
243
|
+
end
|
244
|
+
}.strip)
|
245
|
+
|
246
|
+
method_finder = MethodLog::MethodFinder.new(source_file: foo)
|
247
|
+
method_definition = method_finder.find('Foo::Bar.foo')
|
248
|
+
|
249
|
+
expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 9..11))
|
250
|
+
end
|
228
251
|
end
|
data/spec/scope_spec.rb
CHANGED
@@ -75,5 +75,22 @@ describe MethodLog::Scope do
|
|
75
75
|
expect(b).not_to be_nil
|
76
76
|
expect(c).not_to be_nil
|
77
77
|
end
|
78
|
+
|
79
|
+
it 'returns root scope' do
|
80
|
+
a = root.define(:A)
|
81
|
+
b = a.define(:B)
|
82
|
+
c = b.define(:C)
|
83
|
+
expect(c.root).to eq(root)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'looks up ambiguous module via top-level module' do
|
87
|
+
a = root.define(:A)
|
88
|
+
b = a.define(:B)
|
89
|
+
c = root.define(:C)
|
90
|
+
aa = c.define(:A)
|
91
|
+
bb = aa.define(:B)
|
92
|
+
|
93
|
+
expect(c.for([:root, :A, :B])).to eq(b)
|
94
|
+
end
|
78
95
|
end
|
79
96
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Mead
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rugged
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: trollop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|