diff --git a/templates/repo/empty.tmpl b/templates/repo/empty.tmpl index 2032b7e5d2..3a8995bfea 100644 --- a/templates/repo/empty.tmpl +++ b/templates/repo/empty.tmpl @@ -50,7 +50,7 @@

{{ctx.Locale.Tr "repo.create_new_repo_command"}}

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}}
 git add README.md
 git commit -m "first commit"
diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go
index cd81b37ffc..59cf642719 100644
--- a/tests/integration/repo_test.go
+++ b/tests/integration/repo_test.go
@@ -9,6 +9,7 @@ import (
 	"net/http"
 	"net/url"
 	"path"
+	"regexp"
 	"strings"
 	"testing"
 	"time"
@@ -19,6 +20,7 @@ import (
 	"forgejo.org/models/unittest"
 	user_model "forgejo.org/models/user"
 	"forgejo.org/modules/git"
+	"forgejo.org/modules/optional"
 	"forgejo.org/modules/setting"
 	"forgejo.org/modules/test"
 	"forgejo.org/modules/translation"
@@ -1438,3 +1440,58 @@ func TestBlameDirectory(t *testing.T) {
 	req = NewRequest(t, "GET", "/user2/repo59/blame/branch/master/deep")
 	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"))
+	})
+}
diff --git a/tests/test_utils.go b/tests/test_utils.go
index e6aa9c7963..721adbd497 100644
--- a/tests/test_utils.go
+++ b/tests/test_utils.go
@@ -355,6 +355,7 @@ type DeclarativeRepoOptions struct {
 	WikiBranch    optional.Option[string]
 	AutoInit      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()) {
@@ -378,14 +379,15 @@ func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts
 
 	// Create the repository
 	repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{
-		Name:          repoName,
-		Description:   "Temporary Repo",
-		AutoInit:      autoInit,
-		Gitignores:    "",
-		License:       "WTFPL",
-		Readme:        "Default",
-		DefaultBranch: "main",
-		IsTemplate:    opts.IsTemplate.Value(),
+		Name:             repoName,
+		Description:      "Temporary Repo",
+		AutoInit:         autoInit,
+		Gitignores:       "",
+		License:          "WTFPL",
+		Readme:           "Default",
+		DefaultBranch:    "main",
+		IsTemplate:       opts.IsTemplate.Value(),
+		ObjectFormatName: opts.ObjectFormat.Value(),
 	})
 	require.NoError(t, err)
 	assert.NotEmpty(t, repo)