mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-25 10:11:52 +00:00
before: gitrepo vs git packages after: git package fully handle all git operations by the way, use `WithRepo(repo)` instead of `WithDir(repo.Path)` to hide path details. benefits: 1. remove all unnecessary wrappers, developers no need to struggle with "which package should be used" 2. simplify code, RepositoryFacade can (will) be used everywhere, all "path" details are (will be) hidden
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/log"
|
|
repo_module "gitea.dev/modules/repository"
|
|
asymkey_service "gitea.dev/services/asymkey"
|
|
)
|
|
|
|
// initRepoCommit temporarily changes with work directory.
|
|
func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User) (err error) {
|
|
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,
|
|
)
|
|
committerName := sig.Name
|
|
committerEmail := sig.Email
|
|
|
|
if stdout, _, err := gitcmd.NewCommand("add", "--all").WithDir(tmpPath).RunStdString(ctx); err != nil {
|
|
log.Error("git add --all failed: Stdout: %s\nError: %v", stdout, err)
|
|
return fmt.Errorf("git add --all: %w", err)
|
|
}
|
|
|
|
cmd := gitcmd.NewCommand("commit", "--message=Initial commit").
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)
|
|
|
|
sign, key, signer, _ := asymkey_service.SignInitialCommit(ctx, u)
|
|
if sign {
|
|
if key.Format != "" {
|
|
cmd.AddConfig("gpg.format", key.Format)
|
|
}
|
|
cmd.AddOptionFormat("-S%s", key.KeyID)
|
|
|
|
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
|
|
// need to set the committer to the KeyID owner
|
|
committerName = signer.Name
|
|
committerEmail = signer.Email
|
|
}
|
|
} else {
|
|
cmd.AddArguments("--no-gpg-sign")
|
|
}
|
|
|
|
env = append(env,
|
|
"GIT_COMMITTER_NAME="+committerName,
|
|
"GIT_COMMITTER_EMAIL="+committerEmail,
|
|
)
|
|
|
|
if stdout, _, err := cmd.WithDir(tmpPath).WithEnv(env).RunStdString(ctx); err != nil {
|
|
log.Error("Failed to commit: %v: Stdout: %s\nError: %v", cmd.LogString(), stdout, err)
|
|
return fmt.Errorf("git commit: %w", err)
|
|
}
|
|
|
|
if err := git.PushFromLocal(ctx, tmpPath, repo, git.PushOptions{
|
|
LocalRefName: "HEAD",
|
|
Branch: repo.DefaultBranch,
|
|
Env: repo_module.InternalPushingEnvironment(u, repo),
|
|
}); err != nil {
|
|
log.Error("Failed to push back to HEAD Error: %v", err)
|
|
return fmt.Errorf("git push: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|