Visitar URL original
project creation ignores default_branch and default-branch contradicts documentation · Issue #3181 · python-gitlab/python-gitlab · GitHub
Skip to content

project creation ignores default_branch and default-branch contradicts documentation #3181

Description

@schollii

Description of the problem, including code/CLI snippet

I've spent a couple hours on this, but basically when creating a project from the python package or from CLI, the default_branch (in SDK) and --default-branch settings are ignored,

    group_id = gl.groups.list(search=namespace)[0].id
    project = gl.projects.create(
        {
            'name': project_name,
            'path': repo_name,
            'namespace_id': group_id,
            'default_branch': 'master',
        }
    )

The same thing happens from CLI:

gitlab project create --name myproj --path myrepo --namespace-id GROUP_ID --default-branch master

Expected Behavior

After creation as described, the project object should show default_branch == "master" and the branch should show as protected.

Regardless of being created from CLI or SDK.

Actual Behavior

The CLI and SDK both show that the default branch is "main". The console says that a default branch cannot be set on an empty repo.

If this is not possible because of how the gitlab package is a thin wrapper over gitlab REST API, the readme in this repo should at least mention such discrepencies between the docs (auto generated from the REST API, shown in the CLI --help output) and actual behavior.

Workaround (possibly a hack)

Call these 2 functions, in order:

def gitlab_project_repo_setup_content(project):
    try:
        project.files.get('README.md', ref='main')
    except GitlabGetError:
        print('Creating file in repo')
        project.files.create(
            {
                'file_path': 'README.md',
                'branch': 'main',
                'content': '# Placeholder file',
                'commit_message': 'Add README to initialize repository',
            }
        )

def gitlab_project_set_default_branch(project):
    """
    Workaround for gitlab default branch can only be changed after there is content added:
    if you set default_branch in the project.create(), it gets ignored, ie project.default_branch will
    still be 'main'. Same goes for command line. It appears that there HAS to be at least one
    file in the reop for the default branch to be changed.
    """
    # verify that that readme exists, this will raise exception if not:
    project.files.get('README.md', ref='main')

    project.branches.create({'branch': 'master', 'ref': 'main'})
    project.default_branch = 'master'
    project.save()

    project.protectedbranches.create({'name': 'master'})
    project.protectedbranches.delete('main')
    project.branches.delete('main')

After running these 2, the git repo will have only master branch, and gitlab console will show this and that it is default and protected,

Specifications

  • python-gitlab version: 5.6.0
  • Gitlab server version (or gitlab.com): gitlab.com

Activity

  1. JohnVillalovos commented on Apr 26, 2025

    @JohnVillalovos
    Member

    Maybe try with initialize_with_readme?

  2. schollii commented on Apr 26, 2025

    @schollii
    Author

    @JohnVillalovos thanks that's interesting. Led me to this: #2869 (comment), and in particular

    in a group with fully protected default branches

    I'm thinking that could be the cause here, since the gitlab account is part of an organization where there could be such rules in place, is there a way that I can verify whether that's the case?

  3. locked and limited conversation to collaborators on Apr 26, 2025
  4. converted this issue into a discussion #3182 on Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions