Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

AnsweredbyJohnVillalovos
schollii asked this question inQ&A
Discussion options

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, thedefault_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 showdefault_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
You must be logged in to vote

Maybe try withinitialize_with_readme?

Replies: 3 comments 5 replies

Comment options

Maybe try withinitialize_with_readme?

You must be logged in to vote
0 replies
Answer selected byschollii
Comment options

@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?

You must be logged in to vote
2 replies
@JohnVillalovos
Comment options

@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?

Sorry, I don't know off the top of my head, besides asking your administrator.

As a note, always good to look at the upstream docs. I found theinitialize_with_readme at:https://docs.gitlab.com/api/projects/#create-a-project

I don't believe this is a bug/issue with python-gitlab, but a bug/issue with how GitLab works.

@schollii
Comment options

Just saw your answer here. Indeed. That's good to know about the upstream docs. Due to lack of familiarity with how the gitlab package is constructed, I did not think of looking there. I think a short blurb onhttps://python-gitlab.readthedocs.io/en/stable/api-usage.html# explaining that these docs should be used in conjunction withhttps://docs.gitlab.com/api/api_resources/ could save new users some time and pain.

Comment options

So adding'initialize_with_readme': True worked!! Can't believe how much time I spent on this so I'm super thankful for the answer.

However I still think it would be useful to make this more obvious. I searched the web and all the hits I find, nothing indicates thatdefault_branch requiresinitialize_with_readme. Could this be added in the docs, eg in the projects examples?

Also I guess this should be converted back to an issue?

You must be logged in to vote
3 replies
@JohnVillalovos
Comment options

A pull request would be welcome if would like to do it.

Glad to hear it worked for you.

@schollii
Comment options

Just saw your other answer about the API docs, so how about this instead: I'll create a PR to edit the pagehttps://python-gitlab.readthedocs.io/en/stable/api-usage.html#, adding an explanation that python docs should be used in conjunction withhttps://docs.gitlab.com/api/api_resources/.

@JohnVillalovos
Comment options

Sounds good. I thought there was already something there but if so it sounds like it isn't prominent enough.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@schollii@JohnVillalovos
Converted from issue

This discussion was converted from issue #3181 on April 26, 2025 20:41.


[8]ページ先頭

©2009-2025 Movatter.jp