Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultBranch is not set correctly when using template which has 'master' as default #19470

Closed
sschroe opened this issue Apr 23, 2022 · 1 comment · Fixed by #22708
Closed
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented issue/workaround it is or has a workaround type/bug
Milestone

Comments

@sschroe
Copy link

sschroe commented Apr 23, 2022

Description

In #19354 the default branch for new repositories was changed from 'master' to 'main'. When running this new version on existing data where 'master' was still used as default this causes a problem when creating a new repository from a template which still uses 'master'.

The files and master branch are correctly copied to the newly generated repository however giteas internal default branch is set to 'main' instead of 'master'. This causes giteas "Branches" page to remain blank despite showing 1 branch and when cloning the repository git shows warning: remote HEAD refers to nonexistent ref, unable to checkout..

When opening the "Branches" page gitea throws an error: 2022/04/23 10:48:22 ...s/web/repo/branch.go:177:loadBranches() [W] [6263bd56] loadBranches: missing default branch main for 60:sschroe/template_test

Example of a broken repo on the demo site: https://try.gitea.io/steffen/template_test/branches

To reproduce:

  • Run gitea with the default config
  • Create a new template repository and set its main branch to 'master'
  • Create a new repository and select the previously made template with "Git Content(Default Branch)" checked
  • Open the "Branches" page on the new repository

Gitea Version

9550e5a

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

2.36.0

Operating System

Arch Linux

How are you running Gitea?

git clone': git clone --recursive https://github.com/go-gitea/gitea.git
cd gitea
TAGS="bindata sqlite sqlite_unlock_notify" make build
./gitea

Database

SQLite

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 23, 2022

You should update your templates at the moment, or set global default branch back to master. Otherwise Gitea can not guess what's the correct main branch.

For example, even before #19354 , if you create a template repo with main as default branch while global default branch is master, then the same thing happens.

The blank branches page could be another bug. Indeed all branches should be shown correctly even if the default branch is incorrect.

One possible way to improve the behavior is to make Gitea use the only (or latest) branch in template as the default branch for the newly created repo if the default branch name doesn't match.

@wxiaoguang wxiaoguang added the issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented label Apr 23, 2022
@wxiaoguang wxiaoguang added this to the 1.17.0 milestone Apr 23, 2022
@wxiaoguang wxiaoguang added the issue/workaround it is or has a workaround label Apr 23, 2022
@wxiaoguang wxiaoguang modified the milestones: 1.17.0, 1.18.0 Jun 8, 2022
@lunny lunny modified the milestones: 1.18.0, 1.19.0 Oct 21, 2022
jolheiser pushed a commit that referenced this issue Feb 2, 2023
Fix #21994. 
And fix #19470.

While generating new repo from a template, it does something like
"commit to git repo, re-fetch repo model from DB, and update default
branch if it's empty".


https://github.com/go-gitea/gitea/blob/19d5b2f922c2defde579a935fbedb680eb8fff18/modules/repository/generate.go#L241-L253

Unfortunately, when load repo from DB, the default branch will be set to
`setting.Repository.DefaultBranch` if it's empty:


https://github.com/go-gitea/gitea/blob/19d5b2f922c2defde579a935fbedb680eb8fff18/models/repo/repo.go#L228-L233

I believe it's a very old temporary patch but has been kept for many
years, see:
[2d2d85b](2d2d85bb#diff-1851799b06733db4df3ec74385c1e8850ee5aedee70b8b55366910d22725eea8)

I know it's a risk to delete it, may lead to potential behavioral
changes, but we cannot keep the outdated `FIXME` forever. On the other
hand, an empty `DefaultBranch` does make sense: an empty repo doesn't
have one conceptually (actually, Gitea will still set it to
`setting.Repository.DefaultBranch` to make it safer).
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented issue/workaround it is or has a workaround type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants