Commit 9720bcd8 authored by Rémy Coutable's avatar Rémy Coutable

Optimize a bit Gitlab::Git::Repository#log_by_shell and its specs

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 3eb03540
...@@ -333,21 +333,21 @@ module Gitlab ...@@ -333,21 +333,21 @@ module Gitlab
offset_in_ruby = use_follow_flag && options[:offset].present? offset_in_ruby = use_follow_flag && options[:offset].present?
limit += offset if offset_in_ruby limit += offset if offset_in_ruby
cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{path} log) cmd = %W[#{Gitlab.config.git.bin_path} --git-dir=#{path} log]
cmd += %W(-n #{limit}) cmd << "--max-count=#{limit}"
cmd += %w(--format=%H) cmd << '--format=%H'
cmd += %W(--skip=#{offset}) unless offset_in_ruby cmd << "--skip=#{offset}" unless offset_in_ruby
cmd += %w(--follow) if use_follow_flag cmd << '--follow' if use_follow_flag
cmd += %w(--no-merges) if options[:skip_merges] cmd << '--no-merges' if options[:skip_merges]
cmd += %W(--after=#{options[:after].iso8601}) if options[:after] cmd << "--after=#{options[:after].iso8601}" if options[:after]
cmd += %W(--before=#{options[:before].iso8601}) if options[:before] cmd << "--before=#{options[:before].iso8601}" if options[:before]
cmd += [sha] cmd << sha
cmd += %W(-- #{options[:path]}) if options[:path].present? cmd += %W[-- #{options[:path]}] if options[:path].present?
raw_output = IO.popen(cmd) { |io| io.read } raw_output = IO.popen(cmd) { |io| io.read }
lines = offset_in_ruby ? raw_output.lines.drop(offset) : raw_output.lines lines = offset_in_ruby ? raw_output.lines.drop(offset) : raw_output.lines
lines.map { |c| Rugged::Commit.new(rugged, c.strip) } lines.map! { |c| Rugged::Commit.new(rugged, c.strip) }
end end
def sha_from_ref(ref) def sha_from_ref(ref)
......
...@@ -529,7 +529,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -529,7 +529,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
commit_with_new_name = nil commit_with_new_name = nil
rename_commit = nil rename_commit = nil
before(:all) do before(:context) do
# Add new commits so that there's a renamed file in the commit history # Add new commits so that there's a renamed file in the commit history
repo = Gitlab::Git::Repository.new(TEST_REPO_PATH).rugged repo = Gitlab::Git::Repository.new(TEST_REPO_PATH).rugged
...@@ -538,123 +538,119 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -538,123 +538,119 @@ describe Gitlab::Git::Repository, seed_helper: true do
commit_with_new_name = new_commit_edit_new_file(repo) commit_with_new_name = new_commit_edit_new_file(repo)
end end
after(:context) do
# Erase our commits so other tests get the original repo
repo = Gitlab::Git::Repository.new(TEST_REPO_PATH).rugged
repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID)
end
context "where 'follow' == true" do context "where 'follow' == true" do
options = { ref: "master", follow: true } let(:options) { { ref: "master", follow: true } }
context "and 'path' is a directory" do context "and 'path' is a directory" do
let(:log_commits) do
repository.log(options.merge(path: "encoding"))
end
it "does not follow renames" do it "does not follow renames" do
log_commits = repository.log(options.merge(path: "encoding"))
aggregate_failures do
expect(log_commits).to include(commit_with_new_name) expect(log_commits).to include(commit_with_new_name)
expect(log_commits).to include(rename_commit) expect(log_commits).to include(rename_commit)
expect(log_commits).not_to include(commit_with_old_name) expect(log_commits).not_to include(commit_with_old_name)
end end
end end
end
context "and 'path' is a file that matches the new filename" do context "and 'path' is a file that matches the new filename" do
context 'without offset' do context 'without offset' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG"))
end
it "follows renames" do it "follows renames" do
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG"))
aggregate_failures do
expect(log_commits).to include(commit_with_new_name) expect(log_commits).to include(commit_with_new_name)
expect(log_commits).to include(rename_commit) expect(log_commits).to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to include(commit_with_old_name)
end end
end end
context 'with offset=1' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1))
end end
context 'with offset=1' do
it "follows renames and skip the latest commit" do it "follows renames and skip the latest commit" do
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1))
aggregate_failures do
expect(log_commits).not_to include(commit_with_new_name) expect(log_commits).not_to include(commit_with_new_name)
expect(log_commits).to include(rename_commit) expect(log_commits).to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to include(commit_with_old_name)
end end
end end
context 'with offset=1', 'and limit=1' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 1))
end end
context 'with offset=1', 'and limit=1' do
it "follows renames, skip the latest commit and return only one commit" do it "follows renames, skip the latest commit and return only one commit" do
expect(log_commits).not_to include(commit_with_new_name) log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 1))
expect(log_commits).to include(rename_commit)
expect(log_commits).not_to include(commit_with_old_name) expect(log_commits).to contain_exactly(rename_commit)
end end
end end
context 'with offset=1', 'and limit=2' do context 'with offset=1', 'and limit=2' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 2))
end
it "follows renames, skip the latest commit and return only two commits" do it "follows renames, skip the latest commit and return only two commits" do
expect(log_commits).not_to include(commit_with_new_name) log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 1, limit: 2))
expect(log_commits).to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) aggregate_failures do
expect(log_commits).to contain_exactly(rename_commit, commit_with_old_name)
end end
end end
context 'with offset=2' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2))
end end
context 'with offset=2' do
it "follows renames and skip the latest commit" do it "follows renames and skip the latest commit" do
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2))
aggregate_failures do
expect(log_commits).not_to include(commit_with_new_name) expect(log_commits).not_to include(commit_with_new_name)
expect(log_commits).not_to include(rename_commit) expect(log_commits).not_to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to include(commit_with_old_name)
end end
end end
context 'with offset=2', 'and limit=1' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 1))
end end
context 'with offset=2', 'and limit=1' do
it "follows renames, skip the two latest commit and return only one commit" do it "follows renames, skip the two latest commit and return only one commit" do
expect(log_commits).not_to include(commit_with_new_name) log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 1))
expect(log_commits).not_to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to contain_exactly(commit_with_old_name)
end end
end end
context 'with offset=2', 'and limit=2' do context 'with offset=2', 'and limit=2' do
let(:log_commits) do
repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 2))
end
it "follows renames, skip the two latest commit and return only one commit" do it "follows renames, skip the two latest commit and return only one commit" do
log_commits = repository.log(options.merge(path: "encoding/CHANGELOG", offset: 2, limit: 2))
aggregate_failures do
expect(log_commits).not_to include(commit_with_new_name) expect(log_commits).not_to include(commit_with_new_name)
expect(log_commits).not_to include(rename_commit) expect(log_commits).not_to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to include(commit_with_old_name)
end end
end end
end end
context "and 'path' is a file that matches the old filename" do
let(:log_commits) do
repository.log(options.merge(path: "CHANGELOG"))
end end
context "and 'path' is a file that matches the old filename" do
it "does not follow renames" do it "does not follow renames" do
log_commits = repository.log(options.merge(path: "CHANGELOG"))
aggregate_failures do
expect(log_commits).not_to include(commit_with_new_name) expect(log_commits).not_to include(commit_with_new_name)
expect(log_commits).to include(rename_commit) expect(log_commits).to include(rename_commit)
expect(log_commits).to include(commit_with_old_name) expect(log_commits).to include(commit_with_old_name)
end end
end end
end
context "unknown ref" do context "unknown ref" do
let(:log_commits) { repository.log(options.merge(ref: 'unknown')) } it "returns an empty array" do
log_commits = repository.log(options.merge(ref: 'unknown'))
it "return an empty array" do
expect(log_commits).to eq([]) expect(log_commits).to eq([])
end end
end end
...@@ -773,12 +769,6 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -773,12 +769,6 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
end end
end end
after(:all) do
# Erase our commits so other tests get the original repo
repo = Gitlab::Git::Repository.new(TEST_REPO_PATH).rugged
repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID)
end
end end
describe "#commits_between" do describe "#commits_between" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment