Merge two methods.
This commit is contained in:
parent
e78f1048d9
commit
1a59766d0c
3 changed files with 10 additions and 13 deletions
|
@ -654,20 +654,17 @@ class Repository
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_commit_for_path(sha, path)
|
def last_commit_for_path(sha, path)
|
||||||
sha = cache_last_commit_id_for_path(sha, path)
|
sha = last_commit_id_for_path(sha, path)
|
||||||
commit(sha)
|
commit(sha)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_commit_id_for_path(sha, path)
|
def last_commit_id_for_path(sha, path)
|
||||||
|
key = path.blank? ? "last_commit_id_for_path:#{sha}" : "last_commit_id_for_path:#{sha}:#{Digest::SHA1.hexdigest(path)}"
|
||||||
|
|
||||||
|
cache.fetch(key) do
|
||||||
args = %W(#{Gitlab.config.git.bin_path} rev-list --max-count=1 #{sha} -- #{path})
|
args = %W(#{Gitlab.config.git.bin_path} rev-list --max-count=1 #{sha} -- #{path})
|
||||||
Gitlab::Popen.popen(args, path_to_repo).first.strip
|
Gitlab::Popen.popen(args, path_to_repo).first.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_last_commit_id_for_path(sha, path)
|
|
||||||
key = path.blank? ? "last_commit_id_for_path:#{sha}" : "last_commit_id_for_path:#{sha}:#{Digest::SHA1.hexdigest(path)}"
|
|
||||||
cache.fetch(key) do
|
|
||||||
last_commit_id_for_path(sha, path)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_branch(name, opts = {})
|
def next_branch(name, opts = {})
|
||||||
|
|
|
@ -70,7 +70,7 @@ module API
|
||||||
ref: params[:ref],
|
ref: params[:ref],
|
||||||
blob_id: blob.id,
|
blob_id: blob.id,
|
||||||
commit_id: commit.id,
|
commit_id: commit.id,
|
||||||
last_commit_id: repo.cache_last_commit_id_for_path(commit.sha, params[:file_path])
|
last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path])
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -142,16 +142,16 @@ describe Repository, models: true do
|
||||||
describe '#last_commit_id_for_path' do
|
describe '#last_commit_id_for_path' do
|
||||||
subject { repository.last_commit_id_for_path(sample_commit.id, '.gitignore') }
|
subject { repository.last_commit_id_for_path(sample_commit.id, '.gitignore') }
|
||||||
|
|
||||||
it { is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8') }
|
it "returns last commit id for a given path" do
|
||||||
|
is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8')
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#cache_last_commit_id_for_path' do
|
it "caches last commit id for a given path" do
|
||||||
it "caches #last_commit_id_for_path" do
|
|
||||||
cache = repository.send(:cache)
|
cache = repository.send(:cache)
|
||||||
key = "last_commit_id_for_path:#{sample_commit.id}:#{Digest::SHA1.hexdigest('.gitignore')}"
|
key = "last_commit_id_for_path:#{sample_commit.id}:#{Digest::SHA1.hexdigest('.gitignore')}"
|
||||||
|
|
||||||
expect(cache).to receive(:fetch).with(key).and_return('c1acaa5')
|
expect(cache).to receive(:fetch).with(key).and_return('c1acaa5')
|
||||||
expect(repository.cache_last_commit_id_for_path(sample_commit.id, '.gitignore')).to eq('c1acaa5')
|
is_expected.to eq('c1acaa5')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue