diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-12-22 13:22:07 +0000 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-12-22 11:25:26 -0200 |
commit | 1c82d9984501743ee3ca86af62f1c2007ae28c95 (patch) | |
tree | 4e67385dbb952a8c37ef9f00e3d41b5359fcc4bb | |
parent | 65cbcfa6ef6bf506dce0369d3efc37873e07c10e (diff) | |
download | gitlab-ce-1c82d9984501743ee3ca86af62f1c2007ae28c95.tar.gz |
Merge branch 'cache-last-commit-sha-for-path' into 'master'
Cache last commit id for path
See merge request !8098
-rw-r--r-- | app/models/repository.rb | 12 | ||||
-rw-r--r-- | changelogs/unreleased/cache-last-commit-sha-for-path.yml | 4 | ||||
-rw-r--r-- | lib/api/files.rb | 2 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 16 |
4 files changed, 31 insertions, 3 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 1ccabdb7c1f..3266e9c75f0 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -654,11 +654,19 @@ class Repository end def last_commit_for_path(sha, path) - args = %W(#{Gitlab.config.git.bin_path} rev-list --max-count=1 #{sha} -- #{path}) - sha = Gitlab::Popen.popen(args, path_to_repo).first.strip + sha = last_commit_id_for_path(sha, path) commit(sha) end + 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}) + Gitlab::Popen.popen(args, path_to_repo).first.strip + end + end + def next_branch(name, opts = {}) branch_ids = self.branch_names.map do |n| next 1 if n == name diff --git a/changelogs/unreleased/cache-last-commit-sha-for-path.yml b/changelogs/unreleased/cache-last-commit-sha-for-path.yml new file mode 100644 index 00000000000..9cd8c5bab86 --- /dev/null +++ b/changelogs/unreleased/cache-last-commit-sha-for-path.yml @@ -0,0 +1,4 @@ +--- +title: Cache last commit id for path +merge_request: 8098 +author: Hiroyuki Sato diff --git a/lib/api/files.rb b/lib/api/files.rb index 532a317c89e..2e79e22e649 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -68,7 +68,7 @@ module API ref: params[:ref], blob_id: blob.id, commit_id: commit.id, - last_commit_id: repo.last_commit_for_path(commit.sha, params[:file_path]).id + last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path]) } end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index b5a42edd192..af7e89eae05 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -139,6 +139,22 @@ describe Repository, models: true do it { is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8') } end + describe '#last_commit_id_for_path' do + subject { repository.last_commit_id_for_path(sample_commit.id, '.gitignore') } + + it "returns last commit id for a given path" do + is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8') + end + + it "caches last commit id for a given path" do + cache = repository.send(:cache) + key = "last_commit_id_for_path:#{sample_commit.id}:#{Digest::SHA1.hexdigest('.gitignore')}" + + expect(cache).to receive(:fetch).with(key).and_return('c1acaa5') + is_expected.to eq('c1acaa5') + end + end + describe '#find_commits_by_message' do it 'returns commits with messages containing a given string' do commit_ids = repository.find_commits_by_message('submodule').map(&:id) |