Skip to content

Commit 81314a8

Browse files
committed
fix(stapel): fix git apply patch error handling
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com>
1 parent 0d13d5f commit 81314a8

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

pkg/build/stage/git_mapping.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ func (gm *GitMapping) applyPatchCommand(patchFile *ContainerFileDescriptor, arch
231231
))
232232

233233
gitCommand := fmt.Sprintf(
234-
"%s %s apply --ignore-whitespace --whitespace=nowarn --directory=\"%s\" --unsafe-paths %s",
234+
"%s %s apply --ignore-whitespace --whitespace=nowarn --directory=\"%s\" --unsafe-paths %s %s",
235235
stapel.OptionalSudoCommand(gm.Owner, gm.Group),
236236
stapel.GitBinPath(),
237237
applyPatchDirectory,
238238
patchFile.ContainerFilePath,
239+
fmt.Sprintf("|| exit %d", container_backend.ErrPatchApplyCode),
239240
)
240241

241242
commands = append(commands, strings.TrimLeft(gitCommand, " "))

pkg/container_backend/errors.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
"github.com/containers/storage/types"
7+
"github.com/docker/cli/cli"
78
)
89

910
var (
@@ -13,3 +14,34 @@ var (
1314
ErrImageUsedByContainer = types.ErrImageUsedByContainer
1415
ErrPruneIsAlreadyRunning = errors.New("a prune operation is already running")
1516
)
17+
18+
var (
19+
ErrPatchApply = errors.New(`werf cannot apply the patch to the current source code because the files being added were modified by user commands in earlier stages.
20+
21+
- If these files should NOT be changed, update the instructions for the preceding stages with user commands.
22+
23+
- If these files SHOULD be changed, declare this dependency using the stageDependencies directive, and these files will be updated before running user commands.
24+
25+
- If these files are NOT required, exclude them using the git.[*].includePaths / excludePaths directives.`)
26+
)
27+
28+
const (
29+
ErrPatchApplyCode = 42
30+
)
31+
32+
var errByCode = map[int]error{
33+
ErrPatchApplyCode: ErrPatchApply,
34+
}
35+
36+
func CliErrorByCode(err error) error {
37+
if err == nil {
38+
return nil
39+
}
40+
var statusError cli.StatusError
41+
if errors.As(err, &statusError) {
42+
if e, ok := errByCode[statusError.StatusCode]; ok {
43+
return e
44+
}
45+
}
46+
return err
47+
}

pkg/container_backend/legacy_stage_image_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (c *LegacyStageImageContainer) run(ctx context.Context) error {
288288
err = docker.CliRun_LiveOutput(ctx, runArgs...)
289289
UnregisterRunningContainer(c.name)
290290
if err != nil {
291-
return fmt.Errorf("container run failed: %w", err)
291+
return fmt.Errorf("container run failed: %w", CliErrorByCode(err))
292292
}
293293
return nil
294294
}

pkg/stapel/stapel.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package stapel
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strconv"
@@ -226,5 +225,5 @@ func CreateScript(path string, commands []string) error {
226225
scriptLines = append(scriptLines, commands...)
227226
scriptData := []byte(strings.Join(scriptLines, "\n") + "\n")
228227

229-
return ioutil.WriteFile(path, scriptData, os.FileMode(0o667))
228+
return os.WriteFile(path, scriptData, os.FileMode(0o667))
230229
}

0 commit comments

Comments
 (0)