Skip to content

Commit

Permalink
fix(repositories): clean repo basedir if any operation occurs in push…
Browse files Browse the repository at this point in the history
… operation (#5490)
  • Loading branch information
fsamin authored Oct 14, 2020
1 parent 308cb0d commit 107a814
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions engine/repositories/fs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package repositories

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
)

func (s *Service) checkOrCreateRootFS() error {
Expand Down Expand Up @@ -34,3 +36,8 @@ func (s *Service) checkOrCreateFS(r *sdk.OperationRepo) error {
r.Basedir = path
return nil
}

func (s *Service) cleanFS(ctx context.Context, r *sdk.OperationRepo) error {
log.Info(ctx, "cleaning operation basedir: %v", r.Basedir)
return sdk.WithStack(os.RemoveAll(r.Basedir))
}
12 changes: 11 additions & 1 deletion engine/repositories/processor_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ovh/cds/sdk/log"
)

func (s *Service) processPush(ctx context.Context, op *sdk.Operation) error {
func (s *Service) processPush(ctx context.Context, op *sdk.Operation) (globalErr error) {
var missingAuth bool
if op.RepositoryStrategy.ConnectionType == "ssh" {
missingAuth = op.RepositoryStrategy.SSHKey == "" || op.RepositoryStrategy.SSHKeyContent == ""
Expand Down Expand Up @@ -64,6 +64,16 @@ func (s *Service) processPush(ctx context.Context, op *sdk.Operation) error {
}
}

// In case of error, we have to clean the filesystem, to avoid pending local branches or uncommited modification
defer func() {
if globalErr != nil {
r := s.Repo(*op)
if err := s.cleanFS(ctx, r); err != nil {
log.Error(ctx, "unable to clean FS: %v", err)
}
}
}()

// Erase existing cds directory for migration, if update make sure that the cds directory exists
if !op.Setup.Push.Update {
if _, err := os.Stat(path + "/.cds"); err == nil {
Expand Down

0 comments on commit 107a814

Please sign in to comment.