diff options
author | John Cai <jcai@gitlab.com> | 2022-03-02 00:59:00 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-03-02 00:59:00 +0300 |
commit | 32e231be9178563f935d4b21d7e055156a8f29d4 (patch) | |
tree | 4fb8879df2f3c056891d87a60efa7e3b463f8201 | |
parent | 147dedbc9543275efcf6943a903277e8a850d602 (diff) | |
parent | 037f1719d75ecd3fa2ccdaa8cd3cb8e8b6a723c5 (diff) |
Merge branch 'pks-refs-unlock-symref-on-failed-update' into 'master'
localrepo: Unlock symbolic refs when the update fails
Closes #4075
See merge request gitlab-org/gitaly!4379
-rw-r--r-- | internal/git/localrepo/refs.go | 6 | ||||
-rw-r--r-- | internal/git/localrepo/refs_test.go | 24 |
2 files changed, 30 insertions, 0 deletions
diff --git a/internal/git/localrepo/refs.go b/internal/git/localrepo/refs.go index 769a833a4..30cc93000 100644 --- a/internal/git/localrepo/refs.go +++ b/internal/git/localrepo/refs.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "gitlab.com/gitlab-org/gitaly/v14/internal/command" "gitlab.com/gitlab-org/gitaly/v14/internal/git" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/transaction" @@ -205,6 +206,11 @@ func (repo *Repo) setDefaultBranchWithTransaction(ctx context.Context, txManager if err != nil { return fmt.Errorf("getting file writer: %w", err) } + defer func() { + if err := lockingFileWriter.Close(); err != nil { + ctxlogrus.Extract(ctx).WithError(err).Error("closing locked HEAD: %w", err) + } + }() if _, err := lockingFileWriter.Write(newHeadContent); err != nil { return fmt.Errorf("writing temporary HEAD: %w", err) diff --git a/internal/git/localrepo/refs_test.go b/internal/git/localrepo/refs_test.go index 65fce6f20..df5c1c7ec 100644 --- a/internal/git/localrepo/refs_test.go +++ b/internal/git/localrepo/refs_test.go @@ -3,6 +3,7 @@ package localrepo import ( "bytes" "context" + "errors" "fmt" "os" "path/filepath" @@ -598,6 +599,29 @@ func TestRepo_SetDefaultBranch_errors(t *testing.T) { ch <- struct{}{} <-doneCh }) + + t.Run("failing vote unlocks symref", func(t *testing.T) { + ctx, err := txinfo.InjectTransaction( + peer.NewContext(ctx, &peer.Peer{}), + 1, + "node", + true, + ) + require.NoError(t, err) + + _, repo, repoPath := setupRepo(t) + + failingTxManager := &transaction.MockManager{ + VoteFn: func(context.Context, txinfo.Transaction, voting.Vote, voting.Phase) error { + return errors.New("injected error") + }, + } + + err = repo.SetDefaultBranch(ctx, failingTxManager, "refs/heads/branch") + require.Error(t, err) + require.Equal(t, "committing temporary HEAD: voting on locked file: preimage vote: injected error", err.Error()) + require.NoFileExists(t, filepath.Join(repoPath, "HEAD.lock")) + }) } func TestGuessHead(t *testing.T) { |