Skip to content

Put actual default branch in readme on project creation

James Fargher requested to merge jf_bugfix_default_branch_readme into master

What does this MR do and why?

gitaly#1446

Adds test for when the user specifies a default branch, rather than relying on the system default.

There are two issues going on here. The first is that gitaly uses a heuristic to determine the default branch, and this heuristic returns nothing when no branches exist. This means when the template uses project.default_branch_or_main since there aren't any branches yet, gitaly returns nothing, so the system-wide default us used instead.

The other issue is that the default branch was never actually passed into project.create_repository, instead this service was relying on the fact that the same heuristic in gitaly will return the only branch that exists as the default branch. Once this branch is created, gitlab-rails then updates HEAD with a hook.

The heuristic is quite unintuitive and gitaly are looking to remove it which should allow this code to be simplified in the future.

How to set up and validate locally

The gitlab UI doesn't allow setting the default branch on creation, but the option is still available in the API.

  1. Create a project via the API specifying both readme and some default branch:
    $ curl --request POST --header "PRIVATE-TOKEN: <your-token>" \
         --header "Content-Type: application/json" --data '{
            "name": "new_project", "description": "New Project", "path": "new_project",
            "default_branch": "bananas", "initialize_with_readme": "true"}' \
         --url 'http://127.0.0.1:3000/api/v4/projects/'
    {"id":10,"description":"New Project","name":"new_project","name_with_namespace":"Administrator / new_project","path":"new_project","path_with_namespace":"root/new_project","created_at":"2023-05-02T23:53:06.104Z","default_branch":"bananas","tag_list":[],"topics":[],"ssh_url_to_repo":"ssh://git@127.0.0.1:2222/root/new_project.git","http_url_to_repo":"http://127.0.0.1:3000/root/new_project.git","web_url":"http://127.0.0.1:3000/root/new_project",...
  2. Check the returned project and see that the readme has the proper branch on it: Screenshot_from_2023-05-03_11-53-57

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by James Fargher

Merge request reports