Skip to content

Move duplicated functions #33977

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

Merged
merged 4 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
11 changes: 6 additions & 5 deletions modules/git/batch_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"

"github.com/djherbis/buffer"
"github.com/djherbis/nio/v3"
Expand All @@ -35,7 +36,7 @@ func ensureValidGitRepository(ctx context.Context, repoPath string) error {
Stderr: &stderr,
})
if err != nil {
return ConcatenateError(err, (&stderr).String())
return util.ConcatenateError(err, (&stderr).String())
}
return nil
}
Expand Down Expand Up @@ -71,8 +72,8 @@ func catFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
UseContextTimeout: true,
})
if err != nil {
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
} else {
_ = batchStdoutWriter.Close()
_ = batchStdinReader.Close()
Expand Down Expand Up @@ -119,8 +120,8 @@ func catFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
UseContextTimeout: true,
})
if err != nil {
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
} else {
_ = batchStdoutWriter.Close()
_ = batchStdinReader.Close()
Expand Down
2 changes: 1 addition & 1 deletion modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ type runStdError struct {
func (r *runStdError) Error() string {
// the stderr must be in the returned error text, some code only checks `strings.Contains(err.Error(), "git error")`
if r.errMsg == "" {
r.errMsg = ConcatenateError(r.err, r.stderr).Error()
r.errMsg = util.ConcatenateError(r.err, r.stderr).Error()
}
return r.errMsg
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
})
w.Close() // Close writer to exit parsing goroutine
if err != nil {
return nil, ConcatenateError(err, stderr.String())
return nil, util.ConcatenateError(err, stderr.String())
}

<-done
Expand Down
30 changes: 24 additions & 6 deletions modules/git/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,28 @@ func GetHook(repoPath, name string) (*Hook, error) {
}
h := &Hook{
name: name,
path: path.Join(repoPath, "hooks", name+".d", name),
path: filepath.Join(repoPath, "hooks", name+".d", name),
}
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
if isFile(h.path) {
isFile, err := util.IsFile(h.path)
if err != nil {
return nil, err
}
if isFile {
data, err := os.ReadFile(h.path)
if err != nil {
return nil, err
}
h.IsActive = true
h.Content = string(data)
} else if isFile(samplePath) {
return h, nil
}

samplePath := filepath.Join(repoPath, "hooks", name+".sample")
isFile, err = util.IsFile(samplePath)
if err != nil {
return nil, err
}
if isFile {
data, err := os.ReadFile(samplePath)
if err != nil {
return nil, err
Expand All @@ -79,7 +90,11 @@ func (h *Hook) Name() string {
// Update updates hook settings.
func (h *Hook) Update() error {
if len(strings.TrimSpace(h.Content)) == 0 {
if isExist(h.path) {
exist, err := util.IsExist(h.path)
if err != nil {
return err
}
if exist {
err := util.Remove(h.path)
if err != nil {
return err
Expand All @@ -103,7 +118,10 @@ func (h *Hook) Update() error {

// ListHooks returns a list of Git hooks of given repository.
func ListHooks(repoPath string) (_ []*Hook, err error) {
if !isDir(path.Join(repoPath, "hooks")) {
exist, err := util.IsDir(filepath.Join(repoPath, "hooks"))
if err != nil {
return nil, err
} else if !exist {
return nil, errors.New("hooks path does not exist")
}

Expand Down
3 changes: 2 additions & 1 deletion modules/git/log_name_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/util"

"github.com/djherbis/buffer"
"github.com/djherbis/nio/v3"
Expand Down Expand Up @@ -70,7 +71,7 @@ func LogNameStatusRepo(ctx context.Context, repository, head, treepath string, p
Stderr: &stderr,
})
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
return
}

Expand Down
3 changes: 2 additions & 1 deletion modules/git/pipeline/lfs_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
)

// FindLFSFile finds commits that contain a provided pointer file hash
Expand All @@ -38,7 +39,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
Stderr: &stderr,
})
if err != nil {
_ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String()))
_ = revListWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
} else {
_ = revListWriter.Close()
}
Expand Down
3 changes: 2 additions & 1 deletion modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/util"
)

// GPGSettings represents the default GPG settings for this repository
Expand Down Expand Up @@ -176,7 +177,7 @@ func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, op
Stdout: io.Discard,
Stderr: stderr,
}); err != nil {
return ConcatenateError(err, stderr.String())
return util.ConcatenateError(err, stderr.String())
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion modules/git/repo_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io"
"path/filepath"
"strings"

"code.gitea.io/gitea/modules/util"
)

// ArchiveType archive types
Expand Down Expand Up @@ -67,7 +69,7 @@ func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, t
Stderr: &stderr,
})
if err != nil {
return ConcatenateError(err, stderr.String())
return util.ConcatenateError(err, stderr.String())
}
return nil
}
7 changes: 6 additions & 1 deletion modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
} else if !isDir(repoPath) {
}
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
}
if !exist {
return nil, util.NewNotExistErrorf("no such file or directory")
}

Expand Down
7 changes: 6 additions & 1 deletion modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
} else if !isDir(repoPath) {
}
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
}
if !exist {
return nil, util.NewNotExistErrorf("no such file or directory")
}

Expand Down
3 changes: 2 additions & 1 deletion modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

// IsObjectExist returns true if the given object exists in the repository.
Expand Down Expand Up @@ -119,7 +120,7 @@ func WalkShowRef(ctx context.Context, repoPath string, extraArgs TrustedCmdArgs,
_ = stdoutWriter.Close()
return
}
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, stderrBuilder.String()))
} else {
_ = stdoutWriter.Close()
}
Expand Down
3 changes: 2 additions & 1 deletion modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

// GetBranchCommitID returns last commit ID string of given branch.
Expand Down Expand Up @@ -239,7 +240,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
Stderr: &stderr,
})
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, (&stderr).String()))
} else {
_ = stdoutWriter.Close()
}
Expand Down
4 changes: 3 additions & 1 deletion modules/git/repo_ref_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"bufio"
"io"
"strings"

"code.gitea.io/gitea/modules/util"
)

// GetRefsFiltered returns all references of the repository that matches patterm exactly or starting with.
Expand All @@ -27,7 +29,7 @@ func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
Stderr: stderrBuilder,
})
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, stderrBuilder.String()))
} else {
_ = stdoutWriter.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) {
AddOptionFormat("--format=%s", forEachRefFmt.Flag()).
AddArguments("--sort", "-*creatordate", "refs/tags").Run(repo.Ctx, rc)
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderr.String()))
_ = stdoutWriter.CloseWithError(util.ConcatenateError(err, stderr.String()))
} else {
_ = stdoutWriter.Close()
}
Expand Down
4 changes: 3 additions & 1 deletion modules/git/repo_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strings"
"time"

"code.gitea.io/gitea/modules/util"
)

// CommitTreeOpts represents the possible options to CommitTree
Expand Down Expand Up @@ -61,7 +63,7 @@ func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opt
Stderr: stderr,
})
if err != nil {
return nil, ConcatenateError(err, stderr.String())
return nil, util.ConcatenateError(err, stderr.String())
}
return NewIDFromString(strings.TrimSpace(stdout.String()))
}
37 changes: 0 additions & 37 deletions modules/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package git
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -41,41 +39,6 @@ func (oc *ObjectCache[T]) Get(id string) (T, bool) {
return obj, has
}

// isDir returns true if given path is a directory,
// or returns false when it's a file or does not exist.
func isDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}

// isFile returns true if given path is a file,
// or returns false when it's a directory or does not exist.
func isFile(filePath string) bool {
f, e := os.Stat(filePath)
if e != nil {
return false
}
return !f.IsDir()
}

// isExist checks whether a file or directory exists.
// It returns false when the file or directory does not exist.
func isExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}

// ConcatenateError concatenats an error with stderr string
func ConcatenateError(err error, stderr string) error {
if len(stderr) == 0 {
return err
}
return fmt.Errorf("%w - %s", err, stderr)
}

// ParseBool returns the boolean value represented by the string as per git's git_config_bool
// true will be returned for the result if the string is empty, but valid will be false.
// "true", "yes", "on" are all true, true
Expand Down
8 changes: 8 additions & 0 deletions modules/util/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ func ErrorAsLocale(err error) *LocaleWrapper {
}
return nil
}

// ConcatenateError concatenats an error with stderr string
func ConcatenateError(err error, stderr string) error {
if len(stderr) == 0 {
return err
}
return fmt.Errorf("%w - %s", err, stderr)
}
3 changes: 2 additions & 1 deletion services/pull/patch_unmerged.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

// lsFileLine is a Quadruplet struct (+error) representing a partially parsed line from ls-files
Expand Down Expand Up @@ -116,7 +117,7 @@ func readUnmergedLsFileLines(ctx context.Context, tmpBasePath string, outputChan
},
})
if err != nil {
outputChan <- &lsFileLine{err: fmt.Errorf("git ls-files -u -z: %w", git.ConcatenateError(err, stderr.String()))}
outputChan <- &lsFileLine{err: fmt.Errorf("git ls-files -u -z: %w", util.ConcatenateError(err, stderr.String()))}
}
}

Expand Down
2 changes: 1 addition & 1 deletion services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func checkIfPRContentChanged(ctx context.Context, pr *issues_model.PullRequest,
if err == util.ErrNotEmpty {
return true, nil
}
err = git.ConcatenateError(err, stderr.String())
err = util.ConcatenateError(err, stderr.String())

log.Error("Unable to run diff on %s %s %s in tempRepo for PR[%d]%s/%s...%s/%s: Error: %v",
newCommitID, oldCommitID, base,
Expand Down