Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/path_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -138,6 +139,16 @@ func (b *Backend) pathPublish(ctx context.Context, req *logical.Request, fields

isAncestor, err := trdlGit.IsAncestor(gitRepo, lastPublishedGitCommit, headRef.Hash().String())
if err != nil {
var ancestorErr *trdlGit.AncestorCommitLookupError
if errors.As(err, &ancestorErr) {
msg := fmt.Sprintf(
"Previously published commit %q not found in the Git repository: %v. This might have been caused by a force push rewriting history. If you're confident this is expected, you can override 'last_published_git_commit' option in the project configuration.",
lastPublishedGitCommit, ancestorErr,
)
logboek.Context(ctx).Default().LogF(msg)
b.Logger().Error(msg)
return err
}
return err
}

Expand Down
24 changes: 22 additions & 2 deletions server/pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"time"

"github.com/go-git/go-billy/v5/memfs"
Expand Down Expand Up @@ -169,15 +170,34 @@ func ReadWorktreeFile(gitRepo *git.Repository, path string) ([]byte, error) {
return data, nil
}

type AncestorCommitLookupError struct {
Commit string
Err error
}

func (e *AncestorCommitLookupError) Error() string {
return fmt.Sprintf("unable to get ancestor commit %q object: %v", e.Commit, e.Err)
}

func (e *AncestorCommitLookupError) Unwrap() error {
return e.Err
}

func IsAncestor(gitRepo *git.Repository, ancestorCommit, descendantCommit string) (bool, error) {
ancestorCommitObj, err := gitRepo.CommitObject(plumbing.NewHash(ancestorCommit))
if err != nil {
return false, fmt.Errorf("unable to get commit %q object: %w", ancestorCommit, err)
if strings.Contains(err.Error(), "object not found") {
return false, &AncestorCommitLookupError{
Commit: ancestorCommit,
Err: err,
}
}
return false, fmt.Errorf("unable to get ancestor commit %q object: %w", ancestorCommit, err)
}

descendantCommitObj, err := gitRepo.CommitObject(plumbing.NewHash(descendantCommit))
if err != nil {
return false, fmt.Errorf("unable to get commit %q object: %w", descendantCommitObj, err)
return false, fmt.Errorf("unable to get descendant commit %q object: %w", descendantCommitObj, err)
}

isAncestor, err := ancestorCommitObj.IsAncestor(descendantCommitObj)
Expand Down
Loading