mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Move some functions to gitrepo package (#35503)
This commit is contained in:
		| @@ -1,25 +0,0 @@ | |||||||
| // Copyright 2017 The Gitea Authors. All rights reserved. |  | ||||||
| // SPDX-License-Identifier: MIT |  | ||||||
|  |  | ||||||
| package git |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"fmt" |  | ||||||
|  |  | ||||||
| 	"code.gitea.io/gitea/modules/git/gitcmd" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| // LineBlame returns the latest commit at the given line |  | ||||||
| func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) { |  | ||||||
| 	res, _, err := gitcmd.NewCommand("blame"). |  | ||||||
| 		AddOptionFormat("-L %d,%d", line, line). |  | ||||||
| 		AddOptionValues("-p", revision). |  | ||||||
| 		AddDashesAndList(file).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: path}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
| 	if len(res) < 40 { |  | ||||||
| 		return nil, fmt.Errorf("invalid result of blame: %s", res) |  | ||||||
| 	} |  | ||||||
| 	return repo.GetCommit(res[:40]) |  | ||||||
| } |  | ||||||
| @@ -5,70 +5,12 @@ | |||||||
| package git | package git | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"errors" |  | ||||||
| 	"strings" |  | ||||||
|  |  | ||||||
| 	"code.gitea.io/gitea/modules/git/gitcmd" | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // BranchPrefix base dir of the branch information file store on git | // BranchPrefix base dir of the branch information file store on git | ||||||
| const BranchPrefix = "refs/heads/" | const BranchPrefix = "refs/heads/" | ||||||
|  |  | ||||||
| // IsReferenceExist returns true if given reference exists in the repository. |  | ||||||
| func IsReferenceExist(ctx context.Context, repoPath, name string) bool { |  | ||||||
| 	_, _, err := gitcmd.NewCommand("show-ref", "--verify").AddDashesAndList(name).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath}) |  | ||||||
| 	return err == nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // IsBranchExist returns true if given branch exists in the repository. |  | ||||||
| func IsBranchExist(ctx context.Context, repoPath, name string) bool { |  | ||||||
| 	return IsReferenceExist(ctx, repoPath, BranchPrefix+name) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) { |  | ||||||
| 	stdout, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
| 	stdout = strings.TrimSpace(stdout) |  | ||||||
| 	if !strings.HasPrefix(stdout, BranchPrefix) { |  | ||||||
| 		return "", errors.New("the HEAD is not a branch: " + stdout) |  | ||||||
| 	} |  | ||||||
| 	return strings.TrimPrefix(stdout, BranchPrefix), nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // DeleteBranchOptions Option(s) for delete branch |  | ||||||
| type DeleteBranchOptions struct { |  | ||||||
| 	Force bool |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // DeleteBranch delete a branch by name on repository. |  | ||||||
| func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error { |  | ||||||
| 	cmd := gitcmd.NewCommand("branch") |  | ||||||
|  |  | ||||||
| 	if opts.Force { |  | ||||||
| 		cmd.AddArguments("-D") |  | ||||||
| 	} else { |  | ||||||
| 		cmd.AddArguments("-d") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	cmd.AddDashesAndList(name) |  | ||||||
| 	_, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) |  | ||||||
|  |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // CreateBranch create a new branch |  | ||||||
| func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error { |  | ||||||
| 	cmd := gitcmd.NewCommand("branch") |  | ||||||
| 	cmd.AddDashesAndList(branch, oldbranchOrCommit) |  | ||||||
|  |  | ||||||
| 	_, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) |  | ||||||
|  |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // AddRemote adds a new remote to repository. | // AddRemote adds a new remote to repository. | ||||||
| func (repo *Repository) AddRemote(name, url string, fetch bool) error { | func (repo *Repository) AddRemote(name, url string, fetch bool) error { | ||||||
| 	cmd := gitcmd.NewCommand("remote", "add") | 	cmd := gitcmd.NewCommand("remote", "add") | ||||||
| @@ -80,9 +22,3 @@ func (repo *Repository) AddRemote(name, url string, fetch bool) error { | |||||||
| 	_, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | 	_, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  |  | ||||||
| // RenameBranch rename a branch |  | ||||||
| func (repo *Repository) RenameBranch(from, to string) error { |  | ||||||
| 	_, _, err := gitcmd.NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path}) |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -7,14 +7,12 @@ package git | |||||||
| import ( | import ( | ||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"context" |  | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strconv" |  | ||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"code.gitea.io/gitea/modules/git/gitcmd" | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
| @@ -87,57 +85,8 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis | |||||||
| 	return w.numLines, nil | 	return w.numLines, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions |  | ||||||
| // TODO: it can be merged with another "GetDiffShortStat" in the future |  | ||||||
| func GetDiffShortStatByCmdArgs(ctx context.Context, repoPath string, trustedArgs gitcmd.TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) { |  | ||||||
| 	// Now if we call: |  | ||||||
| 	// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 |  | ||||||
| 	// we get: |  | ||||||
| 	// " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n" |  | ||||||
| 	cmd := gitcmd.NewCommand("diff", "--shortstat").AddArguments(trustedArgs...).AddDynamicArguments(dynamicArgs...) |  | ||||||
| 	stdout, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return 0, 0, 0, err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return parseDiffStat(stdout) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| var shortStatFormat = regexp.MustCompile( |  | ||||||
| 	`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`) |  | ||||||
|  |  | ||||||
| var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`) | var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`) | ||||||
|  |  | ||||||
| func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) { |  | ||||||
| 	if len(stdout) == 0 || stdout == "\n" { |  | ||||||
| 		return 0, 0, 0, nil |  | ||||||
| 	} |  | ||||||
| 	groups := shortStatFormat.FindStringSubmatch(stdout) |  | ||||||
| 	if len(groups) != 4 { |  | ||||||
| 		return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	numFiles, err = strconv.Atoi(groups[1]) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %w", stdout, err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if len(groups[2]) != 0 { |  | ||||||
| 		totalAdditions, err = strconv.Atoi(groups[2]) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %w", stdout, err) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if len(groups[3]) != 0 { |  | ||||||
| 		totalDeletions, err = strconv.Atoi(groups[3]) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %w", stdout, err) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return numFiles, totalAdditions, totalDeletions, err |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // GetDiff generates and returns patch data between given revisions, optimized for human readability | // GetDiff generates and returns patch data between given revisions, optimized for human readability | ||||||
| func (repo *Repository) GetDiff(compareArg string, w io.Writer) error { | func (repo *Repository) GetDiff(compareArg string, w io.Writer) error { | ||||||
| 	stderr := new(bytes.Buffer) | 	stderr := new(bytes.Buffer) | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								modules/gitrepo/blame.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								modules/gitrepo/blame.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | // Copyright 2025 The Gitea Authors. All rights reserved. | ||||||
|  | // SPDX-License-Identifier: MIT | ||||||
|  |  | ||||||
|  | package gitrepo | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  |  | ||||||
|  | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func LineBlame(ctx context.Context, repo Repository, revision, file string, line uint) (string, error) { | ||||||
|  | 	return runCmdString(ctx, repo, | ||||||
|  | 		gitcmd.NewCommand("blame"). | ||||||
|  | 			AddOptionFormat("-L %d,%d", line, line). | ||||||
|  | 			AddOptionValues("-p", revision). | ||||||
|  | 			AddDashesAndList(file)) | ||||||
|  | } | ||||||
| @@ -5,6 +5,8 @@ package gitrepo | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"errors" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/git/gitcmd" | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
| @@ -34,23 +36,61 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str | |||||||
|  |  | ||||||
| // SetDefaultBranch sets default branch of repository. | // SetDefaultBranch sets default branch of repository. | ||||||
| func SetDefaultBranch(ctx context.Context, repo Repository, name string) error { | func SetDefaultBranch(ctx context.Context, repo Repository, name string) error { | ||||||
| 	_, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD"). | 	_, err := runCmdString(ctx, repo, gitcmd.NewCommand("symbolic-ref", "HEAD"). | ||||||
| 		AddDynamicArguments(git.BranchPrefix+name). | 		AddDynamicArguments(git.BranchPrefix+name)) | ||||||
| 		RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) |  | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetDefaultBranch gets default branch of repository. | // GetDefaultBranch gets default branch of repository. | ||||||
| func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) { | func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) { | ||||||
| 	return git.GetDefaultBranch(ctx, repoPath(repo)) | 	stdout, err := runCmdString(ctx, repo, gitcmd.NewCommand("symbolic-ref", "HEAD")) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	stdout = strings.TrimSpace(stdout) | ||||||
|  | 	if !strings.HasPrefix(stdout, git.BranchPrefix) { | ||||||
|  | 		return "", errors.New("the HEAD is not a branch: " + stdout) | ||||||
|  | 	} | ||||||
|  | 	return strings.TrimPrefix(stdout, git.BranchPrefix), nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // IsReferenceExist returns true if given reference exists in the repository. | // IsReferenceExist returns true if given reference exists in the repository. | ||||||
| func IsReferenceExist(ctx context.Context, repo Repository, name string) bool { | func IsReferenceExist(ctx context.Context, repo Repository, name string) bool { | ||||||
| 	return git.IsReferenceExist(ctx, repoPath(repo), name) | 	_, err := runCmdString(ctx, repo, gitcmd.NewCommand("show-ref", "--verify").AddDashesAndList(name)) | ||||||
|  | 	return err == nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // IsBranchExist returns true if given branch exists in the repository. | // IsBranchExist returns true if given branch exists in the repository. | ||||||
| func IsBranchExist(ctx context.Context, repo Repository, name string) bool { | func IsBranchExist(ctx context.Context, repo Repository, name string) bool { | ||||||
| 	return IsReferenceExist(ctx, repo, git.BranchPrefix+name) | 	return IsReferenceExist(ctx, repo, git.BranchPrefix+name) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // DeleteBranch delete a branch by name on repository. | ||||||
|  | func DeleteBranch(ctx context.Context, repo Repository, name string, force bool) error { | ||||||
|  | 	cmd := gitcmd.NewCommand("branch") | ||||||
|  |  | ||||||
|  | 	if force { | ||||||
|  | 		cmd.AddArguments("-D") | ||||||
|  | 	} else { | ||||||
|  | 		cmd.AddArguments("-d") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	cmd.AddDashesAndList(name) | ||||||
|  | 	_, err := runCmdString(ctx, repo, cmd) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // CreateBranch create a new branch | ||||||
|  | func CreateBranch(ctx context.Context, repo Repository, branch, oldbranchOrCommit string) error { | ||||||
|  | 	cmd := gitcmd.NewCommand("branch") | ||||||
|  | 	cmd.AddDashesAndList(branch, oldbranchOrCommit) | ||||||
|  |  | ||||||
|  | 	_, err := runCmdString(ctx, repo, cmd) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // RenameBranch rename a branch | ||||||
|  | func RenameBranch(ctx context.Context, repo Repository, from, to string) error { | ||||||
|  | 	_, err := runCmdString(ctx, repo, gitcmd.NewCommand("branch", "-m").AddDynamicArguments(from, to)) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										15
									
								
								modules/gitrepo/command.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								modules/gitrepo/command.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | // Copyright 2025 The Gitea Authors. All rights reserved. | ||||||
|  | // SPDX-License-Identifier: MIT | ||||||
|  |  | ||||||
|  | package gitrepo | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  |  | ||||||
|  | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func runCmdString(ctx context.Context, repo Repository, cmd *gitcmd.Command) (string, error) { | ||||||
|  | 	res, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) | ||||||
|  | 	return res, err | ||||||
|  | } | ||||||
| @@ -12,9 +12,8 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func GitConfigGet(ctx context.Context, repo Repository, key string) (string, error) { | func GitConfigGet(ctx context.Context, repo Repository, key string) (string, error) { | ||||||
| 	result, _, err := gitcmd.NewCommand("config", "--get"). | 	result, err := runCmdString(ctx, repo, gitcmd.NewCommand("config", "--get"). | ||||||
| 		AddDynamicArguments(key). | 		AddDynamicArguments(key)) | ||||||
| 		RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
| @@ -28,9 +27,8 @@ func getRepoConfigLockKey(repoStoragePath string) string { | |||||||
| // GitConfigAdd add a git configuration key to a specific value for the given repository. | // GitConfigAdd add a git configuration key to a specific value for the given repository. | ||||||
| func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error { | func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error { | ||||||
| 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | ||||||
| 		_, _, err := gitcmd.NewCommand("config", "--add"). | 		_, err := runCmdString(ctx, repo, gitcmd.NewCommand("config", "--add"). | ||||||
| 			AddDynamicArguments(key, value). | 			AddDynamicArguments(key, value)) | ||||||
| 			RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) |  | ||||||
| 		return err | 		return err | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| @@ -40,9 +38,8 @@ func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error | |||||||
| // If the key exists, it will be updated to the new value. | // If the key exists, it will be updated to the new value. | ||||||
| func GitConfigSet(ctx context.Context, repo Repository, key, value string) error { | func GitConfigSet(ctx context.Context, repo Repository, key, value string) error { | ||||||
| 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | ||||||
| 		_, _, err := gitcmd.NewCommand("config"). | 		_, err := runCmdString(ctx, repo, gitcmd.NewCommand("config"). | ||||||
| 			AddDynamicArguments(key, value). | 			AddDynamicArguments(key, value)) | ||||||
| 			RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) |  | ||||||
| 		return err | 		return err | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										62
									
								
								modules/gitrepo/diff.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								modules/gitrepo/diff.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | |||||||
|  | // Copyright 2025 The Gitea Authors. All rights reserved. | ||||||
|  | // SPDX-License-Identifier: MIT | ||||||
|  |  | ||||||
|  | package gitrepo | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  | 	"regexp" | ||||||
|  | 	"strconv" | ||||||
|  |  | ||||||
|  | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions | ||||||
|  | // TODO: it can be merged with another "GetDiffShortStat" in the future | ||||||
|  | func GetDiffShortStatByCmdArgs(ctx context.Context, repo Repository, trustedArgs gitcmd.TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) { | ||||||
|  | 	// Now if we call: | ||||||
|  | 	// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 | ||||||
|  | 	// we get: | ||||||
|  | 	// " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n" | ||||||
|  | 	cmd := gitcmd.NewCommand("diff", "--shortstat").AddArguments(trustedArgs...).AddDynamicArguments(dynamicArgs...) | ||||||
|  | 	stdout, err := runCmdString(ctx, repo, cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return 0, 0, 0, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return parseDiffStat(stdout) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var shortStatFormat = regexp.MustCompile( | ||||||
|  | 	`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`) | ||||||
|  |  | ||||||
|  | func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) { | ||||||
|  | 	if len(stdout) == 0 || stdout == "\n" { | ||||||
|  | 		return 0, 0, 0, nil | ||||||
|  | 	} | ||||||
|  | 	groups := shortStatFormat.FindStringSubmatch(stdout) | ||||||
|  | 	if len(groups) != 4 { | ||||||
|  | 		return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	numFiles, err = strconv.Atoi(groups[1]) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %w", stdout, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(groups[2]) != 0 { | ||||||
|  | 		totalAdditions, err = strconv.Atoi(groups[2]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %w", stdout, err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(groups[3]) != 0 { | ||||||
|  | 		totalDeletions, err = strconv.Atoi(groups[3]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %w", stdout, err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return numFiles, totalAdditions, totalDeletions, err | ||||||
|  | } | ||||||
| @@ -36,9 +36,7 @@ func GitRemoteAdd(ctx context.Context, repo Repository, remoteName, remoteURL st | |||||||
| 				return errors.New("unknown remote option: " + string(options[0])) | 				return errors.New("unknown remote option: " + string(options[0])) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		_, _, err := cmd. | 		_, err := runCmdString(ctx, repo, cmd.AddDynamicArguments(remoteName, remoteURL)) | ||||||
| 			AddDynamicArguments(remoteName, remoteURL). |  | ||||||
| 			RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) |  | ||||||
| 		return err | 		return err | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| @@ -46,7 +44,7 @@ func GitRemoteAdd(ctx context.Context, repo Repository, remoteName, remoteURL st | |||||||
| func GitRemoteRemove(ctx context.Context, repo Repository, remoteName string) error { | func GitRemoteRemove(ctx context.Context, repo Repository, remoteName string) error { | ||||||
| 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | 	return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error { | ||||||
| 		cmd := gitcmd.NewCommand("remote", "rm").AddDynamicArguments(remoteName) | 		cmd := gitcmd.NewCommand("remote", "rm").AddDynamicArguments(remoteName) | ||||||
| 		_, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)}) | 		_, err := runCmdString(ctx, repo, cmd) | ||||||
| 		return err | 		return err | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1620,7 +1620,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	diffShortStat, err := gitdiff.GetDiffShortStat(baseGitRepo, startCommitID, endCommitID) | 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, baseGitRepo, startCommitID, endCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.APIErrorInternal(err) | 		ctx.APIErrorInternal(err) | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	activities_model "code.gitea.io/gitea/models/activities" | 	activities_model "code.gitea.io/gitea/models/activities" | ||||||
| 	"code.gitea.io/gitea/models/git" | 	git_model "code.gitea.io/gitea/models/git" | ||||||
| 	"code.gitea.io/gitea/models/unit" | 	"code.gitea.io/gitea/models/unit" | ||||||
| 	"code.gitea.io/gitea/modules/templates" | 	"code.gitea.io/gitea/modules/templates" | ||||||
| 	"code.gitea.io/gitea/services/context" | 	"code.gitea.io/gitea/services/context" | ||||||
| @@ -56,7 +56,7 @@ func Activity(ctx *context.Context) { | |||||||
| 	canReadCode := ctx.Repo.CanRead(unit.TypeCode) | 	canReadCode := ctx.Repo.CanRead(unit.TypeCode) | ||||||
| 	if canReadCode { | 	if canReadCode { | ||||||
| 		// GetActivityStats needs to read the default branch to get some information | 		// GetActivityStats needs to read the default branch to get some information | ||||||
| 		branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch) | 		branchExist, _ := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch) | ||||||
| 		if !branchExist { | 		if !branchExist { | ||||||
| 			ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch) | 			ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch) | ||||||
| 			ctx.NotFound(nil) | 			ctx.NotFound(nil) | ||||||
|   | |||||||
| @@ -324,7 +324,7 @@ func Diff(ctx *context.Context) { | |||||||
| 		ctx.NotFound(err) | 		ctx.NotFound(err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	diffShortStat, err := gitdiff.GetDiffShortStat(gitRepo, "", commitID) | 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, gitRepo, "", commitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.ServerError("GetDiffShortStat", err) | 		ctx.ServerError("GetDiffShortStat", err) | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -631,7 +631,7 @@ func PrepareCompareDiff( | |||||||
| 		ctx.ServerError("GetDiff", err) | 		ctx.ServerError("GetDiff", err) | ||||||
| 		return false | 		return false | ||||||
| 	} | 	} | ||||||
| 	diffShortStat, err := gitdiff.GetDiffShortStat(ci.HeadGitRepo, beforeCommitID, headCommitID) | 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ci.HeadRepo, ci.HeadGitRepo, beforeCommitID, headCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.ServerError("GetDiffShortStat", err) | 		ctx.ServerError("GetDiffShortStat", err) | ||||||
| 		return false | 		return false | ||||||
|   | |||||||
| @@ -201,7 +201,7 @@ func GetPullDiffStats(ctx *context.Context) { | |||||||
| 		log.Error("Failed to GetRefCommitID: %v, repo: %v", err, ctx.Repo.Repository.FullName()) | 		log.Error("Failed to GetRefCommitID: %v, repo: %v", err, ctx.Repo.Repository.FullName()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, mergeBaseCommitID, headCommitID) | 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, mergeBaseCommitID, headCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Error("Failed to GetDiffShortStat: %v, repo: %v", err, ctx.Repo.Repository.FullName()) | 		log.Error("Failed to GetDiffShortStat: %v, repo: %v", err, ctx.Repo.Repository.FullName()) | ||||||
| 		return | 		return | ||||||
| @@ -761,7 +761,7 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, beforeCommitID, afterCommitID) | 	diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, beforeCommitID, afterCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.ServerError("GetDiffShortStat", err) | 		ctx.ServerError("GetDiffShortStat", err) | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -210,7 +210,7 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep | |||||||
|  |  | ||||||
| 	// Get diff stats for commit | 	// Get diff stats for commit | ||||||
| 	if opts.Stat { | 	if opts.Stat { | ||||||
| 		diffShortStat, err := gitdiff.GetDiffShortStat(gitRepo, "", commit.ID.String()) | 		diffShortStat, err := gitdiff.GetDiffShortStat(ctx, repo, gitRepo, "", commit.ID.String()) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -235,7 +235,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u | |||||||
| 		// Calculate diff | 		// Calculate diff | ||||||
| 		startCommitID = pr.MergeBase | 		startCommitID = pr.MergeBase | ||||||
|  |  | ||||||
| 		diffShortStats, err := gitdiff.GetDiffShortStat(gitRepo, startCommitID, endCommitID) | 		diffShortStats, err := gitdiff.GetDiffShortStat(ctx, pr.BaseRepo, gitRepo, startCommitID, endCommitID) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Error("GetDiffShortStat: %v", err) | 			log.Error("GetDiffShortStat: %v", err) | ||||||
| 		} else { | 		} else { | ||||||
|   | |||||||
| @@ -21,12 +21,14 @@ import ( | |||||||
| 	git_model "code.gitea.io/gitea/models/git" | 	git_model "code.gitea.io/gitea/models/git" | ||||||
| 	issues_model "code.gitea.io/gitea/models/issues" | 	issues_model "code.gitea.io/gitea/models/issues" | ||||||
| 	pull_model "code.gitea.io/gitea/models/pull" | 	pull_model "code.gitea.io/gitea/models/pull" | ||||||
|  | 	repo_model "code.gitea.io/gitea/models/repo" | ||||||
| 	user_model "code.gitea.io/gitea/models/user" | 	user_model "code.gitea.io/gitea/models/user" | ||||||
| 	"code.gitea.io/gitea/modules/analyze" | 	"code.gitea.io/gitea/modules/analyze" | ||||||
| 	"code.gitea.io/gitea/modules/charset" | 	"code.gitea.io/gitea/modules/charset" | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/git/attribute" | 	"code.gitea.io/gitea/modules/git/attribute" | ||||||
| 	"code.gitea.io/gitea/modules/git/gitcmd" | 	"code.gitea.io/gitea/modules/git/gitcmd" | ||||||
|  | 	"code.gitea.io/gitea/modules/gitrepo" | ||||||
| 	"code.gitea.io/gitea/modules/highlight" | 	"code.gitea.io/gitea/modules/highlight" | ||||||
| 	"code.gitea.io/gitea/modules/lfs" | 	"code.gitea.io/gitea/modules/lfs" | ||||||
| 	"code.gitea.io/gitea/modules/log" | 	"code.gitea.io/gitea/modules/log" | ||||||
| @@ -1271,9 +1273,7 @@ type DiffShortStat struct { | |||||||
| 	NumFiles, TotalAddition, TotalDeletion int | 	NumFiles, TotalAddition, TotalDeletion int | ||||||
| } | } | ||||||
|  |  | ||||||
| func GetDiffShortStat(gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) { | func GetDiffShortStat(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, beforeCommitID, afterCommitID string) (*DiffShortStat, error) { | ||||||
| 	repoPath := gitRepo.Path |  | ||||||
|  |  | ||||||
| 	afterCommit, err := gitRepo.GetCommit(afterCommitID) | 	afterCommit, err := gitRepo.GetCommit(afterCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @@ -1285,7 +1285,7 @@ func GetDiffShortStat(gitRepo *git.Repository, beforeCommitID, afterCommitID str | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	diff := &DiffShortStat{} | 	diff := &DiffShortStat{} | ||||||
| 	diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStatByCmdArgs(gitRepo.Ctx, repoPath, nil, actualBeforeCommitID.String(), afterCommitID) | 	diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = gitrepo.GetDiffShortStatByCmdArgs(ctx, repo, nil, actualBeforeCommitID.String(), afterCommitID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -339,7 +339,7 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest | |||||||
| 	} | 	} | ||||||
| 	go func() { | 	go func() { | ||||||
| 		// FIXME: graceful: We need to tell the manager we're doing something... | 		// FIXME: graceful: We need to tell the manager we're doing something... | ||||||
| 		err := InvalidateCodeComments(ctx, requests, doer, gitRepo, branch) | 		err := InvalidateCodeComments(ctx, requests, doer, repo, gitRepo, branch) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Error("PullRequestList.InvalidateCodeComments: %v", err) | 			log.Error("PullRequestList.InvalidateCodeComments: %v", err) | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -47,11 +47,25 @@ func (err ErrDismissRequestOnClosedPR) Unwrap() error { | |||||||
| // ErrSubmitReviewOnClosedPR represents an error when an user tries to submit an approve or reject review associated to a closed or merged PR. | // ErrSubmitReviewOnClosedPR represents an error when an user tries to submit an approve or reject review associated to a closed or merged PR. | ||||||
| var ErrSubmitReviewOnClosedPR = errors.New("can't submit review for a closed or merged PR") | var ErrSubmitReviewOnClosedPR = errors.New("can't submit review for a closed or merged PR") | ||||||
|  |  | ||||||
|  | // LineBlame returns the latest commit at the given line | ||||||
|  | func lineBlame(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, branch, file string, line uint) (*git.Commit, error) { | ||||||
|  | 	sha, err := gitrepo.LineBlame(ctx, repo, branch, file, line) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	if len(sha) < 40 { | ||||||
|  | 		return nil, fmt.Errorf("invalid result of blame: %s", sha) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName) | ||||||
|  | 	return gitRepo.GetCommit(sha[:objectFormat.FullLength()]) | ||||||
|  | } | ||||||
|  |  | ||||||
| // checkInvalidation checks if the line of code comment got changed by another commit. | // checkInvalidation checks if the line of code comment got changed by another commit. | ||||||
| // If the line got changed the comment is going to be invalidated. | // If the line got changed the comment is going to be invalidated. | ||||||
| func checkInvalidation(ctx context.Context, c *issues_model.Comment, repo *git.Repository, branch string) error { | func checkInvalidation(ctx context.Context, c *issues_model.Comment, repo *repo_model.Repository, gitRepo *git.Repository, branch string) error { | ||||||
| 	// FIXME differentiate between previous and proposed line | 	// FIXME differentiate between previous and proposed line | ||||||
| 	commit, err := repo.LineBlame(branch, repo.Path, c.TreePath, uint(c.UnsignedLine())) | 	commit, err := lineBlame(ctx, repo, gitRepo, branch, c.TreePath, uint(c.UnsignedLine())) | ||||||
| 	if err != nil && (strings.Contains(err.Error(), "fatal: no such path") || notEnoughLines.MatchString(err.Error())) { | 	if err != nil && (strings.Contains(err.Error(), "fatal: no such path") || notEnoughLines.MatchString(err.Error())) { | ||||||
| 		c.Invalidated = true | 		c.Invalidated = true | ||||||
| 		return issues_model.UpdateCommentInvalidate(ctx, c) | 		return issues_model.UpdateCommentInvalidate(ctx, c) | ||||||
| @@ -67,7 +81,7 @@ func checkInvalidation(ctx context.Context, c *issues_model.Comment, repo *git.R | |||||||
| } | } | ||||||
|  |  | ||||||
| // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change | // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change | ||||||
| func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestList, doer *user_model.User, repo *git.Repository, branch string) error { | func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestList, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branch string) error { | ||||||
| 	if len(prs) == 0 { | 	if len(prs) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| @@ -83,7 +97,7 @@ func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestLis | |||||||
| 		return fmt.Errorf("find code comments: %v", err) | 		return fmt.Errorf("find code comments: %v", err) | ||||||
| 	} | 	} | ||||||
| 	for _, comment := range codeComments { | 	for _, comment := range codeComments { | ||||||
| 		if err := checkInvalidation(ctx, comment, repo, branch); err != nil { | 		if err := checkInvalidation(ctx, comment, repo, gitRepo, branch); err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -233,7 +247,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo | |||||||
| 			// FIXME validate treePath | 			// FIXME validate treePath | ||||||
| 			// Get latest commit referencing the commented line | 			// Get latest commit referencing the commented line | ||||||
| 			// No need for get commit for base branch changes | 			// No need for get commit for base branch changes | ||||||
| 			commit, err := gitRepo.LineBlame(head, gitRepo.Path, treePath, uint(line)) | 			commit, err := lineBlame(ctx, pr.BaseRepo, gitRepo, head, treePath, uint(line)) | ||||||
| 			if err == nil { | 			if err == nil { | ||||||
| 				commitID = commit.ID.String() | 				commitID = commit.ID.String() | ||||||
| 			} else if !(strings.Contains(err.Error(), "exit status 128 - fatal: no such path") || notEnoughLines.MatchString(err.Error())) { | 			} else if !(strings.Contains(err.Error(), "exit status 128 - fatal: no such path") || notEnoughLines.MatchString(err.Error())) { | ||||||
|   | |||||||
| @@ -441,7 +441,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error { | 	if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error { | ||||||
| 		err2 := gitRepo.RenameBranch(from, to) | 		err2 := gitrepo.RenameBranch(ctx, repo, from, to) | ||||||
| 		if err2 != nil { | 		if err2 != nil { | ||||||
| 			return err2 | 			return err2 | ||||||
| 		} | 		} | ||||||
| @@ -552,9 +552,7 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R | |||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ | 		return gitrepo.DeleteBranch(ctx, repo, branchName, true) | ||||||
| 			Force: true, |  | ||||||
| 		}) |  | ||||||
| 	}); err != nil { | 	}); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -27,8 +27,8 @@ import ( | |||||||
| 	"code.gitea.io/gitea/modules/util" | 	"code.gitea.io/gitea/modules/util" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOptions, migrateTimeout time.Duration) (string, error) { | func cloneWiki(ctx context.Context, repo *repo_model.Repository, opts migration.MigrateOptions, migrateTimeout time.Duration) (string, error) { | ||||||
| 	wikiPath := repo_model.WikiPath(u.Name, opts.RepoName) | 	wikiPath := repo.WikiPath() | ||||||
| 	wikiRemotePath := repo_module.WikiRemoteURL(ctx, opts.CloneAddr) | 	wikiRemotePath := repo_module.WikiRemoteURL(ctx, opts.CloneAddr) | ||||||
| 	if wikiRemotePath == "" { | 	if wikiRemotePath == "" { | ||||||
| 		return "", nil | 		return "", nil | ||||||
| @@ -59,7 +59,7 @@ func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOp | |||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	defaultBranch, err := git.GetDefaultBranch(ctx, wikiPath) | 	defaultBranch, err := gitrepo.GetDefaultBranch(ctx, repo.WikiStorageRepo()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		cleanIncompleteWikiPath() | 		cleanIncompleteWikiPath() | ||||||
| 		return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err) | 		return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err) | ||||||
| @@ -73,7 +73,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, | |||||||
| 	repo *repo_model.Repository, opts migration.MigrateOptions, | 	repo *repo_model.Repository, opts migration.MigrateOptions, | ||||||
| 	httpTransport *http.Transport, | 	httpTransport *http.Transport, | ||||||
| ) (*repo_model.Repository, error) { | ) (*repo_model.Repository, error) { | ||||||
| 	repoPath := repo_model.RepoPath(u.Name, opts.RepoName) | 	repoPath := repo.RepoPath() | ||||||
|  |  | ||||||
| 	if u.IsOrganization() { | 	if u.IsOrganization() { | ||||||
| 		t, err := organization.OrgFromUser(u).GetOwnerTeam(ctx) | 		t, err := organization.OrgFromUser(u).GetOwnerTeam(ctx) | ||||||
| @@ -108,7 +108,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if opts.Wiki { | 	if opts.Wiki { | ||||||
| 		defaultWikiBranch, err := cloneWiki(ctx, u, opts, migrateTimeout) | 		defaultWikiBranch, err := cloneWiki(ctx, repo, opts, migrateTimeout) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return repo, fmt.Errorf("clone wiki error: %w", err) | 			return repo, fmt.Errorf("clone wiki error: %w", err) | ||||||
| 		} | 		} | ||||||
| @@ -137,7 +137,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, | |||||||
| 	if !repo.IsEmpty { | 	if !repo.IsEmpty { | ||||||
| 		if len(repo.DefaultBranch) == 0 { | 		if len(repo.DefaultBranch) == 0 { | ||||||
| 			// Try to get HEAD branch and set it as default branch. | 			// Try to get HEAD branch and set it as default branch. | ||||||
| 			headBranchName, err := git.GetDefaultBranch(ctx, repoPath) | 			headBranchName, err := gitrepo.GetDefaultBranch(ctx, repo) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				return repo, fmt.Errorf("GetHEADBranch: %w", err) | 				return repo, fmt.Errorf("GetHEADBranch: %w", err) | ||||||
| 			} | 			} | ||||||
|   | |||||||
| @@ -6,7 +6,6 @@ package wiki | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| @@ -22,7 +21,6 @@ import ( | |||||||
| 	"code.gitea.io/gitea/modules/graceful" | 	"code.gitea.io/gitea/modules/graceful" | ||||||
| 	"code.gitea.io/gitea/modules/log" | 	"code.gitea.io/gitea/modules/log" | ||||||
| 	repo_module "code.gitea.io/gitea/modules/repository" | 	repo_module "code.gitea.io/gitea/modules/repository" | ||||||
| 	"code.gitea.io/gitea/modules/util" |  | ||||||
| 	asymkey_service "code.gitea.io/gitea/services/asymkey" | 	asymkey_service "code.gitea.io/gitea/services/asymkey" | ||||||
| 	repo_service "code.gitea.io/gitea/services/repository" | 	repo_service "code.gitea.io/gitea/services/repository" | ||||||
| ) | ) | ||||||
| @@ -393,15 +391,7 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n | |||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		gitRepo, err := gitrepo.OpenRepository(ctx, repo.WikiStorageRepo()) | 		err = gitrepo.RenameBranch(ctx, repo.WikiStorageRepo(), oldDefBranch, newBranch) | ||||||
| 		if errors.Is(err, util.ErrNotExist) { |  | ||||||
| 			return nil // no git repo on storage, no need to do anything else |  | ||||||
| 		} else if err != nil { |  | ||||||
| 			return fmt.Errorf("unable to open repository: %w", err) |  | ||||||
| 		} |  | ||||||
| 		defer gitRepo.Close() |  | ||||||
|  |  | ||||||
| 		err = gitRepo.RenameBranch(oldDefBranch, newBranch) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("unable to rename default branch: %w", err) | 			return fmt.Errorf("unable to rename default branch: %w", err) | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user