Skip to content

Commit ce0ec3b

Browse files
committed
Move duplicated functions
1 parent e1bba9c commit ce0ec3b

19 files changed

+75
-63
lines changed

modules/git/batch_reader.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/util"
1617

1718
"github.com/djherbis/buffer"
1819
"github.com/djherbis/nio/v3"
@@ -35,7 +36,7 @@ func ensureValidGitRepository(ctx context.Context, repoPath string) error {
3536
Stderr: &stderr,
3637
})
3738
if err != nil {
38-
return ConcatenateError(err, (&stderr).String())
39+
return util.ConcatenateError(err, (&stderr).String())
3940
}
4041
return nil
4142
}
@@ -71,8 +72,8 @@ func catFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
7172
UseContextTimeout: true,
7273
})
7374
if err != nil {
74-
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
75-
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
75+
_ = batchStdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
76+
_ = batchStdinReader.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
7677
} else {
7778
_ = batchStdoutWriter.Close()
7879
_ = batchStdinReader.Close()
@@ -119,8 +120,8 @@ func catFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
119120
UseContextTimeout: true,
120121
})
121122
if err != nil {
122-
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
123-
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
123+
_ = batchStdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
124+
_ = batchStdinReader.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
124125
} else {
125126
_ = batchStdoutWriter.Close()
126127
_ = batchStdinReader.Close()

modules/git/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ type runStdError struct {
380380
func (r *runStdError) Error() string {
381381
// the stderr must be in the returned error text, some code only checks `strings.Contains(err.Error(), "git error")`
382382
if r.errMsg == "" {
383-
r.errMsg = ConcatenateError(r.err, r.stderr).Error()
383+
r.errMsg = util.ConcatenateError(r.err, r.stderr).Error()
384384
}
385385
return r.errMsg
386386
}

modules/git/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
448448
})
449449
w.Close() // Close writer to exit parsing goroutine
450450
if err != nil {
451-
return nil, ConcatenateError(err, stderr.String())
451+
return nil, util.ConcatenateError(err, stderr.String())
452452
}
453453

454454
<-done

modules/git/hook.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,28 @@ func GetHook(repoPath, name string) (*Hook, error) {
5151
}
5252
h := &Hook{
5353
name: name,
54-
path: path.Join(repoPath, "hooks", name+".d", name),
54+
path: filepath.Join(repoPath, "hooks", name+".d", name),
5555
}
56-
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
57-
if isFile(h.path) {
56+
isFile, err := util.IsFile(h.path)
57+
if err != nil {
58+
return nil, err
59+
}
60+
if isFile {
5861
data, err := os.ReadFile(h.path)
5962
if err != nil {
6063
return nil, err
6164
}
6265
h.IsActive = true
6366
h.Content = string(data)
64-
} else if isFile(samplePath) {
67+
return h, nil
68+
}
69+
70+
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
71+
isFile, err = util.IsFile(samplePath)
72+
if err != nil {
73+
return nil, err
74+
}
75+
if isFile {
6576
data, err := os.ReadFile(samplePath)
6677
if err != nil {
6778
return nil, err
@@ -79,7 +90,11 @@ func (h *Hook) Name() string {
7990
// Update updates hook settings.
8091
func (h *Hook) Update() error {
8192
if len(strings.TrimSpace(h.Content)) == 0 {
82-
if isExist(h.path) {
93+
exist, err := util.IsExist(h.path)
94+
if err != nil {
95+
return err
96+
}
97+
if exist {
8398
err := util.Remove(h.path)
8499
if err != nil {
85100
return err
@@ -103,7 +118,10 @@ func (h *Hook) Update() error {
103118

104119
// ListHooks returns a list of Git hooks of given repository.
105120
func ListHooks(repoPath string) (_ []*Hook, err error) {
106-
if !isDir(path.Join(repoPath, "hooks")) {
121+
exist, err := util.IsDir(filepath.Join(repoPath, "hooks"))
122+
if err != nil {
123+
return nil, err
124+
} else if !exist {
107125
return nil, errors.New("hooks path does not exist")
108126
}
109127

modules/git/log_name_status.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"code.gitea.io/gitea/modules/container"
17+
"code.gitea.io/gitea/modules/util"
1718

1819
"github.com/djherbis/buffer"
1920
"github.com/djherbis/nio/v3"
@@ -70,7 +71,7 @@ func LogNameStatusRepo(ctx context.Context, repository, head, treepath string, p
7071
Stderr: &stderr,
7172
})
7273
if err != nil {
73-
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
74+
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
7475
return
7576
}
7677

modules/git/pipeline/lfs_nogogit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sync"
1515

1616
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/util"
1718
)
1819

1920
// FindLFSFile finds commits that contain a provided pointer file hash
@@ -38,7 +39,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
3839
Stderr: &stderr,
3940
})
4041
if err != nil {
41-
_ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String()))
42+
_ = revListWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
4243
} else {
4344
_ = revListWriter.Close()
4445
}

modules/git/repo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"code.gitea.io/gitea/modules/proxy"
21+
"code.gitea.io/gitea/modules/util"
2122
)
2223

2324
// GPGSettings represents the default GPG settings for this repository
@@ -176,7 +177,7 @@ func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, op
176177
Stdout: io.Discard,
177178
Stderr: stderr,
178179
}); err != nil {
179-
return ConcatenateError(err, stderr.String())
180+
return util.ConcatenateError(err, stderr.String())
180181
}
181182
return nil
182183
}

modules/git/repo_archive.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"io"
1111
"path/filepath"
1212
"strings"
13+
14+
"code.gitea.io/gitea/modules/util"
1315
)
1416

1517
// ArchiveType archive types
@@ -67,7 +69,7 @@ func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, t
6769
Stderr: &stderr,
6870
})
6971
if err != nil {
70-
return ConcatenateError(err, stderr.String())
72+
return util.ConcatenateError(err, stderr.String())
7173
}
7274
return nil
7375
}

modules/git/repo_base_gogit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4949
repoPath, err := filepath.Abs(repoPath)
5050
if err != nil {
5151
return nil, err
52-
} else if !isDir(repoPath) {
52+
}
53+
exist, err := util.IsDir(repoPath)
54+
if err != nil {
55+
return nil, err
56+
}
57+
if !exist {
5358
return nil, util.NewNotExistErrorf("no such file or directory")
5459
}
5560

modules/git/repo_base_nogogit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4747
repoPath, err := filepath.Abs(repoPath)
4848
if err != nil {
4949
return nil, err
50-
} else if !isDir(repoPath) {
50+
}
51+
exist, err := util.IsDir(repoPath)
52+
if err != nil {
53+
return nil, err
54+
}
55+
if !exist {
5156
return nil, util.NewNotExistErrorf("no such file or directory")
5257
}
5358

0 commit comments

Comments
 (0)