Skip to content

Redirect to the new default branch from a deleted default branch

Nick Thomas requested to merge 329100-redirect-last-default-branch into master

What does this MR do?

Adds a column to store the old name of the default branch for a project; stores a value in it every time the default branch changes; and sets up a redirect from the old name to the new name if the old one doesn't exist in the repository any more.

For now, only trigger this for projects/blob#show and projects/tree#show. However, we may want to roll it out to a number of other controllers in the future: #329100 (comment 620834617)

When it all works together, we get pages like this when visiting, e.g., http://localhost:3000/gitlab-org/gitlab/-/blob/foo-master/README.md:

Screenshots (strongly suggested)

Screenshot_from_2021-07-07_17-13-31

Does this MR meet the acceptance criteria?

Information for database reviewers

db:migrate

== 20210705124128 AddProjectSettingsPreviousDefaultBranch: migrating ==========
-- add_column(:project_settings, :previous_default_branch, :text)
   -> 0.0020s
== 20210705124128 AddProjectSettingsPreviousDefaultBranch: migrated (0.0020s) =
== 20210707173645 AddProjectSettingsPreviousDefaultBranchTextLimit: migrating =
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0003s
-- execute("ALTER TABLE project_settings\nADD CONSTRAINT check_3a03e7557a\nCHECK ( char_length(previous_default_branch) <= 4096 )\nNOT VALID;\n")
   -> 0.0025s
-- current_schema()
   -> 0.0002s
-- execute("SET statement_timeout TO 0")
   -> 0.0006s
-- execute("ALTER TABLE project_settings VALIDATE CONSTRAINT check_3a03e7557a;")
   -> 0.0020s
-- execute("RESET ALL")
   -> 0.0005s
== 20210707173645 AddProjectSettingsPreviousDefaultBranchTextLimit: migrated (0.0185s) 

db:rollback

== 20210707173645 AddProjectSettingsPreviousDefaultBranchTextLimit: reverting =
-- execute("ALTER TABLE project_settings\nDROP CONSTRAINT IF EXISTS check_3a03e7557a\n")
   -> 0.0019s
== 20210707173645 AddProjectSettingsPreviousDefaultBranchTextLimit: reverted (0.0084s) 
== 20210705124128 AddProjectSettingsPreviousDefaultBranch: reverting ==========
-- remove_column(:project_settings, :previous_default_branch, :text)
   -> 0.0052s
== 20210705124128 AddProjectSettingsPreviousDefaultBranch: reverted (0.0066s) =

Conformity

Availability and Testing

To test this locally, take a random project in GDK and create a new branch that is a copy of master (I called mine foo-master).

Change to that as the default branch; previous_default_branch will then be master, but since master exists, no redirect will happen.

Change the default branch back to master. Still no redirect, but now previous_default_branch will be foo-master.

Delete foo-master. Now visiting a blob, tree, etc, URL will redirect you to the equivalent page for master.

Related to #329100 (closed)

Edited by Nick Thomas

Merge request reports