Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy git code (ver < 2.0), fine tune markup tests #19930

Merged
merged 21 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
db0d7a1
clean git support for ver < 2.0
wxiaoguang Jun 10, 2022
d01ca3a
fine tune tests for markup (which requires git module)
wxiaoguang Jun 10, 2022
5e8c6b4
remove unnecessary comments
wxiaoguang Jun 10, 2022
e8fad37
try to fix tests
wxiaoguang Jun 10, 2022
9f89b2f
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 11, 2022
b32101f
try test again
wxiaoguang Jun 11, 2022
187a27f
use const for GitVersionRequired instead of var
wxiaoguang Jun 11, 2022
09e29f4
try to fix integration test
wxiaoguang Jun 11, 2022
94b2386
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 11, 2022
6d87ea8
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 13, 2022
334e5d2
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 13, 2022
038929e
Refactor CheckAttributeReader to make a *git.Repository version
zeripath Jun 13, 2022
a8f2999
Merge pull request #6 from zeripath/fix-git-legacy
wxiaoguang Jun 14, 2022
64959f0
update document for commit signing with Gitea's internal gitconfig
wxiaoguang Jun 14, 2022
8a2b4a8
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 14, 2022
e4dcba6
update document for commit signing with Gitea's internal gitconfig
wxiaoguang Jun 14, 2022
c551d8d
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 15, 2022
30ae9f3
Merge branch 'main' into fix-git-legacy
wxiaoguang Jun 15, 2022
4a3aa85
Merge branch 'main' into fix-git-legacy
lunny Jun 16, 2022
faed560
Merge branch 'main' into fix-git-legacy
lunny Jun 16, 2022
375eaba
Merge branch 'main' into fix-git-legacy
lunny Jun 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,17 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
return false, nil
}

if err := CheckGitVersionAtLeast("1.8"); err == nil {
_, _, err := NewCommand(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunStdString(&RunOpts{Dir: c.repo.Path})
if err == nil {
return true, nil
_, _, err := NewCommand(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunStdString(&RunOpts{Dir: c.repo.Path})
if err == nil {
return true, nil
}
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
return false, nil
}
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
return false, nil
}
}
return false, err
}

result, _, err := NewCommand(c.repo.Ctx, "rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunStdString(&RunOpts{Dir: c.repo.Path})
if err != nil {
return false, err
}

return len(strings.TrimSpace(result)) > 0, nil
return false, err
}

// CommitsBeforeLimit returns num commits before current revision
Expand Down
3 changes: 0 additions & 3 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (

var (
// GitVersionRequired is the minimum Git version required
// At the moment, all code for git 1.x are not changed, if some users want to test with old git client
// or bypass the check, they still have a chance to edit this variable manually.
// If everything works fine, the code for git 1.x could be removed in a separate PR before 1.17 frozen.
GitVersionRequired = "2.0.0"
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

// GitExecutable is the command name of git
Expand Down
26 changes: 9 additions & 17 deletions modules/git/repo_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ type CheckAttributeOpts struct {
func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) {
env := []string{}

if len(opts.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
if len(opts.IndexFile) > 0 {
env = append(env, "GIT_INDEX_FILE="+opts.IndexFile)
}
if len(opts.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
if len(opts.WorkTree) > 0 {
env = append(env, "GIT_WORK_TREE="+opts.WorkTree)
}

Expand All @@ -56,8 +56,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[
}
}

// git check-attr --cached first appears in git 1.7.8
if opts.CachedOnly && CheckGitVersionAtLeast("1.7.8") == nil {
if opts.CachedOnly {
cmdArgs = append(cmdArgs, "--cached")
}

Expand Down Expand Up @@ -125,12 +124,12 @@ type CheckAttributeReader struct {
func (c *CheckAttributeReader) Init(ctx context.Context) error {
cmdArgs := []string{"check-attr", "--stdin", "-z"}

if len(c.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
if len(c.IndexFile) > 0 {
cmdArgs = append(cmdArgs, "--cached")
c.env = append(c.env, "GIT_INDEX_FILE="+c.IndexFile)
}

if len(c.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
if len(c.WorkTree) > 0 {
c.env = append(c.env, "GIT_WORK_TREE="+c.WorkTree)
}

Expand Down Expand Up @@ -160,17 +159,10 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error {
return err
}

if CheckGitVersionAtLeast("1.8.5") == nil {
lw := new(nulSeparatedAttributeWriter)
lw.attributes = make(chan attributeTriple, 5)
lw.closed = make(chan struct{})
c.stdOut = lw
} else {
lw := new(lineSeparatedAttributeWriter)
lw.attributes = make(chan attributeTriple, 5)
lw.closed = make(chan struct{})
c.stdOut = lw
}
lw := new(nulSeparatedAttributeWriter)
lw.attributes = make(chan attributeTriple, 5)
lw.closed = make(chan struct{})
c.stdOut = lw
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions modules/git/repo_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,7 @@ func (repo *Repository) GetDiff(base, head string, w io.Writer) error {

// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
if CheckGitVersionAtLeast("1.7.7") == nil {
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).Run(&RunOpts{
Dir: repo.Path,
Stdout: w,
})
}
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--patience", base, head).Run(&RunOpts{
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).Run(&RunOpts{
Dir: repo.Path,
Stdout: w,
})
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_language_stats_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err

var checker *CheckAttributeReader

if CheckGitVersionAtLeast("1.7.8") == nil {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
{
indexFilename, workTree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
if err == nil {
defer deleteTemporaryFile()
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err

var checker *CheckAttributeReader

if CheckGitVersionAtLeast("1.7.8") == nil {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
{
indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
if err == nil {
defer deleteTemporaryFile()
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opt
_, _ = messageBytes.WriteString(opts.Message)
_, _ = messageBytes.WriteString("\n")

if CheckGitVersionAtLeast("1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
if opts.KeyID != "" || opts.AlwaysSign {
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
}

if CheckGitVersionAtLeast("2.0.0") == nil && opts.NoGPGSign {
if opts.NoGPGSign {
cmd.AddArguments("--no-gpg-sign")
}
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
9 changes: 9 additions & 0 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package markup_test

import (
"context"
"io"
"strings"
"testing"

"code.gitea.io/gitea/modules/emoji"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
. "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
Expand All @@ -25,6 +27,13 @@ var localMetas = map[string]string{
"repoPath": "../../integrations/gitea-repositories-meta/user13/repo11.git/",
}

func TestMain(m *testing.M) {
setting.LoadAllowEmpty()
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
}
}

func TestRender_Commits(t *testing.T) {
setting.AppURL = TestAppURL
test := func(input, expected string) {
Expand Down
8 changes: 8 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package markdown_test

import (
"context"
"strings"
"testing"

Expand All @@ -31,6 +32,13 @@ var localMetas = map[string]string{
"repoPath": "../../../integrations/gitea-repositories-meta/user13/repo11.git/",
}

func TestMain(m *testing.M) {
setting.LoadAllowEmpty()
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
}
}

func TestRender_StandardLinks(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL
Expand Down
22 changes: 10 additions & 12 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,17 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
"-m", "Initial commit",
}

if git.CheckGitVersionAtLeast("1.7.9") == nil {
sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
if sign {
args = append(args, "-S"+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 if git.CheckGitVersionAtLeast("2.0.0") == nil {
args = append(args, "--no-gpg-sign")
sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
if sign {
args = append(args, "-S"+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 {
args = append(args, "--no-gpg-sign")
}

env = append(env,
Expand Down
2 changes: 1 addition & 1 deletion services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff

var checker *git.CheckAttributeReader

if git.CheckGitVersionAtLeast("1.7.8") == nil {
{
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(opts.AfterCommitID)
if err == nil {
defer deleteTemporaryFile()
Expand Down
27 changes: 9 additions & 18 deletions services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,8 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
return "", fmt.Errorf("Unable to write .git/info/sparse-checkout file in tmpBasePath: %v", err)
}

var gitConfigCommand func() *git.Command
if git.CheckGitVersionAtLeast("1.8.0") == nil {
gitConfigCommand = func() *git.Command {
return git.NewCommand(ctx, "config", "--local")
}
} else {
gitConfigCommand = func() *git.Command {
return git.NewCommand(ctx, "config")
}
gitConfigCommand := func() *git.Command {
return git.NewCommand(ctx, "config", "--local")
}

// Switch off LFS process (set required, clean and smudge here also)
Expand Down Expand Up @@ -364,16 +357,14 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User

// Determine if we should sign
signArg := ""
if git.CheckGitVersionAtLeast("1.7.9") == nil {
sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, pr, doer, tmpBasePath, "HEAD", trackingBranch)
if sign {
signArg = "-S" + keyID
if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
committer = signer
}
} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
signArg = "--no-gpg-sign"
sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, pr, doer, tmpBasePath, "HEAD", trackingBranch)
if sign {
signArg = "-S" + keyID
if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
committer = signer
}
} else {
signArg = "--no-gpg-sign"
}

commitTimeStr := time.Now().Format(time.RFC3339)
Expand Down
49 changes: 23 additions & 26 deletions services/repository/files/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,31 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(parent string, author, co
args = []string{"commit-tree", treeHash}
}

// Determine if we should sign
if git.CheckGitVersionAtLeast("1.7.9") == nil {
var sign bool
var keyID string
var signer *git.Signature
if parent != "" {
sign, keyID, signer, _ = asymkey_service.SignCRUDAction(t.ctx, t.repo.RepoPath(), author, t.basePath, parent)
} else {
sign, keyID, signer, _ = asymkey_service.SignInitialCommit(t.ctx, t.repo.RepoPath(), author)
}
if sign {
args = append(args, "-S"+keyID)
if t.repo.GetTrustModel() == repo_model.CommitterTrustModel || t.repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
if committerSig.Name != authorSig.Name || committerSig.Email != authorSig.Email {
// Add trailers
_, _ = messageBytes.WriteString("\n")
_, _ = messageBytes.WriteString("Co-authored-by: ")
_, _ = messageBytes.WriteString(committerSig.String())
_, _ = messageBytes.WriteString("\n")
_, _ = messageBytes.WriteString("Co-committed-by: ")
_, _ = messageBytes.WriteString(committerSig.String())
_, _ = messageBytes.WriteString("\n")
}
committerSig = signer
var sign bool
var keyID string
var signer *git.Signature
if parent != "" {
sign, keyID, signer, _ = asymkey_service.SignCRUDAction(t.ctx, t.repo.RepoPath(), author, t.basePath, parent)
} else {
sign, keyID, signer, _ = asymkey_service.SignInitialCommit(t.ctx, t.repo.RepoPath(), author)
}
if sign {
args = append(args, "-S"+keyID)
if t.repo.GetTrustModel() == repo_model.CommitterTrustModel || t.repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
if committerSig.Name != authorSig.Name || committerSig.Email != authorSig.Email {
// Add trailers
_, _ = messageBytes.WriteString("\n")
_, _ = messageBytes.WriteString("Co-authored-by: ")
_, _ = messageBytes.WriteString(committerSig.String())
_, _ = messageBytes.WriteString("\n")
_, _ = messageBytes.WriteString("Co-committed-by: ")
_, _ = messageBytes.WriteString(committerSig.String())
_, _ = messageBytes.WriteString("\n")
}
} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
args = append(args, "--no-gpg-sign")
committerSig = signer
}
} else {
args = append(args, "--no-gpg-sign")
}

if signoff {
Expand Down