Skip to content

Commit d2a372f

Browse files
authored
Move some functions to gitrepo package to reduce RepoPath reference directly (#36126)
1 parent f25409f commit d2a372f

File tree

18 files changed

+121
-89
lines changed

18 files changed

+121
-89
lines changed

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: 13 additions & 15 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).
@@ -228,14 +237,3 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
228237

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

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+
}

routers/web/repo/editor_cherry_pick.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/gitrepo"
1213
"code.gitea.io/gitea/modules/util"
1314
"code.gitea.io/gitea/services/context"
1415
"code.gitea.io/gitea/services/forms"
@@ -66,7 +67,7 @@ func CherryPickPost(ctx *context.Context) {
6667
// Drop through to the "apply" method
6768
buf := &bytes.Buffer{}
6869
if parsed.form.Revert {
69-
err = git.GetReverseRawDiff(ctx, ctx.Repo.Repository.RepoPath(), fromCommitID, buf)
70+
err = gitrepo.GetReverseRawDiff(ctx, ctx.Repo.Repository, fromCommitID, buf)
7071
} else {
7172
err = git.GetRawDiff(ctx.Repo.GitRepo, fromCommitID, "patch", buf)
7273
}

routers/web/repo/issue_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func NewComment(ctx *context.Context) {
111111
ctx.ServerError("Unable to load base repo", err)
112112
return
113113
}
114-
prHeadCommitID, err := git.GetFullCommitID(ctx, pull.BaseRepo.RepoPath(), prHeadRef)
114+
prHeadCommitID, err := gitrepo.GetFullCommitID(ctx, pull.BaseRepo, prHeadRef)
115115
if err != nil {
116116
ctx.ServerError("Get head commit Id of pr fail", err)
117117
return
@@ -128,7 +128,7 @@ func NewComment(ctx *context.Context) {
128128
return
129129
}
130130
headBranchRef := git.RefNameFromBranch(pull.HeadBranch)
131-
headBranchCommitID, err := git.GetFullCommitID(ctx, pull.HeadRepo.RepoPath(), headBranchRef.String())
131+
headBranchCommitID, err := gitrepo.GetFullCommitID(ctx, pull.HeadRepo, headBranchRef.String())
132132
if err != nil {
133133
ctx.ServerError("Get head commit Id of head branch fail", err)
134134
return

0 commit comments

Comments
 (0)