Skip to content

Commit 5275fbd

Browse files
Gusted0ko
authored andcommitted
fix(ui): clarify repo init instruction for sha256 (go-gitea#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 <[email protected]> Co-authored-by: Gusted <[email protected]> Co-committed-by: Gusted <[email protected]>
1 parent 86039a8 commit 5275fbd

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

templates/repo/empty.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<h3>{{ctx.Locale.Tr "repo.create_new_repo_command"}}</h3>
5151
<div class="markup">
5252
<pre><code>touch README.md
53-
git init
53+
git init{{if eq .Repository.ObjectFormatName "sha256"}} --object-format=sha256{{end}}
5454
{{if ne .Repository.DefaultBranch "master"}}git switch -c {{.Repository.DefaultBranch}}{{end}}
5555
git add README.md
5656
git commit -m "first commit"

tests/integration/repo_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111
"path"
12+
"regexp"
1213
"strings"
1314
"testing"
1415
"time"
@@ -19,6 +20,7 @@ import (
1920
"forgejo.org/models/unittest"
2021
user_model "forgejo.org/models/user"
2122
"forgejo.org/modules/git"
23+
"forgejo.org/modules/optional"
2224
"forgejo.org/modules/setting"
2325
"forgejo.org/modules/test"
2426
"forgejo.org/modules/translation"
@@ -1438,3 +1440,58 @@ func TestBlameDirectory(t *testing.T) {
14381440
req = NewRequest(t, "GET", "/user2/repo59/blame/branch/master/deep")
14391441
MakeRequest(t, req, http.StatusNotFound)
14401442
}
1443+
1444+
func TestInitInstructions(t *testing.T) {
1445+
defer tests.PrepareTestEnv(t)()
1446+
1447+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
1448+
session := loginUser(t, user.Name)
1449+
1450+
sha256Repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
1451+
Name: optional.Some("sha256-instruction"),
1452+
AutoInit: optional.Some(false),
1453+
EnabledUnits: optional.Some([]unit_model.Type{unit_model.TypeCode}),
1454+
ObjectFormat: optional.Some("sha256"),
1455+
})
1456+
defer f()
1457+
1458+
sha1Repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
1459+
Name: optional.Some("sha1-instruction"),
1460+
AutoInit: optional.Some(false),
1461+
EnabledUnits: optional.Some([]unit_model.Type{unit_model.TypeCode}),
1462+
ObjectFormat: optional.Some("sha1"),
1463+
})
1464+
defer f()
1465+
1466+
portMatcher := regexp.MustCompile(`localhost:\d+`)
1467+
1468+
t.Run("sha256", func(t *testing.T) {
1469+
defer tests.PrintCurrentTest(t)()
1470+
1471+
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+sha256Repo.FullName()), http.StatusOK)
1472+
1473+
htmlDoc := NewHTMLParser(t, resp.Body)
1474+
assert.Equal(t, `touch README.md
1475+
git init --object-format=sha256
1476+
git switch -c main
1477+
git add README.md
1478+
git commit -m "first commit"
1479+
git remote add origin http://localhost/user2/sha256-instruction.git
1480+
git push -u origin main`, portMatcher.ReplaceAllString(htmlDoc.Find(".empty-repo-guide code").First().Text(), "localhost"))
1481+
})
1482+
1483+
t.Run("sha1", func(t *testing.T) {
1484+
defer tests.PrintCurrentTest(t)()
1485+
1486+
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+sha1Repo.FullName()), http.StatusOK)
1487+
1488+
htmlDoc := NewHTMLParser(t, resp.Body)
1489+
assert.Equal(t, `touch README.md
1490+
git init
1491+
git switch -c main
1492+
git add README.md
1493+
git commit -m "first commit"
1494+
git remote add origin http://localhost/user2/sha1-instruction.git
1495+
git push -u origin main`, portMatcher.ReplaceAllString(htmlDoc.Find(".empty-repo-guide code").First().Text(), "localhost"))
1496+
})
1497+
}

tests/test_utils.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ type DeclarativeRepoOptions struct {
355355
WikiBranch optional.Option[string]
356356
AutoInit optional.Option[bool]
357357
IsTemplate optional.Option[bool]
358+
ObjectFormat optional.Option[string]
358359
}
359360

360361
func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) {
@@ -378,14 +379,15 @@ func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts
378379

379380
// Create the repository
380381
repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{
381-
Name: repoName,
382-
Description: "Temporary Repo",
383-
AutoInit: autoInit,
384-
Gitignores: "",
385-
License: "WTFPL",
386-
Readme: "Default",
387-
DefaultBranch: "main",
388-
IsTemplate: opts.IsTemplate.Value(),
382+
Name: repoName,
383+
Description: "Temporary Repo",
384+
AutoInit: autoInit,
385+
Gitignores: "",
386+
License: "WTFPL",
387+
Readme: "Default",
388+
DefaultBranch: "main",
389+
IsTemplate: opts.IsTemplate.Value(),
390+
ObjectFormatName: opts.ObjectFormat.Value(),
389391
})
390392
require.NoError(t, err)
391393
assert.NotEmpty(t, repo)

0 commit comments

Comments
 (0)