Skip to content

Commit

Permalink
scan: always process every requested files in a single commit together
Browse files Browse the repository at this point in the history
  • Loading branch information
cfillion committed Nov 8, 2016
1 parent 46482c5 commit 5193147
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
18 changes: 12 additions & 6 deletions lib/reapack/index/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,23 @@ def do_scan
else
@index.auto_bump_commit = false

# call process_commit only once per commit instead of once per --scan argument
commits = Hash.new
@opts[:scan].map {|hash|
files = @git.last_commits_for(@git.relative_path hash)
files = @git.last_commits_for @git.relative_path(hash)
if !files.empty?
files.map {|f, c| [c, f] }.compact
files.each {|commit, files|
commits[commit] = Array.new unless commits.has_key? commit
commits[commit].concat files unless commits[commit].nil?
}
elsif c = @git.get_commit(hash)
c
commits[c] = nil
else
$stderr.puts "--scan: bad file or revision: '%s'" % hash
throw :stop, false
end
}.compact.flatten 1
}
commits.to_a
end

unless commits.empty?
Expand All @@ -98,7 +104,7 @@ def do_scan
end
end

def process_commit(commit, file = nil)
def process_commit(commit, files = nil)
if @opts[:verbose]
log 'processing %s: %s' % [commit.short_id, commit.summary]
end
Expand All @@ -109,7 +115,7 @@ def process_commit(commit, file = nil)

commit.each_diff
.select {|diff|
(file.nil? || diff.file == file) &&
(files.nil? || files.include?(diff.file)) &&
(not ignored? expand_path(diff.file)) &&
ReaPack::Index.is_package?(diff.file)
}
Expand Down
18 changes: 15 additions & 3 deletions lib/reapack/index/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ def last_commit_for(file)

def last_commits_for(pattern)
dir = pattern.empty? ? '' : pattern + '/'
Hash[last_commit.filelist.map {|file|
out = Hash.new
last_commit.filelist.each {|file|
path = File.split(file).first + '/'
[file, last_commit_for(file)] if path.start_with?(dir) || file == pattern
}.compact]
next unless path.start_with?(dir) || file == pattern

commit = last_commit_for(file)
out[commit] ||= Array.new
out[commit] << file
}
out
end

def guess_url_template
Expand Down Expand Up @@ -172,6 +178,12 @@ def ==(o)
o && id == o.id
end

alias :eql? :==

def hash
id.hash
end

def inspect
"#<#{self.class} #{id} #{summary}>"
end
Expand Down
48 changes: 37 additions & 11 deletions test/cli/test_scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,24 @@ def test_scan_file
@git.create_commit 'second commit', [
mkfile('cat/test1.lua', '@version 2'),
mkfile('cat/test2.lua', '@version 2.2'),
mkfile('cat/test3.lua', '@version 3'),
]

@git.create_commit 'third commit',
[mkfile('cat/test3.lua', '@version 3')]
[mkfile('cat/test4.lua', '@version 4')]
}

wrapper ['--scan', 'cat/test1.lua'], setup: setup do
capture_io { @cli.run }
wrapper ['--progress', '--scan', 'cat/test1.lua', '--scan', 'cat/test2.lua'],
setup: setup do
stdin, stderr = capture_io { @cli.run }

contents = read_index
refute_match 'version name="1"', contents, 'The initial commit was scanned'
assert_match 'version name="2"', contents
refute_match 'test2.lua', contents, 'test2.lua was indexed'
assert_match 'test2.lua', contents, 'test2.lua was indexed'
refute_match 'test3.lua', contents, 'The third commit was scanned'

assert_match /Indexing commit \d+ of 1/, stderr # not 3 commits!
end
end

Expand All @@ -354,20 +358,22 @@ def test_scan_directory
[mkfile('dir1/test1.lua', '@version 1')]

@git.create_commit 'second commit', [
mkfile('dir1/test1.lua', '@version 2'),
mkfile('dir2/test2.lua', '@version 2.2'),
mkfile('dir1/test2.lua', '@version 2'),
mkfile('dir1/sub/test3.lua', '@version 3'),
mkfile('dir2/test4.lua', '@version 4'),
]
}

wrapper ['--scan', 'dir1'], setup: setup do
capture_io { @cli.run }
wrapper ['--progress', '--scan', 'dir1'], setup: setup do
stdout, stderr = capture_io { @cli.run }

contents = read_index
refute_match 'version name="1"', contents, 'The initial commit was scanned'
assert_match 'version name="2"', contents
assert_match 'test1.lua', contents
assert_match 'test2.lua', contents
assert_match 'test3.lua', contents
refute_match 'test2.lua', contents, 'test2.lua was indexed'
refute_match 'test4.lua', contents, 'test4.lua was indexed'

assert_match /Indexing commit \d+ of 2/, stderr # not 3 commits!
end
end

Expand Down Expand Up @@ -421,6 +427,26 @@ def test_scan_directory_root
end
end

def test_scan_stick_to_full_commit
options = ['--scan', nil, '--scan', 'cat/test1.lua']

setup = proc {
Dir.chdir @git.path

@git.create_commit 'initial commit',
[mkfile('cat/test1.lua', '@version 1'),
mkfile('cat/test2.lua', '@version 2')]

options[1] = @git.last_commit.id
}

wrapper options, setup: setup do
capture_io { @cli.run }

assert_match 'test2.lua', read_index
end
end

def test_reset
options = ['--scan']

Expand Down
13 changes: 7 additions & 6 deletions test/test_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ def test_last_commits_for_directory
c5 = @git.create_commit 'fifth commit',
[mkfile('helloworld/a'), mkfile('hello-world')]

assert_equal({'hello/world' => c2, 'hello/sub/world' => c4},
assert_equal({c2 => ['hello/world'], c4 => ['hello/sub/world']},
@git.last_commits_for('hello'))
assert_equal({'chunky/bacon' => c3}, @git.last_commits_for('chunky'))
assert_equal({'hello/sub/world' => c4}, @git.last_commits_for('hello/sub'))
assert_equal({c3 => ['chunky/bacon']}, @git.last_commits_for('chunky'))
assert_equal({c4 => ['hello/sub/world']}, @git.last_commits_for('hello/sub'))
assert_empty @git.last_commits_for('foobar')

assert_equal({'hello/world' => c2}, @git.last_commits_for('hello/world'))
assert_equal({'chunky/bacon' => c3, 'hello-world' => c5,
'hello/sub/world' => c4, 'hello/world' => c2, 'helloworld/a' => c5}, @git.last_commits_for(''))
assert_equal({c2 => ['hello/world']}, @git.last_commits_for('hello/world'))
assert_equal({c3 => ['chunky/bacon'], c5 => ['hello-world', 'helloworld/a'],
c4 => ['hello/sub/world'], c2 => ['hello/world']},
@git.last_commits_for(''))
end

def test_multibyte_filename
Expand Down

0 comments on commit 5193147

Please sign in to comment.