2020-01-12 13:11:17 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-09-23 17:45:36 +02:00
|
|
|
"context"
|
2020-01-12 13:11:17 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 13:11:17 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-06-17 22:53:55 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 22:05:34 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-12-10 09:14:24 +01:00
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2020-01-12 13:11:17 +01:00
|
|
|
|
|
|
|
"github.com/unknwon/com"
|
|
|
|
)
|
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts models.CreateRepoOptions) error {
|
2020-01-12 13:11:17 +01:00
|
|
|
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.
|
2022-01-20 00:26:57 +01:00
|
|
|
if stdout, err := git.NewCommandContext(ctx, "clone", repoPath, tmpDir).
|
2020-01-12 13:11:17 +01:00
|
|
|
SetDescription(fmt.Sprintf("prepareRepoCommit (git clone): %s to %s", repoPath, tmpDir)).
|
|
|
|
RunInDirWithEnv("", env); err != nil {
|
|
|
|
log.Error("Failed to clone from %v into %s: stdout: %s\nError: %v", repo, tmpDir, stdout, err)
|
|
|
|
return fmt.Errorf("git clone: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// README
|
|
|
|
data, err := models.GetRepoInitFile("readme", opts.Readme)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepoInitFile[%s]: %v", opts.Readme, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
match := map[string]string{
|
|
|
|
"Name": repo.Name,
|
|
|
|
"Description": repo.Description,
|
|
|
|
"CloneURL.SSH": cloneLink.SSH,
|
|
|
|
"CloneURL.HTTPS": cloneLink.HTTPS,
|
2020-04-07 03:40:38 +02:00
|
|
|
"OwnerName": repo.OwnerName,
|
2020-01-12 13:11:17 +01:00
|
|
|
}
|
2021-09-22 07:38:34 +02:00
|
|
|
if err = os.WriteFile(filepath.Join(tmpDir, "README.md"),
|
2020-01-12 13:11:17 +01:00
|
|
|
[]byte(com.Expand(string(data), match)), 0644); err != nil {
|
|
|
|
return fmt.Errorf("write README.md: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// .gitignore
|
|
|
|
if len(opts.Gitignores) > 0 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
names := strings.Split(opts.Gitignores, ",")
|
|
|
|
for _, name := range names {
|
|
|
|
data, err = models.GetRepoInitFile("gitignore", name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepoInitFile[%s]: %v", name, err)
|
|
|
|
}
|
|
|
|
buf.WriteString("# ---> " + name + "\n")
|
|
|
|
buf.Write(data)
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.Len() > 0 {
|
2021-09-22 07:38:34 +02:00
|
|
|
if err = os.WriteFile(filepath.Join(tmpDir, ".gitignore"), buf.Bytes(), 0644); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("write .gitignore: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LICENSE
|
|
|
|
if len(opts.License) > 0 {
|
|
|
|
data, err = models.GetRepoInitFile("license", opts.License)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepoInitFile[%s]: %v", opts.License, err)
|
|
|
|
}
|
|
|
|
|
2021-09-22 07:38:34 +02:00
|
|
|
if err = os.WriteFile(filepath.Join(tmpDir, "LICENSE"), data, 0644); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("write LICENSE: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initRepoCommit temporarily changes with work directory.
|
2022-01-20 00:26:57 +01:00
|
|
|
func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
|
2020-01-12 13:11:17 +01:00
|
|
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
|
|
|
|
|
|
|
sig := u.NewGitSig()
|
|
|
|
// Because this may call hooks we should pass in the environment
|
|
|
|
env := append(os.Environ(),
|
|
|
|
"GIT_AUTHOR_NAME="+sig.Name,
|
|
|
|
"GIT_AUTHOR_EMAIL="+sig.Email,
|
|
|
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
|
|
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
|
|
|
)
|
2020-09-19 18:44:55 +02:00
|
|
|
committerName := sig.Name
|
|
|
|
committerEmail := sig.Email
|
2020-01-12 13:11:17 +01:00
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
if stdout, err := git.NewCommandContext(ctx, "add", "--all").
|
2020-01-12 13:11:17 +01:00
|
|
|
SetDescription(fmt.Sprintf("initRepoCommit (git add): %s", tmpPath)).
|
|
|
|
RunInDir(tmpPath); err != nil {
|
|
|
|
log.Error("git add --all failed: Stdout: %s\nError: %v", stdout, err)
|
|
|
|
return fmt.Errorf("git add --all: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-05 18:42:58 +02:00
|
|
|
err = git.LoadGitVersion()
|
2020-01-12 13:11:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to get git version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
|
|
"-m", "Initial commit",
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:42:08 +02:00
|
|
|
if git.CheckGitVersionAtLeast("1.7.9") == nil {
|
2022-01-20 00:26:57 +01:00
|
|
|
sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
|
2020-01-12 13:11:17 +01:00
|
|
|
if sign {
|
|
|
|
args = append(args, "-S"+keyID)
|
2020-09-19 18:44:55 +02:00
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
|
2020-09-19 18:44:55 +02:00
|
|
|
// need to set the committer to the KeyID owner
|
|
|
|
committerName = signer.Name
|
|
|
|
committerEmail = signer.Email
|
|
|
|
}
|
2020-10-21 17:42:08 +02:00
|
|
|
} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
args = append(args, "--no-gpg-sign")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:44:55 +02:00
|
|
|
env = append(env,
|
|
|
|
"GIT_COMMITTER_NAME="+committerName,
|
|
|
|
"GIT_COMMITTER_EMAIL="+committerEmail,
|
|
|
|
)
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
if stdout, err := git.NewCommandContext(ctx, args...).
|
2020-01-12 13:11:17 +01:00
|
|
|
SetDescription(fmt.Sprintf("initRepoCommit (git commit): %s", tmpPath)).
|
|
|
|
RunInDirWithEnv(tmpPath, env); err != nil {
|
|
|
|
log.Error("Failed to commit: %v: Stdout: %s\nError: %v", args, stdout, err)
|
|
|
|
return fmt.Errorf("git commit: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:14:51 +01:00
|
|
|
if len(defaultBranch) == 0 {
|
2020-06-17 22:53:55 +02:00
|
|
|
defaultBranch = setting.Repository.DefaultBranch
|
2020-03-26 20:14:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
if stdout, err := git.NewCommandContext(ctx, "push", "origin", "HEAD:"+defaultBranch).
|
2020-01-12 13:11:17 +01:00
|
|
|
SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)).
|
|
|
|
RunInDirWithEnv(tmpPath, models.InternalPushingEnvironment(u, repo)); err != nil {
|
2020-11-28 22:00:38 +01:00
|
|
|
log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err)
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("git push: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
func checkInitRepository(ctx context.Context, owner, name string) (err error) {
|
2020-01-12 13:11:17 +01:00
|
|
|
// Somehow the directory could exist.
|
2021-12-10 02:27:50 +01:00
|
|
|
repoPath := repo_model.RepoPath(owner, name)
|
2020-11-28 03:42:08 +01:00
|
|
|
isExist, err := util.IsExist(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isExist {
|
2021-12-12 16:48:20 +01:00
|
|
|
return repo_model.ErrRepoFilesAlreadyExist{
|
2020-09-25 06:09:23 +02:00
|
|
|
Uname: owner,
|
|
|
|
Name: name,
|
|
|
|
}
|
2020-01-12 13:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init git bare new repository.
|
2022-01-20 00:26:57 +01:00
|
|
|
if err = git.InitRepository(ctx, repoPath, true); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("git.InitRepository: %v", err)
|
2020-01-20 21:01:19 +01:00
|
|
|
} else if err = createDelegateHooks(repoPath); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRepository initializes README and .gitignore if needed.
|
2021-12-10 02:27:50 +01:00
|
|
|
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts models.CreateRepoOptions) (err error) {
|
2022-01-20 00:26:57 +01:00
|
|
|
if err = checkInitRepository(ctx, repo.OwnerName, repo.Name); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize repository according to user's choice.
|
|
|
|
if opts.AutoInit {
|
2021-09-22 07:38:34 +02:00
|
|
|
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
2020-01-12 13:11:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.RepoPath(), err)
|
|
|
|
}
|
2020-08-11 22:05:34 +02:00
|
|
|
defer func() {
|
|
|
|
if err := util.RemoveAll(tmpDir); err != nil {
|
|
|
|
log.Warn("Unable to remove temporary directory: %s: Error: %v", tmpDir, err)
|
|
|
|
}
|
|
|
|
}()
|
2020-01-12 13:11:17 +01:00
|
|
|
|
|
|
|
if err = prepareRepoCommit(ctx, repo, tmpDir, repoPath, opts); err != nil {
|
|
|
|
return fmt.Errorf("prepareRepoCommit: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply changes and commit.
|
2022-01-20 00:26:57 +01:00
|
|
|
if err = initRepoCommit(ctx, tmpDir, repo, u, opts.DefaultBranch); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("initRepoCommit: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-fetch the repository from database before updating it (else it would
|
|
|
|
// override changes that were done earlier with sql)
|
2021-12-10 02:27:50 +01:00
|
|
|
if repo, err = repo_model.GetRepositoryByIDCtx(ctx, repo.ID); err != nil {
|
2020-01-12 13:11:17 +01:00
|
|
|
return fmt.Errorf("getRepositoryByID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.AutoInit {
|
|
|
|
repo.IsEmpty = true
|
|
|
|
}
|
|
|
|
|
2020-09-25 06:09:23 +02:00
|
|
|
repo.DefaultBranch = setting.Repository.DefaultBranch
|
|
|
|
|
2020-03-26 20:14:51 +01:00
|
|
|
if len(opts.DefaultBranch) > 0 {
|
|
|
|
repo.DefaultBranch = opts.DefaultBranch
|
2022-01-20 00:26:57 +01:00
|
|
|
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
|
2020-07-05 17:25:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("openRepository: %v", err)
|
|
|
|
}
|
2020-12-11 22:41:59 +01:00
|
|
|
defer gitRepo.Close()
|
2020-07-05 17:25:46 +02:00
|
|
|
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
|
|
|
return fmt.Errorf("setDefaultBranch: %v", err)
|
|
|
|
}
|
2020-03-26 20:14:51 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 13:11:17 +01:00
|
|
|
if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
|
|
|
|
return fmt.Errorf("updateRepository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|