Skip to content

Commit fabcc01

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Move some functions to gitrepo package to reduce RepoPath reference directly (go-gitea#36126) Make Golang correctly delete temp files during uploading (go-gitea#36128) [skip ci] Updated translations via Crowdin Support updating branch via API (go-gitea#35951) Use gitrepo's clone and push when possible (go-gitea#36093) Improve math rendering (go-gitea#36124) Add matching pair insertion to markdown textarea (go-gitea#36121) Changed a small typo in an error message and code comments. (go-gitea#36117)
2 parents c620fa7 + d2a372f commit fabcc01

54 files changed

Lines changed: 646 additions & 200 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

models/repo/repo.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -869,16 +869,6 @@ func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repos
869869
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
870870
}
871871

872-
// IsRepositoryModelOrDirExist returns true if the repository with given name under user has already existed.
873-
func IsRepositoryModelOrDirExist(ctx context.Context, u *user_model.User, repoName string) (bool, error) {
874-
has, err := IsRepositoryModelExist(ctx, u, repoName)
875-
if err != nil {
876-
return false, err
877-
}
878-
isDir, err := util.IsDir(RepoPath(u.Name, repoName))
879-
return has || isDir, err
880-
}
881-
882872
func IsRepositoryModelExist(ctx context.Context, u *user_model.User, repoName string) (bool, error) {
883873
return db.GetEngine(ctx).Get(&Repository{
884874
OwnerID: u.ID,

models/repo/update.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"time"
1010

1111
"code.gitea.io/gitea/models/db"
12-
user_model "code.gitea.io/gitea/models/user"
13-
"code.gitea.io/gitea/modules/log"
1412
"code.gitea.io/gitea/modules/util"
1513
)
1614

@@ -106,35 +104,6 @@ func (err ErrRepoFilesAlreadyExist) Unwrap() error {
106104
return util.ErrAlreadyExist
107105
}
108106

109-
// CheckCreateRepository check if doer could create a repository in new owner
110-
func CheckCreateRepository(ctx context.Context, doer, owner *user_model.User, name string, overwriteOrAdopt bool) error {
111-
if !doer.CanCreateRepoIn(owner) {
112-
return ErrReachLimitOfRepo{owner.MaxRepoCreation}
113-
}
114-
115-
if err := IsUsableRepoName(name); err != nil {
116-
return err
117-
}
118-
119-
has, err := IsRepositoryModelOrDirExist(ctx, owner, name)
120-
if err != nil {
121-
return fmt.Errorf("IsRepositoryExist: %w", err)
122-
} else if has {
123-
return ErrRepoAlreadyExist{owner.Name, name}
124-
}
125-
126-
repoPath := RepoPath(owner.Name, name)
127-
isExist, err := util.IsExist(repoPath)
128-
if err != nil {
129-
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
130-
return err
131-
}
132-
if !overwriteOrAdopt && isExist {
133-
return ErrRepoFilesAlreadyExist{owner.Name, name}
134-
}
135-
return nil
136-
}
137-
138107
// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
139108
func UpdateRepoSize(ctx context.Context, repoID, gitSize, lfsSize int64) error {
140109
_, err := db.GetEngine(ctx).ID(repoID).Cols("size", "git_size", "lfs_size").NoAutoTime().Update(&Repository{

modules/git/diff.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer
3232
return GetRepoRawDiffForFile(repo, "", commitID, diffType, "", writer)
3333
}
3434

35-
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.
36-
func GetReverseRawDiff(ctx context.Context, repoPath, commitID string, writer io.Writer) error {
37-
stderr := new(bytes.Buffer)
38-
if err := gitcmd.NewCommand("show", "--pretty=format:revert %H%n", "-R").
39-
AddDynamicArguments(commitID).
40-
WithDir(repoPath).
41-
WithStdout(writer).
42-
WithStderr(stderr).
43-
Run(ctx); err != nil {
44-
return fmt.Errorf("Run: %w - %s", err, stderr)
45-
}
46-
return nil
47-
}
48-
4935
// GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository
5036
func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
5137
commit, err := repo.GetCommit(endCommit)

modules/git/repo.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ type CloneRepoOptions struct {
123123
Depth int
124124
Filter string
125125
SkipTLSVerify bool
126+
SingleBranch bool
127+
Env []string
126128
}
127129

128130
// Clone clones original repository to target path.
@@ -157,6 +159,9 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
157159
if opts.Filter != "" {
158160
cmd.AddArguments("--filter").AddDynamicArguments(opts.Filter)
159161
}
162+
if opts.SingleBranch {
163+
cmd.AddArguments("--single-branch")
164+
}
160165
if len(opts.Branch) > 0 {
161166
cmd.AddArguments("-b").AddDynamicArguments(opts.Branch)
162167
}
@@ -167,13 +172,17 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
167172
}
168173

169174
envs := os.Environ()
170-
u, err := url.Parse(from)
171-
if err == nil {
172-
envs = proxy.EnvWithProxy(u)
175+
if opts.Env != nil {
176+
envs = opts.Env
177+
} else {
178+
u, err := url.Parse(from)
179+
if err == nil {
180+
envs = proxy.EnvWithProxy(u)
181+
}
173182
}
174183

175184
stderr := new(bytes.Buffer)
176-
if err = cmd.
185+
if err := cmd.
177186
WithTimeout(opts.Timeout).
178187
WithEnv(envs).
179188
WithStdout(io.Discard).
@@ -186,18 +195,21 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
186195

187196
// PushOptions options when push to remote
188197
type PushOptions struct {
189-
Remote string
190-
Branch string
191-
Force bool
192-
Mirror bool
193-
Env []string
194-
Timeout time.Duration
198+
Remote string
199+
Branch string
200+
Force bool
201+
ForceWithLease string
202+
Mirror bool
203+
Env []string
204+
Timeout time.Duration
195205
}
196206

197207
// Push pushs local commits to given remote branch.
198208
func Push(ctx context.Context, repoPath string, opts PushOptions) error {
199209
cmd := gitcmd.NewCommand("push")
200-
if opts.Force {
210+
if opts.ForceWithLease != "" {
211+
cmd.AddOptionFormat("--force-with-lease=%s", opts.ForceWithLease)
212+
} else if opts.Force {
201213
cmd.AddArguments("-f")
202214
}
203215
if opts.Mirror {
@@ -225,14 +237,3 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
225237

226238
return nil
227239
}
228-
229-
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
230-
func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error) {
231-
cmd := gitcmd.NewCommand("for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
232-
stdout, _, err := cmd.WithDir(repoPath).RunStdString(ctx)
233-
if err != nil {
234-
return time.Time{}, err
235-
}
236-
commitTime := strings.TrimSpace(stdout)
237-
return time.Parse("Mon Jan _2 15:04:05 2006 -0700", commitTime)
238-
}

modules/git/repo_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func TestGetLatestCommitTime(t *testing.T) {
14-
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
15-
lct, err := GetLatestCommitTime(t.Context(), bareRepo1Path)
16-
assert.NoError(t, err)
17-
// Time is Sun Nov 13 16:40:14 2022 +0100
18-
// which is the time of commit
19-
// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
20-
assert.EqualValues(t, 1668354014, lct.Unix())
21-
}
22-
2313
func TestRepoIsEmpty(t *testing.T) {
2414
emptyRepo2Path := filepath.Join(testReposDir, "repo2_empty")
2515
repo, err := OpenRepository(t.Context(), emptyRepo2Path)

modules/gitrepo/clone.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ func CloneExternalRepo(ctx context.Context, fromRemoteURL string, toRepo Reposit
1818
func CloneRepoToLocal(ctx context.Context, fromRepo Repository, toLocalPath string, opts git.CloneRepoOptions) error {
1919
return git.Clone(ctx, repoPath(fromRepo), toLocalPath, opts)
2020
}
21+
22+
func Clone(ctx context.Context, fromRepo, toRepo Repository, opts git.CloneRepoOptions) error {
23+
return git.Clone(ctx, repoPath(fromRepo), repoPath(toRepo), opts)
24+
}

modules/gitrepo/commit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"strconv"
99
"strings"
10+
"time"
1011

1112
"code.gitea.io/gitea/modules/git"
1213
"code.gitea.io/gitea/modules/git/gitcmd"
@@ -94,3 +95,18 @@ func AllCommitsCount(ctx context.Context, repo Repository, hidePRRefs bool, file
9495

9596
return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
9697
}
98+
99+
func GetFullCommitID(ctx context.Context, repo Repository, shortID string) (string, error) {
100+
return git.GetFullCommitID(ctx, repoPath(repo), shortID)
101+
}
102+
103+
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
104+
func GetLatestCommitTime(ctx context.Context, repo Repository) (time.Time, error) {
105+
stdout, err := RunCmdString(ctx, repo,
106+
gitcmd.NewCommand("for-each-ref", "--sort=-committerdate", git.BranchPrefix, "--count", "1", "--format=%(committerdate)"))
107+
if err != nil {
108+
return time.Time{}, err
109+
}
110+
commitTime := strings.TrimSpace(stdout)
111+
return time.Parse("Mon Jan _2 15:04:05 2006 -0700", commitTime)
112+
}

modules/gitrepo/commit_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ func TestCommitsCountWithoutBase(t *testing.T) {
3333
assert.NoError(t, err)
3434
assert.Equal(t, int64(2), commitsCount)
3535
}
36+
37+
func TestGetLatestCommitTime(t *testing.T) {
38+
bareRepo1 := &mockRepository{path: "repo1_bare"}
39+
lct, err := GetLatestCommitTime(t.Context(), bareRepo1)
40+
assert.NoError(t, err)
41+
// Time is Sun Nov 13 16:40:14 2022 +0100
42+
// which is the time of commit
43+
// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
44+
assert.EqualValues(t, 1668354014, lct.Unix())
45+
}

modules/gitrepo/diff.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package gitrepo
55

66
import (
7+
"bytes"
78
"context"
89
"fmt"
10+
"io"
911
"regexp"
1012
"strconv"
1113

@@ -60,3 +62,15 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
6062
}
6163
return numFiles, totalAdditions, totalDeletions, err
6264
}
65+
66+
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.
67+
func GetReverseRawDiff(ctx context.Context, repo Repository, commitID string, writer io.Writer) error {
68+
stderr := new(bytes.Buffer)
69+
if err := RunCmd(ctx, repo, gitcmd.NewCommand("show", "--pretty=format:revert %H%n", "-R").
70+
AddDynamicArguments(commitID).
71+
WithStdout(writer).
72+
WithStderr(stderr)); err != nil {
73+
return fmt.Errorf("GetReverseRawDiff: %w - %s", err, stderr)
74+
}
75+
return nil
76+
}

modules/gitrepo/gitrepo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,23 @@ func UpdateServerInfo(ctx context.Context, repo Repository) error {
9898
func GetRepoFS(repo Repository) fs.FS {
9999
return os.DirFS(repoPath(repo))
100100
}
101+
102+
func IsRepoFileExist(ctx context.Context, repo Repository, relativeFilePath string) (bool, error) {
103+
absoluteFilePath := filepath.Join(repoPath(repo), relativeFilePath)
104+
return util.IsExist(absoluteFilePath)
105+
}
106+
107+
func IsRepoDirExist(ctx context.Context, repo Repository, relativeDirPath string) (bool, error) {
108+
absoluteDirPath := filepath.Join(repoPath(repo), relativeDirPath)
109+
return util.IsDir(absoluteDirPath)
110+
}
111+
112+
func RemoveRepoFile(ctx context.Context, repo Repository, relativeFilePath string) error {
113+
absoluteFilePath := filepath.Join(repoPath(repo), relativeFilePath)
114+
return util.Remove(absoluteFilePath)
115+
}
116+
117+
func CreateRepoFile(ctx context.Context, repo Repository, relativeFilePath string) (io.WriteCloser, error) {
118+
absoluteFilePath := filepath.Join(repoPath(repo), relativeFilePath)
119+
return os.Create(absoluteFilePath)
120+
}

0 commit comments

Comments
 (0)