fix(ui): clarify repo init instruction for sha256 (#7394)

- When the repository is initalized with a different objectformat than sha1, ensure that the empty repository instructions reflects that the `git init` command also needs to be initialized with that objectformat.
- Resolves https://codeberg.org/codeberg/community/issues/1837
- Added integration test.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7394
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-03-30 18:08:05 +00:00 committed by 0ko
parent 86039a89fc
commit 5275fbd4ea
3 changed files with 68 additions and 9 deletions

View file

@ -50,7 +50,7 @@
<h3>{{ctx.Locale.Tr "repo.create_new_repo_command"}}</h3> <h3>{{ctx.Locale.Tr "repo.create_new_repo_command"}}</h3>
<div class="markup"> <div class="markup">
<pre><code>touch README.md <pre><code>touch README.md
git init git init{{if eq .Repository.ObjectFormatName "sha256"}} --object-format=sha256{{end}}
{{if ne .Repository.DefaultBranch "master"}}git switch -c {{.Repository.DefaultBranch}}{{end}} {{if ne .Repository.DefaultBranch "master"}}git switch -c {{.Repository.DefaultBranch}}{{end}}
git add README.md git add README.md
git commit -m "first commit" git commit -m "first commit"

View file

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"regexp"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -19,6 +20,7 @@ import (
"forgejo.org/models/unittest" "forgejo.org/models/unittest"
user_model "forgejo.org/models/user" user_model "forgejo.org/models/user"
"forgejo.org/modules/git" "forgejo.org/modules/git"
"forgejo.org/modules/optional"
"forgejo.org/modules/setting" "forgejo.org/modules/setting"
"forgejo.org/modules/test" "forgejo.org/modules/test"
"forgejo.org/modules/translation" "forgejo.org/modules/translation"
@ -1438,3 +1440,58 @@ func TestBlameDirectory(t *testing.T) {
req = NewRequest(t, "GET", "/user2/repo59/blame/branch/master/deep") req = NewRequest(t, "GET", "/user2/repo59/blame/branch/master/deep")
MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
} }
func TestInitInstructions(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
session := loginUser(t, user.Name)
sha256Repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
Name: optional.Some("sha256-instruction"),
AutoInit: optional.Some(false),
EnabledUnits: optional.Some([]unit_model.Type{unit_model.TypeCode}),
ObjectFormat: optional.Some("sha256"),
})
defer f()
sha1Repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
Name: optional.Some("sha1-instruction"),
AutoInit: optional.Some(false),
EnabledUnits: optional.Some([]unit_model.Type{unit_model.TypeCode}),
ObjectFormat: optional.Some("sha1"),
})
defer f()
portMatcher := regexp.MustCompile(`localhost:\d+`)
t.Run("sha256", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+sha256Repo.FullName()), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assert.Equal(t, `touch README.md
git init --object-format=sha256
git switch -c main
git add README.md
git commit -m "first commit"
git remote add origin http://localhost/user2/sha256-instruction.git
git push -u origin main`, portMatcher.ReplaceAllString(htmlDoc.Find(".empty-repo-guide code").First().Text(), "localhost"))
})
t.Run("sha1", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+sha1Repo.FullName()), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assert.Equal(t, `touch README.md
git init
git switch -c main
git add README.md
git commit -m "first commit"
git remote add origin http://localhost/user2/sha1-instruction.git
git push -u origin main`, portMatcher.ReplaceAllString(htmlDoc.Find(".empty-repo-guide code").First().Text(), "localhost"))
})
}

View file

@ -355,6 +355,7 @@ type DeclarativeRepoOptions struct {
WikiBranch optional.Option[string] WikiBranch optional.Option[string]
AutoInit optional.Option[bool] AutoInit optional.Option[bool]
IsTemplate optional.Option[bool] IsTemplate optional.Option[bool]
ObjectFormat optional.Option[string]
} }
func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) { func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) {
@ -386,6 +387,7 @@ func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts
Readme: "Default", Readme: "Default",
DefaultBranch: "main", DefaultBranch: "main",
IsTemplate: opts.IsTemplate.Value(), IsTemplate: opts.IsTemplate.Value(),
ObjectFormatName: opts.ObjectFormat.Value(),
}) })
require.NoError(t, err) require.NoError(t, err)
assert.NotEmpty(t, repo) assert.NotEmpty(t, repo)