Skip to content

Commit

Permalink
fix(submodules): auto handle "commits not present" patch creation error
Browse files Browse the repository at this point in the history
When "commits not present" error occurs during patch creation between FROM and TO commits,
then reset service git worktree to the FROM commit (which in turn fetches lost submodule commit)
and retry patch creation procedure.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 24, 2022
1 parent 6837bda commit 91a829b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pkg/git_repo/base.go
Expand Up @@ -225,7 +225,31 @@ func (repo *Base) createPatch(ctx context.Context, repoPath, gitDir, repoID, wor

var desc *true_git.PatchDescriptor
if hasSubmodules {
var retryCount int

TryCreatePatch:
desc, err = true_git.PatchWithSubmodules(ctx, fileHandler, gitDir, workTreeCacheDir, true_git.PatchOptions(opts))

if true_git.IsCommitsNotPresentError(err) && retryCount == 0 {
logboek.Context(ctx).Default().LogF("Detected not present commits when creating patch: %s\n", err)
logboek.Context(ctx).Default().LogF("Will switch worktree to original commit %q and retry\n", opts.FromCommit)

if err := fileHandler.Truncate(0); err != nil {
return nil, fmt.Errorf("unable to truncate file %s: %w", tmpFile, err)
}
if _, err := fileHandler.Seek(0, 0); err != nil {
return nil, fmt.Errorf("unable to reset file %s: %w", tmpFile, err)
}

if err := true_git.WithWorkTree(ctx, gitDir, workTreeCacheDir, opts.FromCommit, true_git.WithWorkTreeOptions{HasSubmodules: true}, func(workTreeDir string) error {
return nil
}); err != nil {
return nil, fmt.Errorf("unable to switch worktree to commit %q: %w", opts.FromCommit, err)
}

retryCount++
goto TryCreatePatch
}
} else {
desc, err = true_git.Patch(ctx, fileHandler, gitDir, true_git.PatchOptions(opts))
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/true_git/diff_parser.go
Expand Up @@ -2,6 +2,7 @@ package true_git

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -13,6 +14,12 @@ import (
"github.com/werf/werf/pkg/util"
)

var ErrCommitsNotPresent = errors.New("commits not present")

func IsCommitsNotPresentError(err error) bool {
return err != nil && strings.HasSuffix(err.Error(), ErrCommitsNotPresent.Error())
}

func makeDiffParser(out io.Writer, pathScope string, pathMatcher path_matcher.PathMatcher, fileRenames map[string]string) *diffParser {
return &diffParser{
PathScope: pathScope,
Expand Down Expand Up @@ -379,7 +386,7 @@ func (p *diffParser) handleModifyFilePathB(line string) error {
func (p *diffParser) handleSubmoduleLine(line string) error {
p.state = unrecognized
if strings.HasSuffix(line, " (commits not present)") {
return fmt.Errorf("cannot handle \"commits not present\" in git diff line %q, check specified submodule commits are correct", line)
return fmt.Errorf("cannot handle git diff line %q, check specified commits are correct: %w", line, ErrCommitsNotPresent)
}
return nil
}
Expand Down

0 comments on commit 91a829b

Please sign in to comment.