method_log 0.0.4 → 0.0.5

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: e31f34e363bbbac21719ef061d0fa0351a3fb5ea
4
- data.tar.gz: 89fdf48679161e222f0ca463e6f4421479f6ba9f
3
+ metadata.gz: 41e9aab264b13fe45d2a9647bf235cea14ea5378
4
+ data.tar.gz: 15fd978c654b949dc848f1c91057d6a104f008b8
5
5
  SHA512:
6
- metadata.gz: c4d4a549bc0d49b7ebce947cb1c561a43d4d187e4f19a6065cb214b6fd7dde8199f9db4f627808c254c9fcd4cae8287495b325efb9865b9adf44f79d8527c70c
7
- data.tar.gz: 6391d7f9a0670ebc319c4f5a314613352b14a6110c813623247ce407c12ec66a2a6be0f94f11709e5f01721fd9d485614097638cdabfed535eb8386e49492dd5
6
+ metadata.gz: 6d077e6eb40d283f0f490e0420a2650f29a6eb8382e38ab36c6ab8551c6fe88fccc6f087d6eed70851ee4749f88e5c6fdb1889c98380076a02ba568daf0d05f8
7
+ data.tar.gz: 7dd0f4d2191b7e83b9d9b483f1452c0cde6af6f0cd5ee8e30e0395d2a88fa623a1c25c48f819a2077031657fff3bdd21bb7825983e82b4fa9343d69f13c1c83f
data/README.md CHANGED
@@ -21,12 +21,14 @@ 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
+ * Investigate whether parser gem can only parse source code written using the current Ruby version
25
+ * It would be nice to have a better error message if parsing fails
24
26
  * Support for Rspec tests
25
27
  * Default to looking for commits in current git branch
26
28
  * Maybe add as new git command or extension to existing command e.g. `git log`
27
29
  * Optimise search for method definitions:
28
- * Only consider commits where file that last contained method has changed
29
30
  * First look in file where method was last defined
31
+ * By default stop when method disappears from history
30
32
  * Find "similar" method implementations e.g. by comparing ASTs of implementations
31
33
 
32
34
  ### Credits
@@ -11,13 +11,16 @@ module MethodLog
11
11
  def history(method_identifier, max_count: nil)
12
12
  method_name = method_identifier.split(Regexp.union('#', '.')).last
13
13
  Enumerator.new do |yielder|
14
+ last_source_file = nil
14
15
  @repository.commits(max_count: max_count).each do |commit|
16
+ next if last_source_file && commit.contains?(last_source_file)
15
17
  source_files = commit.source_files.select { |sf| sf.source[Regexp.new(method_name)] }
16
- method_definitions = source_files.inject([]) do |definitions, source_file|
17
- method_finder = MethodFinder.new(source_file: source_file)
18
- definitions += Array(method_finder.find(method_identifier))
18
+ method_definition = nil
19
+ source_files.map { |sf| MethodFinder.new(source_file: sf) }.each do |method_finder|
20
+ break if method_definition = method_finder.find(method_identifier)
19
21
  end
20
- yielder << MethodCommit.new(commit: commit, method_definition: method_definitions.first)
22
+ last_source_file = method_definition && method_definition.source_file
23
+ yielder << MethodCommit.new(commit: commit, method_definition: method_definition)
21
24
  end
22
25
  end
23
26
  end
@@ -24,15 +24,18 @@ module MethodLog
24
24
  end
25
25
 
26
26
  def source_files
27
- source_files = []
28
- commit.tree.walk_blobs do |root, blob_hash|
29
- name = blob_hash[:name]
30
- next unless File.extname(name) == '.rb'
31
- path = root.empty? ? name : File.join(root, name)
32
- source = @repository.lookup(blob_hash[:oid]).text
33
- source_files << SourceFile.new(path: path, source: source)
27
+ @source_files ||= Enumerator.new do |yielder|
28
+ commit.tree.walk_blobs do |root, blob_hash|
29
+ name = blob_hash[:name]
30
+ next unless File.extname(name) == '.rb'
31
+ path = root.empty? ? name : File.join(root, name)
32
+ yielder << SourceFile.new(path: path, repository: @repository, sha: blob_hash[:oid])
33
+ end
34
34
  end
35
- source_files
35
+ end
36
+
37
+ def contains?(source_file)
38
+ source_files_by_path[source_file.path] == source_file
36
39
  end
37
40
 
38
41
  def author
@@ -60,5 +63,12 @@ module MethodLog
60
63
  def commit
61
64
  @commit ||= @repository.lookup(sha)
62
65
  end
66
+
67
+ def source_files_by_path
68
+ source_files.inject({}) do |hash, source_file|
69
+ hash[source_file.path] = source_file
70
+ hash
71
+ end
72
+ end
63
73
  end
64
74
  end
@@ -1,5 +1,7 @@
1
1
  module MethodLog
2
2
  class MethodDefinition
3
+ attr_reader :source_file
4
+
3
5
  def initialize(source_file: nil, lines: nil)
4
6
  @source_file = source_file
5
7
  @lines = lines
@@ -19,7 +21,6 @@ module MethodLog
19
21
 
20
22
  protected
21
23
 
22
- attr_reader :source_file
23
24
  attr_reader :lines
24
25
  end
25
26
  end
@@ -1,11 +1,17 @@
1
1
  module MethodLog
2
2
  class SourceFile
3
3
  attr_reader :path
4
- attr_reader :source
4
+ attr_reader :sha
5
5
 
6
- def initialize(path: nil, source: nil)
6
+ def initialize(path: nil, source: nil, repository: nil, sha: nil)
7
7
  @path = path
8
8
  @source = source
9
+ @repository = repository
10
+ @sha = sha
11
+ end
12
+
13
+ def source
14
+ @source ||= @repository.lookup(@sha).text
9
15
  end
10
16
 
11
17
  def ==(other)
@@ -13,7 +19,7 @@ module MethodLog
13
19
  end
14
20
 
15
21
  def hash
16
- [path, source].hash
22
+ @sha || [path, source].hash
17
23
  end
18
24
 
19
25
  def snippet(range)
@@ -1,3 +1,3 @@
1
1
  module MethodLog
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -67,4 +67,39 @@ end
67
67
  end
68
68
  }.strip)
69
69
  end
70
+
71
+ it 'finds method which is defined in one commit, then removed in the next commit, and defined again in the next commit' do
72
+ foo_with_bar = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
73
+ class Foo
74
+ def bar; end
75
+ end
76
+ }.strip)
77
+
78
+ foo_without_bar = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
79
+ class Foo
80
+ end
81
+ }.strip)
82
+
83
+ repository = MethodLog::Repository.new(path: repository_path)
84
+
85
+ commit_1 = repository.build_commit
86
+ commit_1.add(foo_with_bar)
87
+ repository.add(commit_1)
88
+
89
+ commit_2 = repository.build_commit
90
+ commit_2.add(foo_without_bar)
91
+ repository.add(commit_2)
92
+
93
+ commit_3 = repository.build_commit
94
+ commit_3.add(foo_with_bar)
95
+ repository.add(commit_3)
96
+
97
+ repository = MethodLog::Repository.new(path: repository_path)
98
+ api = MethodLog::API.new(repository: repository)
99
+ diffs = api.diffs('Foo#bar').map(&:last).map(&:to_s)
100
+ expect(diffs).to eq([
101
+ "+ def bar; end\n",
102
+ "- def bar; end\n"
103
+ ])
104
+ end
70
105
  end
data/spec/commit_spec.rb CHANGED
@@ -39,7 +39,7 @@ describe MethodLog::Commit do
39
39
 
40
40
  repository = MethodLog::Repository.new(path: repository_path)
41
41
  commit = repository.commits.first
42
- expect(commit.source_files).to eq([source_one, source_two])
42
+ expect(commit.source_files.to_a).to eq([source_one, source_two])
43
43
  end
44
44
 
45
45
  it 'only includes source files with ruby file extension' do
@@ -52,7 +52,21 @@ describe MethodLog::Commit do
52
52
 
53
53
  repository = MethodLog::Repository.new(path: repository_path)
54
54
  commit = repository.commits.first
55
- expect(commit.source_files).to be_empty
55
+ expect(commit.source_files.to_a).to be_empty
56
+ end
57
+
58
+ it 'indicates whether it contains a given source file' do
59
+ source_file = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source-file')
60
+
61
+ repository = MethodLog::Repository.new(path: repository_path)
62
+ commit = repository.build_commit
63
+ commit.add(source_file)
64
+ commit.apply
65
+ source_file = commit.source_files.first
66
+
67
+ repository = MethodLog::Repository.new(path: repository_path)
68
+ commit = repository.commits.first
69
+ expect(commit.contains?(source_file)).to be_true
56
70
  end
57
71
 
58
72
  it 'makes author available' do
@@ -3,6 +3,10 @@ require 'spec_helper'
3
3
  require 'method_log/source_file'
4
4
 
5
5
  describe MethodLog::SourceFile do
6
+ let(:sha) { 'b54d38bbd989f4b54c38fd77767d89d1' }
7
+ let(:repository) { double(:repository) }
8
+ let(:blob) { double(:blob, text: 'source') }
9
+
6
10
  it 'is equal to another source file with same path and source' do
7
11
  file_one = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source-one')
8
12
  file_two = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source-one')
@@ -32,4 +36,10 @@ end
32
36
  end
33
37
  }.strip)
34
38
  end
39
+
40
+ it 'looks up source in repository using SHA if no source set' do
41
+ repository.stub(:lookup).with(sha).and_return(blob)
42
+ file = MethodLog::SourceFile.new(path: 'path/to/source.rb', repository: repository, sha: sha)
43
+ expect(file.source).to eq('source')
44
+ end
35
45
  end
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
4
+ version: 0.0.5
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-22 00:00:00.000000000 Z
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rugged