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

Create from template does not reference the right branch. #21994

Closed
saul-jb opened this issue Nov 30, 2022 · 5 comments · Fixed by #22708
Closed

Create from template does not reference the right branch. #21994

saul-jb opened this issue Nov 30, 2022 · 5 comments · Fixed by #22708
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented type/bug

Comments

@saul-jb
Copy link

saul-jb commented Nov 30, 2022

Description

If you create a template with a different default branch (e.g. not 'main') and create another repository from that template, it sets the default branch to an empty reference to 'main' causing this error when cloning it warning: remote HEAD refers to nonexistent ref, unable to checkout.. This can be resolved by changing the default branch to the one from the template but that should be the default behavior or at least it should provide an option to specify the name of the branch.

To reproduce:

  1. Create a template repository with the default branch as anything but 'main'.
  2. Create a repository from that template including the git content of the template.
  3. Check to see if the default branch points to an invalid ref.

Gitea Version

1.18.0

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

Problem exists on both https://try.gitea.io/ and docker.

Database

No response

@wolfogre
Copy link
Member

I cannot reproduce it on v1.18.0.

Are you sure that you found it on v1.18.0? The release time is after the time you opened this issue.

You can find the exact version in the footer of any page.

@wolfogre wolfogre added the issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail label Jan 19, 2023
@saul-jb
Copy link
Author

saul-jb commented Jan 19, 2023

I cannot reproduce it on v1.18.0.

Are you sure that you found it on v1.18.0? The release time is after the time you opened this issue.

You can find the exact version in the footer of any page.

Just checked and the issue still remains, version is: 1.18.0+rc0 and I also checked it remains on try.gitea.io version 1.19.0+dev-331-gb383652e0: https://try.gitea.io/saul/repo-2-from-template/src/branch/master.

@wolfogre wolfogre removed the issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail label Jan 20, 2023
@wolfogre
Copy link
Member

wolfogre commented Jan 29, 2023

Just checked and the issue still remains, version is: 1.18.0+rc0 and I also checked it remains on try.gitea.io version 1.19.0+dev-331-gb383652e0: try.gitea.io/saul/repo-2-from-template/src/branch/master.

Sorry, could you please point it out where's the problem? I don't see anything special in the example repo you give:

image

And I also created a repo from your template, but I didn't see any warning when browsing or cloning it.

@saul-jb
Copy link
Author

saul-jb commented Jan 29, 2023

Your repo seems to have the same problem.

Sorry, could you please point it out where's the problem? I don't see anything special in the example repo you give:

> git clone https://try.gitea.io/saul/repo-2-from-template.git
Cloning into 'repo-2-from-template'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
warning: remote HEAD refers to nonexistent ref, unable to checkout.

It is that last line that is warning that the HEAD refers to a non-existent ref.

When I try to pull changes, the problem becomes more obvious:

> git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master


Git again complains here because we are not on a branch that exists on the origin. The UI also reflects this:

image

See how the selection shows main but is greyed out since it does not exist and you need to manually change it to master (or any other branch that actually exists).

If you leave the template's default branch as main this issue does not occur. It seems to me that the main branch is hard-coded somewhere when creating a repository from a template rather than using the default branch from the template.

@wolfogre wolfogre added the issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented label Jan 29, 2023
@wolfogre
Copy link
Member

wolfogre commented Feb 1, 2023

Duplicated with #19470.

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 type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants