mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-07-18 17:19:41 +02:00
This change is very similar to what was done in forgejo/forgejo#7727. When a PR is updated, `checkIfPRContentChanged` is called to check if the diff changed - this is done on a temporary repository. This change improves this checking by doing this operation on the bare repository. The change is split into several commits. The following changes were made (in this exact order) 1. Update the `getTestPatchCtx` function that was introduced in forgejo/forgejo#7727 so it can be used outside the context of conflict checking. This is a simple change by making the caller determine if it can use a bare repository or not. 2. Do a small refactor of `ValidatePullRequest` to avoid indentation hell in this function, this is purely a refactor but necessary to not blow my brain while working on this function. 3. The first enhancement, introduce `testPatchCtx` in `ValidatePullRequest` to get diverging commits via the bare repository. 4. The main enhancement, do a refactor to move the function to be part of the repository struct and do a rename as this fits as a general purpose function. This refactoring includes it no longer being specific to a temporary repository and works on a bare repository. 5. Add extensive units tests, integration tests are added in forgejo/forgejo#8450 it checks that both calls to `CheckIfDiffDiffers` work. Because it also fixes a bug I sent it as a different PR. 6. Extend the integration test to check for diffs with commits from different repositories, to demonstrate that `getTestPatchCtx` works (specifically the `env` variable). Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8451 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/util"
|
|
)
|
|
|
|
// CheckIfDiffDiffers returns if the diff of the newCommitID and
|
|
// oldCommitID with the merge base of the base branch has changed.
|
|
//
|
|
// Informally it checks if the following two diffs are exactly the same in their
|
|
// contents, thus ignoring different commit IDs, headers and messages:
|
|
// 1. git diff --merge-base baseReference newCommitID
|
|
// 2. git diff --merge-base baseReference oldCommitID
|
|
func (repo *Repository) CheckIfDiffDiffers(base, oldCommitID, newCommitID string, env []string) (hasChanged bool, err error) {
|
|
cmd := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(newCommitID, oldCommitID, base)
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
|
if err != nil {
|
|
return false, fmt.Errorf("unable to open pipe for to run diff: %w", err)
|
|
}
|
|
|
|
stderr := new(bytes.Buffer)
|
|
if err := cmd.Run(&RunOpts{
|
|
Dir: repo.Path,
|
|
Stdout: stdoutWriter,
|
|
Stderr: stderr,
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
|
_ = stdoutWriter.Close()
|
|
defer func() {
|
|
_ = stdoutReader.Close()
|
|
}()
|
|
return util.IsEmptyReader(stdoutReader)
|
|
},
|
|
}); err != nil {
|
|
if err == util.ErrNotEmpty {
|
|
return true, nil
|
|
}
|
|
err = ConcatenateError(err, stderr.String())
|
|
|
|
log.Error("Unable to run git diff on %s %s %s in %q: Error: %v",
|
|
newCommitID, oldCommitID, base,
|
|
repo.Path,
|
|
err)
|
|
|
|
return false, fmt.Errorf("Unable to run git diff --name-only -z %s %s %s: %w", newCommitID, oldCommitID, base, err)
|
|
}
|
|
|
|
return false, nil
|
|
}
|