2023-03-07 20:07:35 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-03-10 04:17:04 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
2023-03-07 20:07:35 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2023-03-07 20:07:35 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2023-03-10 04:17:04 +01:00
|
|
|
// doMergeStyleSquash gets a commit author signature for squash commits
|
|
|
|
func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
|
|
|
|
if err := ctx.pr.Issue.LoadPoster(ctx); err != nil {
|
|
|
|
log.Error("%-v Issue[%d].LoadPoster: %v", ctx.pr, ctx.pr.Issue.ID, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get an signature from the same user in one of the commits, as the
|
|
|
|
// poster email might be private or commits might have a different signature
|
|
|
|
// than the primary email address of the poster.
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpenPath(ctx, ctx.tmpBasePath)
|
2023-03-10 04:17:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%-v Unable to open base repository: %v", ctx.pr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
commits, err := gitRepo.CommitsBetweenIDs(trackingBranch, "HEAD")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%-v Unable to get commits between: %s %s: %v", ctx.pr, "HEAD", trackingBranch, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueEmails := make(container.Set[string])
|
|
|
|
for _, commit := range commits {
|
|
|
|
if commit.Author != nil && uniqueEmails.Add(commit.Author.Email) {
|
|
|
|
commitUser, _ := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
|
|
|
if commitUser != nil && commitUser.ID == ctx.pr.Issue.Poster.ID {
|
|
|
|
return commit.Author, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.pr.Issue.Poster.NewGitSig(), nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:07:35 +00:00
|
|
|
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
|
|
|
|
func doMergeStyleSquash(ctx *mergeContext, message string) error {
|
2023-03-10 04:17:04 +01:00
|
|
|
sig, err := getAuthorSignatureSquash(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getAuthorSignatureSquash: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:48:52 +08:00
|
|
|
cmdMerge := git.NewCommand(ctx, "merge", "--squash").AddDynamicArguments(trackingBranch)
|
|
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleSquash, cmdMerge); err != nil {
|
2023-03-07 20:07:35 +00:00
|
|
|
log.Error("%-v Unable to merge --squash tracking into base: %v", ctx.pr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:48:52 +08:00
|
|
|
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
|
|
|
|
// add trailer
|
|
|
|
message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String())
|
|
|
|
}
|
|
|
|
cmdCommit := git.NewCommand(ctx, "commit").
|
|
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
|
|
|
|
AddOptionFormat("--message=%s", message)
|
|
|
|
if ctx.signKeyID == "" {
|
|
|
|
cmdCommit.AddArguments("--no-gpg-sign")
|
2023-03-07 20:07:35 +00:00
|
|
|
} else {
|
2023-03-09 23:48:52 +08:00
|
|
|
cmdCommit.AddOptionFormat("-S%s", ctx.signKeyID)
|
|
|
|
}
|
|
|
|
if err := cmdCommit.Run(ctx.RunOpts()); err != nil {
|
|
|
|
log.Error("git commit %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
|
|
|
|
return fmt.Errorf("git commit [%s:%s -> %s:%s]: %w\n%s\n%s", ctx.pr.HeadRepo.FullName(), ctx.pr.HeadBranch, ctx.pr.BaseRepo.FullName(), ctx.pr.BaseBranch, err, ctx.outbuf.String(), ctx.errbuf.String())
|
2023-03-07 20:07:35 +00:00
|
|
|
}
|
|
|
|
ctx.outbuf.Reset()
|
|
|
|
ctx.errbuf.Reset()
|
|
|
|
return nil
|
|
|
|
}
|