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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8416c62e1edaaecff55eca789a8480abee33ed7e
4
- data.tar.gz: e1a5011e4329bf97b0115bdaf95cf56107042b00
3
+ metadata.gz: e31f34e363bbbac21719ef061d0fa0351a3fb5ea
4
+ data.tar.gz: 89fdf48679161e222f0ca463e6f4421479f6ba9f
5
5
  SHA512:
6
- metadata.gz: d221a1bf23774b980ef1b54869e1beca1ec9eac6a98e5fa628235c63e122658ec5f0667beb2ab3536945fd52b0e0d97dd3c59e5288d2f2c0b777d38e3b310a60
7
- data.tar.gz: 787efa546b545eb8376f515d58b98d4161a53dc57a22d17157bae60b481d74ee05c44b4014a5f1499c6685a6defa0c9855067b5f2955467abf930ebfbbedf3bf
6
+ metadata.gz: c4d4a549bc0d49b7ebce947cb1c561a43d4d187e4f19a6065cb214b6fd7dde8199f9db4f627808c254c9fcd4cae8287495b325efb9865b9adf44f79d8527c70c
7
+ data.tar.gz: 6391d7f9a0670ebc319c4f5a314613352b14a6110c813623247ce407c12ec66a2a6be0f94f11709e5f01721fd9d485614097638cdabfed535eb8386e49492dd5
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  Gemfile.lock
2
+ pkg
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
- puts method_diff.to_s(:color)
18
- puts
23
+ if options[:patch]
24
+ puts method_diff.to_s(:color)
25
+ puts
26
+ end
19
27
  end
@@ -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
- method_definitions = commit.source_files.inject([]) do |definitions, source_file|
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(commit: commit, parent: parent)
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(commit: nil, parent: nil)
6
- @commit, @parent = commit, parent
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(@commit.method_source, @parent.method_source).to_s(mode)
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
- namespaces.unshift(name)
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module MethodLog
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
data/method_log.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'rugged'
22
22
  s.add_dependency 'parser'
23
23
  s.add_dependency 'diffy'
24
+ s.add_dependency 'trollop'
24
25
 
25
26
  s.add_development_dependency 'rake'
26
27
  s.add_development_dependency 'rspec'
data/spec/api_spec.rb CHANGED
@@ -62,8 +62,8 @@ end
62
62
  method_commit, method_diff = api.diffs('Foo#bar').first
63
63
  expect(method_diff.to_s.strip).to eq(%{
64
64
  def bar
65
- - # implementation 2
66
- + # implementation 1
65
+ - # implementation 1
66
+ + # implementation 2
67
67
  end
68
68
  }.strip)
69
69
  end
@@ -1,13 +1,13 @@
1
1
  require 'method_log/method_diff'
2
2
 
3
3
  describe MethodLog::MethodDiff do
4
- let(:commit) { double(:commit) }
5
- let(:parent) { double(:parent) }
6
- let(:diff) { MethodLog::MethodDiff.new(commit: commit, parent: parent) }
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
- commit.stub(:method_source).and_return(%{line 1\nline 2\n})
10
- parent.stub(:method_source).and_return(%{line 2\nline 3\n})
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
@@ -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.3
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-21 00:00:00.000000000 Z
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