From 76e10bd706a82af461c7c572ecd317654194b288 Mon Sep 17 00:00:00 2001 From: ghinfey Date: Wed, 31 Jan 2024 11:38:07 +0000 Subject: [PATCH 1/3] Expose committed_before and committed_after Changelog: changed MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/143354 --- app/controllers/concerns/parse_commit_date.rb | 10 +++ .../projects/commits_controller.rb | 7 +- .../projects/merge_requests_controller.rb | 6 +- .../projects/commits_controller_spec.rb | 71 +++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 app/controllers/concerns/parse_commit_date.rb diff --git a/app/controllers/concerns/parse_commit_date.rb b/app/controllers/concerns/parse_commit_date.rb new file mode 100644 index 00000000000000..3d881ae09c608e --- /dev/null +++ b/app/controllers/concerns/parse_commit_date.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module ParseCommitDate + extend ActiveSupport::Concern + + def convert_date_to_epoch(date) + Date.strptime(date, "%Y-%m-%d")&.to_time&.to_i if date + rescue Date::Error, TypeError + end +end diff --git a/app/controllers/projects/commits_controller.rb b/app/controllers/projects/commits_controller.rb index 2d2712ebe4ded8..915b6b6e9b8d73 100644 --- a/app/controllers/projects/commits_controller.rb +++ b/app/controllers/projects/commits_controller.rb @@ -5,6 +5,7 @@ class Projects::CommitsController < Projects::ApplicationController include ExtractsPath include RendersCommits + include ParseCommitDate COMMITS_DEFAULT_LIMIT = 40 prepend_before_action(only: [:show]) { authenticate_sessionless_user!(:rss) } @@ -75,6 +76,8 @@ def set_commits @offset = (permitted_params[:offset] || 0).to_i search = permitted_params[:search] author = permitted_params[:author] + committed_before = convert_date_to_epoch(permitted_params[:committed_before]) + committed_after = convert_date_to_epoch(permitted_params[:committed_after]) # fully_qualified_ref is available in some situations from ExtractsRef ref = @fully_qualified_ref || @ref @@ -89,6 +92,8 @@ def set_commits offset: @offset } options[:author] = author if author.present? + options[:before] = committed_before if committed_before.present? + options[:after] = committed_after if committed_after.present? @repository.commits(ref, **options) end @@ -101,6 +106,6 @@ def set_commits end def permitted_params - params.permit(:limit, :offset, :search, :author) + params.permit(:limit, :offset, :search, :author, :committed_before, :committed_after) end end diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb index 12984c36d4ca05..7755b346b81ee6 100644 --- a/app/controllers/projects/merge_requests_controller.rb +++ b/app/controllers/projects/merge_requests_controller.rb @@ -12,6 +12,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo include DiffHelper include Gitlab::Cache::Helpers include MergeRequestsHelper + include ParseCommitDate prepend_before_action(only: [:index]) { authenticate_sessionless_user!(:rss) } skip_before_action :merge_request, only: [:index, :bulk_update, :export_csv] @@ -639,11 +640,6 @@ def pinned_file_url(project, merge_request) diff_head: true ) end - - def convert_date_to_epoch(date) - Date.strptime(date, "%Y-%m-%d")&.to_time&.to_i if date - rescue Date::Error, TypeError - end end Projects::MergeRequestsController.prepend_mod_with('Projects::MergeRequestsController') diff --git a/spec/controllers/projects/commits_controller_spec.rb b/spec/controllers/projects/commits_controller_spec.rb index 956167ce8385b3..807864278024f1 100644 --- a/spec/controllers/projects/commits_controller_spec.rb +++ b/spec/controllers/projects/commits_controller_spec.rb @@ -102,6 +102,77 @@ end end + context 'date range' do + let(:id) { "master/README.md" } + let(:base_repository_params) do + { + path: "README.md", + limit: described_class::COMMITS_DEFAULT_LIMIT, + offset: 0 + } + end + + let(:base_request_params) do + { + namespace_id: project.namespace, + project_id: project, + id: id + } + end + + shared_examples 'repository commits call' do + it 'passes the correct params' do + expect_any_instance_of(Repository).to receive(:commits).with( + "master", + repository_params + ).and_call_original + + get :show, params: request_params + + expect(response).to be_successful + end + end + + context 'when committed_before param' do + context 'is valid' do + let(:request_params) { base_request_params.merge(committed_before: '2020-01-01') } + let(:repository_params) { base_repository_params.merge(before: 1577836800) } + + it_behaves_like 'repository commits call' + end + + context 'is invalid' do + let(:request_params) { base_request_params.merge(committed_before: 'xxx') } + let(:repository_params) { base_repository_params } + + it_behaves_like 'repository commits call' + end + + context 'is not provided' do + let(:request_params) { base_request_params } + let(:repository_params) { base_repository_params } + + it_behaves_like 'repository commits call' + end + end + + context 'with committed_after param' do + context 'is valid' do + let(:request_params) { base_request_params.merge(committed_after: '2020-01-01') } + let(:repository_params) { base_repository_params.merge(after: 1577836800) } + + it_behaves_like 'repository commits call' + end + + context 'is invalid' do + let(:request_params) { base_request_params.merge(committed_after: 'xxx') } + let(:repository_params) { base_repository_params } + + it_behaves_like 'repository commits call' + end + end + end + it 'loads tags for commits' do expect_next_instance_of(CommitCollection) do |collection| expect(collection).to receive(:load_tags) -- GitLab From eb6a73d16453d1bfcd78c175af094e70b357f390 Mon Sep 17 00:00:00 2001 From: ghinfey Date: Fri, 2 Feb 2024 14:39:34 +0000 Subject: [PATCH 2/3] Update history documentation --- doc/user/project/repository/git_history.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/user/project/repository/git_history.md b/doc/user/project/repository/git_history.md index 1a0f4203b19c20..e25046c34e919a 100644 --- a/doc/user/project/repository/git_history.md +++ b/doc/user/project/repository/git_history.md @@ -28,6 +28,10 @@ The name and email information provided are retrieved from the [Git configuration](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration) of the contributor when a commit is made. +In some very large repositories it is possible that [commit history requests may timeout](https://gitlab.com/gitlab-org/gitaly/-/issues/5426). +In these cases it is possible to constrain the search period by adding `committed_before` and `committed_after` dates as parameters. +To do this add `committed_before=YYYY-MM-DD` or `committed_after=YYYY-MM-DD` parameters to the URL. [For example this URL](https://gitlab.com/gitlab-org/gitlab/-/commits/master/README.md?ref_type=heads&committed_before=2010-11-22&committed_after=2008-05-15). + ## Associated `git` command If you're running `git` from the command line, the equivalent command -- GitLab From 1ff606688a21ca60fc1553a5312769dcbdfe6f28 Mon Sep 17 00:00:00 2001 From: Marcin Sedlak-Jakubowski Date: Tue, 6 Feb 2024 14:16:26 +0000 Subject: [PATCH 3/3] Add doc subsection --- doc/user/project/repository/git_history.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/doc/user/project/repository/git_history.md b/doc/user/project/repository/git_history.md index e25046c34e919a..4e090871dfd103 100644 --- a/doc/user/project/repository/git_history.md +++ b/doc/user/project/repository/git_history.md @@ -28,9 +28,21 @@ The name and email information provided are retrieved from the [Git configuration](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration) of the contributor when a commit is made. -In some very large repositories it is possible that [commit history requests may timeout](https://gitlab.com/gitlab-org/gitaly/-/issues/5426). -In these cases it is possible to constrain the search period by adding `committed_before` and `committed_after` dates as parameters. -To do this add `committed_before=YYYY-MM-DD` or `committed_after=YYYY-MM-DD` parameters to the URL. [For example this URL](https://gitlab.com/gitlab-org/gitlab/-/commits/master/README.md?ref_type=heads&committed_before=2010-11-22&committed_after=2008-05-15). +## Limit history range + +> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/423108) in GitLab 16.9. + +In these cases you can constrain the search period by adding `committed_before` and `committed_after` dates as parameters. +To do this, add `&` and `committed_before=YYYY-MM-DD` or `committed_after=YYYY-MM-DD` parameters to the URL. + +For example: + +```plaintext +https://gitlab.com/gitlab-org/gitlab/-/commits/master/README.md?ref_type=heads&committed_before=2010-11-22&committed_after=2008-05-15 +``` + +Doing this might be necessary to fix [commit history requests timeouts](https://gitlab.com/gitlab-org/gitaly/-/issues/5426) +in very large repositories. ## Associated `git` command -- GitLab