mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package gitrepo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/hanzoai/git/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 Repository, 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 := RunCmdString(ctx, repo, cmd)
|
|
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 Repository, commitID string, writer io.Writer) error {
|
|
return RunCmdWithStderr(ctx, repo, gitcmd.NewCommand("show", "--pretty=format:revert %H%n", "-R").
|
|
AddDynamicArguments(commitID).
|
|
WithStdoutCopy(writer),
|
|
)
|
|
}
|