mirror of
https://github.com/go-gitea/gitea
synced 2024-12-22 04:07:50 +01:00
Refactor getpatch/getdiff functions and remove fallback automatically
This commit is contained in:
parent
0b8a8941a0
commit
0749bb5484
@ -233,72 +233,63 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
|
|||||||
return numFiles, totalAdditions, totalDeletions, err
|
return numFiles, totalAdditions, totalDeletions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiffOrPatch generates either diff or formatted patch data between given revisions
|
func parseCompareArgs(compareArgs string) (args []string) {
|
||||||
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, binary bool) error {
|
parts := strings.Split(compareArgs, "...")
|
||||||
if patch {
|
if len(parts) == 2 {
|
||||||
return repo.GetPatch(base, head, w)
|
return []string{compareArgs}
|
||||||
}
|
}
|
||||||
if binary {
|
parts = strings.Split(compareArgs, "..")
|
||||||
return repo.GetDiffBinary(base, head, w)
|
if len(parts) == 2 {
|
||||||
|
return parts
|
||||||
}
|
}
|
||||||
return repo.GetDiff(base, head, w)
|
parts = strings.Fields(compareArgs)
|
||||||
|
if len(parts) == 2 {
|
||||||
|
return parts
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiff generates and returns patch data between given revisions, optimized for human readability
|
// GetDiff generates and returns patch data between given revisions, optimized for human readability
|
||||||
func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
|
func (repo *Repository) GetDiff(compareArgs string, w io.Writer) error {
|
||||||
|
args := parseCompareArgs(compareArgs)
|
||||||
|
if len(args) == 0 {
|
||||||
|
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
|
||||||
|
}
|
||||||
stderr := new(bytes.Buffer)
|
stderr := new(bytes.Buffer)
|
||||||
err := NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base + "..." + head).
|
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(args...).
|
||||||
Run(&RunOpts{
|
Run(&RunOpts{
|
||||||
Dir: repo.Path,
|
Dir: repo.Path,
|
||||||
Stdout: w,
|
Stdout: w,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
})
|
})
|
||||||
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
|
|
||||||
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base, head).
|
|
||||||
Run(&RunOpts{
|
|
||||||
Dir: repo.Path,
|
|
||||||
Stdout: w,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
|
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
|
||||||
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
|
func (repo *Repository) GetDiffBinary(compareArgs string, w io.Writer) error {
|
||||||
stderr := new(bytes.Buffer)
|
args := parseCompareArgs(compareArgs)
|
||||||
err := NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base + "..." + head).
|
if len(args) == 0 {
|
||||||
Run(&RunOpts{
|
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
|
||||||
Dir: repo.Path,
|
|
||||||
Stdout: w,
|
|
||||||
Stderr: stderr,
|
|
||||||
})
|
|
||||||
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
|
|
||||||
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base, head).
|
|
||||||
Run(&RunOpts{
|
|
||||||
Dir: repo.Path,
|
|
||||||
Stdout: w,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return err
|
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(args...).Run(&RunOpts{
|
||||||
|
Dir: repo.Path,
|
||||||
|
Stdout: w,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
|
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
|
||||||
func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
|
func (repo *Repository) GetPatch(compareArgs string, w io.Writer) error {
|
||||||
|
args := parseCompareArgs(compareArgs)
|
||||||
|
if len(args) == 0 {
|
||||||
|
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
|
||||||
|
}
|
||||||
stderr := new(bytes.Buffer)
|
stderr := new(bytes.Buffer)
|
||||||
err := NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base + "..." + head).
|
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(args...).
|
||||||
Run(&RunOpts{
|
Run(&RunOpts{
|
||||||
Dir: repo.Path,
|
Dir: repo.Path,
|
||||||
Stdout: w,
|
Stdout: w,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
})
|
})
|
||||||
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
|
|
||||||
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base, head).
|
|
||||||
Run(&RunOpts{
|
|
||||||
Dir: repo.Path,
|
|
||||||
Stdout: w,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
|
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
|
||||||
@ -329,21 +320,6 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err
|
|||||||
return split, err
|
return split, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiffFromMergeBase generates and return patch data from merge base to head
|
|
||||||
func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error {
|
|
||||||
stderr := new(bytes.Buffer)
|
|
||||||
err := NewCommand(repo.Ctx, "diff", "-p", "--binary").AddDynamicArguments(base + "..." + head).
|
|
||||||
Run(&RunOpts{
|
|
||||||
Dir: repo.Path,
|
|
||||||
Stdout: w,
|
|
||||||
Stderr: stderr,
|
|
||||||
})
|
|
||||||
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
|
|
||||||
return repo.GetDiffBinary(base, head, w)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadPatchCommit will check if a diff patch exists and return stats
|
// ReadPatchCommit will check if a diff patch exists and return stats
|
||||||
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
|
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
|
||||||
// Migrated repositories download patches to "pulls" location
|
// Migrated repositories download patches to "pulls" location
|
||||||
|
@ -12,6 +12,31 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_parseCompareArgs(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
compareString string
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"master..develop",
|
||||||
|
[]string{"master", "develop"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"master HEAD",
|
||||||
|
[]string{"master", "HEAD"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"HEAD...develop",
|
||||||
|
[]string{"HEAD...develop"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
args := parseCompareArgs(tc.compareString)
|
||||||
|
assert.Equal(t, tc.expected, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetFormatPatch(t *testing.T) {
|
func TestGetFormatPatch(t *testing.T) {
|
||||||
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
||||||
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
||||||
@ -28,7 +53,7 @@ func TestGetFormatPatch(t *testing.T) {
|
|||||||
defer repo.Close()
|
defer repo.Close()
|
||||||
|
|
||||||
rd := &bytes.Buffer{}
|
rd := &bytes.Buffer{}
|
||||||
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
|
err = repo.GetPatch("8d92fc95^...8d92fc95", rd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
return
|
return
|
||||||
|
@ -42,9 +42,19 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io
|
|||||||
}
|
}
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
|
|
||||||
if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch, binary); err != nil {
|
compareString := pr.MergeBase + "..." + pr.GetGitRefName()
|
||||||
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
switch {
|
||||||
return fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
case patch:
|
||||||
|
err = gitRepo.GetPatch(compareString, w)
|
||||||
|
case binary:
|
||||||
|
err = gitRepo.GetDiffBinary(compareString, w)
|
||||||
|
default:
|
||||||
|
err = gitRepo.GetDiff(compareString, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error("unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||||
|
return fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -355,7 +365,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo *
|
|||||||
_ = util.Remove(tmpPatchFile.Name())
|
_ = util.Remove(tmpPatchFile.Name())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := gitRepo.GetDiffBinary(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
|
if err := gitRepo.GetDiffBinary(pr.MergeBase+"...tracking", tmpPatchFile); err != nil {
|
||||||
tmpPatchFile.Close()
|
tmpPatchFile.Close()
|
||||||
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||||
return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||||
|
Loading…
Reference in New Issue
Block a user