mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-16 08:01:15 +00:00
Backport #40376 by @bircni Plain repository creation marked a new repository as ready before its git data, hooks and initial commit existed, and auto-init cloned the new repository to build its initial commit on top of whatever it already contained. - Auto-init builds the initial commit in a freshly initialized temp repository instead of a clone - New repositories are inserted as being created and marked ready once initialized, as fork, template generation, adoption and migrations already do --------- Co-authored-by: bircni <bircni@icloud.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
8 files changed
+23
-32
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user