Title: project creation ignores default_branch and default-branch contradicts documentation · python-gitlab/python-gitlab · Discussion #3182 · GitHub
Open Graph Title: project creation ignores default_branch and default-branch contradicts documentation · python-gitlab/python-gitlab · Discussion #3182
X Title: project creation ignores default_branch and default-branch contradicts documentation · python-gitlab/python-gitlab · Discussion #3182
Description: project creation ignores default_branch and default-branch contradicts documentation
Open Graph Description: 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 ...
X Description: 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) ...
Opengraph URL: https://github.com/python-gitlab/python-gitlab/discussions/3182
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"project creation ignores default_branch and default-branch contradicts documentation","text":"Description of the problem, including code/CLI snippet
\nI'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,
\n group_id = gl.groups.list(search=namespace)[0].id\n project = gl.projects.create(\n {\n 'name': project_name,\n 'path': repo_name,\n 'namespace_id': group_id,\n 'default_branch': 'master',\n }\n )\n
\nThe same thing happens from CLI:
\ngitlab project create --name myproj --path myrepo --namespace-id GROUP_ID --default-branch master\n
\nExpected Behavior
\nAfter creation as described, the project object should show default_branch == \"master\" and the branch should show as protected.
\nRegardless of being created from CLI or SDK.
\nActual Behavior
\nThe 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.
\nIf 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.
\nWorkaround (possibly a hack)
\nCall these 2 functions, in order:
\ndef gitlab_project_repo_setup_content(project):\n try:\n project.files.get('README.md', ref='main')\n except GitlabGetError:\n print('Creating file in repo')\n project.files.create(\n {\n 'file_path': 'README.md',\n 'branch': 'main',\n 'content': '# Placeholder file',\n 'commit_message': 'Add README to initialize repository',\n }\n )\n\ndef gitlab_project_set_default_branch(project):\n \"\"\"\n Workaround for gitlab default branch can only be changed after there is content added:\n if you set default_branch in the project.create(), it gets ignored, ie project.default_branch will\n still be 'main'. Same goes for command line. It appears that there HAS to be at least one\n file in the reop for the default branch to be changed.\n \"\"\"\n # verify that that readme exists, this will raise exception if not:\n project.files.get('README.md', ref='main')\n\n project.branches.create({'branch': 'master', 'ref': 'main'})\n project.default_branch = 'master'\n project.save()\n\n project.protectedbranches.create({'name': 'master'})\n project.protectedbranches.delete('main')\n project.branches.delete('main')\n
\nAfter running these 2, the git repo will have only master branch, and gitlab console will show this and that it is default and protected,
\nSpecifications
\n\n- python-gitlab version: 5.6.0
\n- Gitlab server version (or gitlab.com): gitlab.com
\n
","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"Maybe try with initialize_with_readme?
","upvoteCount":1,"url":"https://github.com/python-gitlab/python-gitlab/discussions/3182#discussioncomment-12957077"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:e7cce1b3-9eed-4081-ee67-ef01421d33b8 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | DA14:3F36AF:35AD166:49635BE:69738A47 |
| html-safe-nonce | 9336a09165b69ef78c1a861b39e392cba7b31485e9041a917659c2f0fddc970c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQTE0OjNGMzZBRjozNUFEMTY2OjQ5NjM1QkU6Njk3MzhBNDciLCJ2aXNpdG9yX2lkIjoiNzEwMDEyMDkzNTQxMDQwMzkxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 512b0d3f93333572d6da91001a41acd5ef7842770fcd5867462802e14396a40d |
| hovercard-subject-tag | discussion:8247352 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-gitlab/python-gitlab/3182/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/269bb648032fb1952d01371a620d075fb823d349c35c507cf92f1f6cf7c5d917/python-gitlab/python-gitlab/discussions/3182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/269bb648032fb1952d01371a620d075fb823d349c35c507cf92f1f6cf7c5d917/python-gitlab/python-gitlab/discussions/3182 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | f0b5e2d1da32de5a0b7bcfbc5ba007c93e8e63098fba29e1200efb674aa72e66 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-gitlab/python-gitlab git https://github.com/python-gitlab/python-gitlab.git |
| octolytics-dimension-user_id | 28886265 |
| octolytics-dimension-user_login | python-gitlab |
| octolytics-dimension-repository_id | 8077625 |
| octolytics-dimension-repository_nwo | python-gitlab/python-gitlab |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 8077625 |
| octolytics-dimension-repository_network_root_nwo | python-gitlab/python-gitlab |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c96653671324f9b6d4c5f787435acf56a06087dd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width