diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index 2fe6280349d..3a23213e372 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -176,7 +176,6 @@ func Migrate(ctx *context.APIContext) { GitServiceType: gitServiceType, IsPrivate: opts.Private || setting.Repository.ForcePrivate, IsMirror: opts.Mirror, - Status: repo_model.RepositoryBeingMigrated, }, false) if err != nil { handleMigrateError(ctx, repoOwner, err) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 5134aa0a0a1..ebbc0023f3f 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -106,7 +106,6 @@ func (g *GiteaLocalUploader) CreateRepo(ctx context.Context, repo *base.Reposito GitServiceType: opts.GitServiceType, IsPrivate: opts.Private || setting.Repository.ForcePrivate, IsMirror: opts.Mirror, - Status: repo_model.RepositoryBeingMigrated, }, false) } else { r, err = repo_model.GetRepositoryByID(ctx, opts.MigrateToRepoID) diff --git a/services/repository/create.go b/services/repository/create.go index 50875cd544e..78dc54d066d 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -49,34 +49,12 @@ type CreateRepoOptions struct { IsMirror bool IsTemplate bool AutoInit bool - Status repo_model.RepositoryStatus TrustModel repo_model.TrustModelType MirrorInterval string ObjectFormatName string } func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir string, opts CreateRepoOptions) error { - commitTimeStr := time.Now().Format(time.RFC3339) - authorSig := repo.Owner.NewGitSig() - - // Because this may call hooks we should pass in the environment - env := append(os.Environ(), - "GIT_AUTHOR_NAME="+authorSig.Name, - "GIT_AUTHOR_EMAIL="+authorSig.Email, - "GIT_AUTHOR_DATE="+commitTimeStr, - "GIT_COMMITTER_NAME="+authorSig.Name, - "GIT_COMMITTER_EMAIL="+authorSig.Email, - "GIT_COMMITTER_DATE="+commitTimeStr, - ) - - // Clone to temporary path and do the init commit. - if err := git.CloneRepoToLocal(ctx, repo, tmpDir, git.CloneRepoOptions{ - Env: env, - }); err != nil { - log.Error("Failed to clone from %v into %s\nError: %v", repo, tmpDir, err) - return fmt.Errorf("git clone: %w", err) - } - // README data, err := options.Readme(opts.Readme) if err != nil { @@ -96,8 +74,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir // here we could just log the error and continue the rendering log.Error("unable to expand template vars for repo README: %s, err: %v", opts.Readme, err) } - if err = os.WriteFile(filepath.Join(tmpDir, "README.md"), - []byte(res), 0o644); err != nil { + if err = os.WriteFile(filepath.Join(tmpDir, "README.md"), []byte(res), 0o644); err != nil { return fmt.Errorf("write README.md: %w", err) } @@ -126,7 +103,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir if len(opts.License) > 0 { data, err = repo_module.GetLicense(opts.License, &repo_module.LicenseValues{ Owner: repo.OwnerName, - Email: authorSig.Email, + Email: repo.Owner.GetEmail(), Repo: repo.Name, Year: time.Now().Format("2006"), }) @@ -139,6 +116,13 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir } } + // the initial commit must only contain the files written above, so start from a fresh git dir + if err = util.RemoveAllWithRetry(filepath.Join(tmpDir, ".git")); err != nil { + return fmt.Errorf("remove git dir: %w", err) + } + if err = git.InitRepositoryLocal(ctx, tmpDir, false, repo.ObjectFormatName); err != nil { + return fmt.Errorf("git init: %w", err) + } return nil } @@ -242,7 +226,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, owner *user_model.User, IsFsckEnabled: !opts.IsMirror, IsTemplate: opts.IsTemplate, CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch, - Status: opts.Status, + Status: repo_model.RepositoryBeingMigrated, IsEmpty: !opts.AutoInit, TrustModel: opts.TrustModel, IsMirror: opts.IsMirror, diff --git a/services/repository/create_test.go b/services/repository/create_test.go index 86dd6188a3c..89d522d810f 100644 --- a/services/repository/create_test.go +++ b/services/repository/create_test.go @@ -5,6 +5,7 @@ package repository import ( "os" + "path/filepath" "testing" repo_model "gitea.dev/models/repo" @@ -35,6 +36,14 @@ func TestCreateRepositoryDirectly(t *testing.T) { unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: user2.Name, Name: createdRepo.Name}) + assert.NoError(t, git.ForceFastImport(t.Context(), createdRepo, []git.FastImportCommit{{ + Ref: git.BranchPrefix + createdRepo.DefaultBranch, + Files: []git.FastImportFile{{Path: "existing.txt"}}, + }})) + tmpDir := t.TempDir() + assert.NoError(t, prepareRepoCommit(t.Context(), createdRepo, tmpDir, CreateRepoOptions{Readme: "Default"})) + assert.NoFileExists(t, filepath.Join(tmpDir, "existing.txt")) + err = DeleteRepositoryDirectly(t.Context(), createdRepo.ID) assert.NoError(t, err) }) diff --git a/services/task/task.go b/services/task/task.go index 869e2427a4e..ecb18383778 100644 --- a/services/task/task.go +++ b/services/task/task.go @@ -9,7 +9,6 @@ import ( "fmt" admin_model "gitea.dev/models/admin" - repo_model "gitea.dev/models/repo" user_model "gitea.dev/models/user" "gitea.dev/modules/graceful" "gitea.dev/modules/json" @@ -114,7 +113,6 @@ func CreateMigrateTask(ctx context.Context, doer, u *user_model.User, opts base. GitServiceType: opts.GitServiceType, IsPrivate: opts.Private || setting.Repository.ForcePrivate, IsMirror: opts.Mirror, - Status: repo_model.RepositoryBeingMigrated, }, false) if err != nil { task.EndTime = timeutil.TimeStampNow() diff --git a/tests/integration/actions_schedule_test.go b/tests/integration/actions_schedule_test.go index c6aa79cf34a..66a778e3f29 100644 --- a/tests/integration/actions_schedule_test.go +++ b/tests/integration/actions_schedule_test.go @@ -162,7 +162,6 @@ func testScheduleUpdateMirrorSync(t *testing.T) { IsPrivate: opts.Private, IsMirror: opts.Mirror, DefaultBranch: repo.DefaultBranch, - Status: repo_model.RepositoryBeingMigrated, }, false) assert.NoError(t, err) assert.True(t, mirrorRepo.IsMirror) diff --git a/tests/integration/git_smart_http_test.go b/tests/integration/git_smart_http_test.go index 9fec0cf392d..29c3e6ca61d 100644 --- a/tests/integration/git_smart_http_test.go +++ b/tests/integration/git_smart_http_test.go @@ -30,6 +30,10 @@ func TestGitSmartHTTP(t *testing.T) { testGitArchiveRemote(t, u) t.Run("AnonymousAccess-Repo", func(t *testing.T) { testGitSmartHTTPPrivateRepoAnonymousAccess(t, false) }) t.Run("AnonymousAccess-Wiki", func(t *testing.T) { testGitSmartHTTPPrivateRepoAnonymousAccess(t, true) }) + t.Run("BeingCreated", func(t *testing.T) { + require.NoError(t, repo_model.UpdateRepositoryColsNoAutoTime(t.Context(), &repo_model.Repository{ID: 1, Status: repo_model.RepositoryBeingMigrated}, "status")) + MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/info/refs?service=git-receive-pack").AddBasicAuth("user2"), http.StatusForbidden) + }) }) } diff --git a/tests/integration/mirror_pull_test.go b/tests/integration/mirror_pull_test.go index 9f80a3b1e49..52387d5fc35 100644 --- a/tests/integration/mirror_pull_test.go +++ b/tests/integration/mirror_pull_test.go @@ -53,7 +53,6 @@ func TestMirrorPull(t *testing.T) { Description: opts.Description, IsPrivate: opts.Private, IsMirror: opts.Mirror, - Status: repo_model.RepositoryBeingMigrated, }, false) assert.NoError(t, err) assert.True(t, mirrorRepo.IsMirror, "expected pull-mirror repo to be marked as a mirror immediately after its creation")