mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-07 13:33:24 +09:00
refactor: GetDiffShortStat and fix panic caused by inconsistent "changed file number" (#39248)
This commit is contained in:
@@ -330,3 +330,11 @@ func GetAffectedFiles(ctx context.Context, repo *Repository, branchName, oldComm
|
||||
|
||||
return affectedFiles, err
|
||||
}
|
||||
|
||||
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.
|
||||
func GetReverseRawDiff(ctx context.Context, repo RepositoryFacade, commitID string, writer io.Writer) error {
|
||||
return gitcmd.NewCommand("show", "--pretty=format:revert %H%n", "-R").
|
||||
AddDynamicArguments(commitID).
|
||||
WithStdoutCopy(writer).
|
||||
WithRepo(repo).RunWithStderr(ctx)
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"gitea.dev/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 RepositoryFacade, 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.WithRepo(repo).RunStdString(ctx)
|
||||
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
|
||||
}
|
||||
|
||||
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.
|
||||
func GetReverseRawDiff(ctx context.Context, repo RepositoryFacade, commitID string, writer io.Writer) error {
|
||||
return gitcmd.NewCommand("show", "--pretty=format:revert %H%n", "-R").
|
||||
AddDynamicArguments(commitID).
|
||||
WithStdoutCopy(writer).
|
||||
WithRepo(repo).RunWithStderr(ctx)
|
||||
}
|
||||
@@ -35,7 +35,8 @@ type FastImportCommit struct {
|
||||
func ForceFastImportWithInit(ctx context.Context, repoLocalPath string, commits []FastImportCommit, initOpts ...FastImportInit) (RepositoryFacade, error) {
|
||||
repo := gitrepo.RepositoryUnmanaged(repoLocalPath)
|
||||
initOpt := util.OptionalArg(initOpts, FastImportInit{Bare: true})
|
||||
if exist, _ := IsRepositoryExist(ctx, repo); !exist {
|
||||
dirEntries, err := os.ReadDir(repoLocalPath)
|
||||
if os.IsNotExist(err) || (err == nil && len(dirEntries) == 0) {
|
||||
_ = os.MkdirAll(repoLocalPath, 0o755)
|
||||
err := InitRepositoryLocal(ctx, repoLocalPath, initOpt.Bare, util.IfZero(initOpt.ObjectFormat, "sha1"))
|
||||
if err != nil {
|
||||
|
||||
@@ -37,10 +37,8 @@ type Sha1ObjectFormatImpl struct{}
|
||||
|
||||
var (
|
||||
emptySha1ObjectID = &Sha1Hash{}
|
||||
emptySha1Tree = &Sha1Hash{
|
||||
0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
|
||||
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04,
|
||||
}
|
||||
// emptySha1Tree: 4b825dc642cb6eb9a060e54bf8d69288fbee4904
|
||||
emptySha1Tree = &Sha1Hash{0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60, 0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04}
|
||||
)
|
||||
|
||||
func (Sha1ObjectFormatImpl) Name() string { return "sha1" }
|
||||
|
||||
Reference in New Issue
Block a user