2018-09-16 14:50:53 +03:00
|
|
|
// Package bug contains the bug data model and low-level related functions
|
2018-07-12 13:44:46 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
import (
|
2018-09-12 17:57:04 +03:00
|
|
|
"encoding/json"
|
2018-07-13 22:21:24 +03:00
|
|
|
"fmt"
|
2018-08-05 16:23:51 +03:00
|
|
|
"strings"
|
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
"github.com/pkg/errors"
|
2019-02-01 14:22:00 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2019-02-03 21:55:35 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-07-13 22:21:24 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2018-07-13 17:13:40 +03:00
|
|
|
)
|
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
const bugsRefPattern = "refs/bugs/"
|
2018-07-25 18:58:54 +03:00
|
|
|
const bugsRemoteRefPattern = "refs/remotes/%s/bugs/"
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
const opsEntryName = "ops"
|
|
|
|
const rootEntryName = "root"
|
2018-08-05 16:23:51 +03:00
|
|
|
const mediaEntryName = "media"
|
2018-07-19 18:58:15 +03:00
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
const createClockEntryPrefix = "create-clock-"
|
|
|
|
const createClockEntryPattern = "create-clock-%d"
|
|
|
|
const editClockEntryPrefix = "edit-clock-"
|
|
|
|
const editClockEntryPattern = "edit-clock-%d"
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
var ErrBugNotExist = errors.New("bug doesn't exist")
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func NewErrMultipleMatchBug(matching []entity.Id) *entity.ErrMultipleMatch {
|
|
|
|
return entity.NewErrMultipleMatch("bug", matching)
|
2018-10-02 00:27:34 +03:00
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func NewErrMultipleMatchOp(matching []entity.Id) *entity.ErrMultipleMatch {
|
|
|
|
return entity.NewErrMultipleMatch("operation", matching)
|
2018-10-02 00:27:34 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
var _ Interface = &Bug{}
|
2019-08-11 15:08:03 +03:00
|
|
|
var _ entity.Interface = &Bug{}
|
2018-08-23 20:11:38 +03:00
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Bug hold the data of a bug thread, organized in a way close to
|
2018-07-23 01:04:46 +03:00
|
|
|
// how it will be persisted inside Git. This is the data structure
|
2018-08-13 16:28:16 +03:00
|
|
|
// used to merge two different version of the same Bug.
|
2018-07-13 17:13:40 +03:00
|
|
|
type Bug struct {
|
2018-08-06 21:31:20 +03:00
|
|
|
|
|
|
|
// A Lamport clock is a logical clock that allow to order event
|
|
|
|
// inside a distributed system.
|
|
|
|
// It must be the first field in this struct due to https://github.com/golang/go/issues/599
|
2018-09-11 23:04:16 +03:00
|
|
|
createTime lamport.Time
|
|
|
|
editTime lamport.Time
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Id used as unique identifier
|
2019-08-11 15:08:03 +03:00
|
|
|
id entity.Id
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
lastCommit git.Hash
|
|
|
|
rootPack git.Hash
|
2018-07-13 17:13:40 +03:00
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// all the committed operations
|
2018-07-13 22:21:24 +03:00
|
|
|
packs []OperationPack
|
2018-07-13 17:13:40 +03:00
|
|
|
|
2018-08-05 16:23:51 +03:00
|
|
|
// a temporary pack of operations used for convenience to pile up new operations
|
|
|
|
// before a commit
|
2018-07-13 22:21:24 +03:00
|
|
|
staging OperationPack
|
2018-07-12 13:44:46 +03:00
|
|
|
}
|
2018-07-12 16:14:37 +03:00
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// NewBug create a new Bug
|
2018-07-19 19:34:25 +03:00
|
|
|
func NewBug() *Bug {
|
2018-07-19 18:58:15 +03:00
|
|
|
// No id yet
|
2018-08-06 21:31:20 +03:00
|
|
|
// No logical clock yet
|
2018-07-19 19:34:25 +03:00
|
|
|
return &Bug{}
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// FindLocalBug find an existing Bug matching a prefix
|
2018-09-21 19:18:51 +03:00
|
|
|
func FindLocalBug(repo repository.ClockedRepo, prefix string) (*Bug, error) {
|
2018-08-19 14:58:55 +03:00
|
|
|
ids, err := ListLocalIds(repo)
|
2018-07-15 10:25:29 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// preallocate but empty
|
2019-08-11 15:08:03 +03:00
|
|
|
matching := make([]entity.Id, 0, 5)
|
2018-07-15 10:25:29 +03:00
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
for _, id := range ids {
|
2019-08-11 15:08:03 +03:00
|
|
|
if id.HasPrefix(prefix) {
|
2018-07-17 02:52:56 +03:00
|
|
|
matching = append(matching, id)
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) == 0 {
|
2018-10-02 00:27:34 +03:00
|
|
|
return nil, errors.New("no matching bug found.")
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
2019-08-12 17:12:14 +03:00
|
|
|
return nil, NewErrMultipleMatchBug(matching)
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
return ReadLocalBug(repo, matching[0])
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ReadLocalBug will read a local bug from its hash
|
2019-08-11 15:08:03 +03:00
|
|
|
func ReadLocalBug(repo repository.ClockedRepo, id entity.Id) (*Bug, error) {
|
|
|
|
ref := bugsRefPattern + id.String()
|
2018-07-23 01:04:46 +03:00
|
|
|
return readBug(repo, ref)
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ReadRemoteBug will read a remote bug from its hash
|
2018-09-21 19:18:51 +03:00
|
|
|
func ReadRemoteBug(repo repository.ClockedRepo, remote string, id string) (*Bug, error) {
|
2018-07-23 01:04:46 +03:00
|
|
|
ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id
|
|
|
|
return readBug(repo, ref)
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// readBug will read and parse a Bug from git
|
2018-09-21 19:18:51 +03:00
|
|
|
func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) {
|
2018-09-09 21:17:12 +03:00
|
|
|
refSplit := strings.Split(ref, "/")
|
2019-08-11 15:08:03 +03:00
|
|
|
id := entity.Id(refSplit[len(refSplit)-1])
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
if err := id.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid ref ")
|
2018-07-19 18:58:15 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:23:49 +03:00
|
|
|
hashes, err := repo.ListCommits(ref)
|
|
|
|
|
|
|
|
// TODO: this is not perfect, it might be a command invoke error
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrBugNotExist
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
bug := Bug{
|
2019-06-19 14:55:22 +03:00
|
|
|
id: id,
|
|
|
|
editTime: 0,
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// Load each OperationPack
|
2018-07-14 23:17:37 +03:00
|
|
|
for _, hash := range hashes {
|
|
|
|
entries, err := repo.ListEntries(hash)
|
|
|
|
if err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "can't list git tree entries")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
bug.lastCommit = hash
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
var opsEntry repository.TreeEntry
|
|
|
|
opsFound := false
|
|
|
|
var rootEntry repository.TreeEntry
|
|
|
|
rootFound := false
|
2018-08-06 21:31:20 +03:00
|
|
|
var createTime uint64
|
|
|
|
var editTime uint64
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
for _, entry := range entries {
|
2018-07-23 01:04:46 +03:00
|
|
|
if entry.Name == opsEntryName {
|
2018-07-14 23:17:37 +03:00
|
|
|
opsEntry = entry
|
|
|
|
opsFound = true
|
|
|
|
continue
|
|
|
|
}
|
2018-07-23 01:04:46 +03:00
|
|
|
if entry.Name == rootEntryName {
|
2018-07-14 23:17:37 +03:00
|
|
|
rootEntry = entry
|
|
|
|
rootFound = true
|
|
|
|
}
|
2018-08-06 21:31:20 +03:00
|
|
|
if strings.HasPrefix(entry.Name, createClockEntryPrefix) {
|
2019-11-19 02:28:06 +03:00
|
|
|
n, err := fmt.Sscanf(entry.Name, createClockEntryPattern, &createTime)
|
2018-08-06 21:31:20 +03:00
|
|
|
if err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "can't read create lamport time")
|
2018-08-06 21:31:20 +03:00
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return nil, fmt.Errorf("could not parse create time lamport value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(entry.Name, editClockEntryPrefix) {
|
2019-11-19 02:28:06 +03:00
|
|
|
n, err := fmt.Sscanf(entry.Name, editClockEntryPattern, &editTime)
|
2018-08-06 21:31:20 +03:00
|
|
|
if err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "can't read edit lamport time")
|
2018-08-06 21:31:20 +03:00
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return nil, fmt.Errorf("could not parse edit time lamport value")
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !opsFound {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.New("invalid tree, missing the ops entry")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
if !rootFound {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.New("invalid tree, missing the root entry")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-19 18:58:15 +03:00
|
|
|
if bug.rootPack == "" {
|
|
|
|
bug.rootPack = rootEntry.Hash
|
2018-09-11 23:04:16 +03:00
|
|
|
bug.createTime = lamport.Time(createTime)
|
2018-08-06 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:55:22 +03:00
|
|
|
// Due to rebase, edit Lamport time are not necessarily ordered
|
|
|
|
if editTime > uint64(bug.editTime) {
|
|
|
|
bug.editTime = lamport.Time(editTime)
|
|
|
|
}
|
2018-08-06 21:31:20 +03:00
|
|
|
|
|
|
|
// Update the clocks
|
2019-11-19 02:27:40 +03:00
|
|
|
if err := repo.WitnessCreate(bug.createTime); err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to update create lamport clock")
|
2018-08-06 21:31:20 +03:00
|
|
|
}
|
2019-11-19 02:27:40 +03:00
|
|
|
if err := repo.WitnessEdit(bug.editTime); err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to update edit lamport clock")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := repo.ReadData(opsEntry.Hash)
|
|
|
|
if err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to read git blob data")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
opp := &OperationPack{}
|
|
|
|
err = json.Unmarshal(data, &opp)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-19 00:36:22 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to decode OperationPack json")
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// tag the pack with the commit hash
|
2018-09-12 17:57:04 +03:00
|
|
|
opp.commitHash = hash
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
bug.packs = append(bug.packs, *opp)
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:22:00 +03:00
|
|
|
// Make sure that the identities are properly loaded
|
|
|
|
resolver := identity.NewSimpleResolver(repo)
|
|
|
|
err = bug.EnsureIdentities(resolver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
return &bug, nil
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
type StreamedBug struct {
|
|
|
|
Bug *Bug
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ReadAllLocalBugs read and parse all local bugs
|
2018-09-21 19:18:51 +03:00
|
|
|
func ReadAllLocalBugs(repo repository.ClockedRepo) <-chan StreamedBug {
|
2018-07-23 01:04:46 +03:00
|
|
|
return readAllBugs(repo, bugsRefPattern)
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ReadAllRemoteBugs read and parse all remote bugs for a given remote
|
2018-09-21 19:18:51 +03:00
|
|
|
func ReadAllRemoteBugs(repo repository.ClockedRepo, remote string) <-chan StreamedBug {
|
2018-07-23 01:04:46 +03:00
|
|
|
refPrefix := fmt.Sprintf(bugsRemoteRefPattern, remote)
|
|
|
|
return readAllBugs(repo, refPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and parse all available bug with a given ref prefix
|
2018-09-21 19:18:51 +03:00
|
|
|
func readAllBugs(repo repository.ClockedRepo, refPrefix string) <-chan StreamedBug {
|
2018-07-23 01:04:46 +03:00
|
|
|
out := make(chan StreamedBug)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
refs, err := repo.ListRefs(refPrefix)
|
|
|
|
if err != nil {
|
|
|
|
out <- StreamedBug{Err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range refs {
|
|
|
|
b, err := readBug(repo, ref)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- StreamedBug{Err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out <- StreamedBug{Bug: b}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// ListLocalIds list all the available local bug ids
|
2019-08-11 15:08:03 +03:00
|
|
|
func ListLocalIds(repo repository.Repo) ([]entity.Id, error) {
|
2018-08-19 14:58:55 +03:00
|
|
|
refs, err := repo.ListRefs(bugsRefPattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return refsToIds(refs), nil
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
func refsToIds(refs []string) []entity.Id {
|
|
|
|
ids := make([]entity.Id, len(refs))
|
2018-08-19 14:58:55 +03:00
|
|
|
|
|
|
|
for i, ref := range refs {
|
2018-09-09 21:17:12 +03:00
|
|
|
split := strings.Split(ref, "/")
|
2019-08-11 15:08:03 +03:00
|
|
|
ids[i] = entity.Id(split[len(split)-1])
|
2018-08-19 14:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
2018-07-29 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
// Validate check if the Bug data is valid
|
|
|
|
func (bug *Bug) Validate() error {
|
2018-07-13 17:13:40 +03:00
|
|
|
// non-empty
|
2018-07-13 22:21:24 +03:00
|
|
|
if len(bug.packs) == 0 && bug.staging.IsEmpty() {
|
2018-09-15 14:15:00 +03:00
|
|
|
return fmt.Errorf("bug has no operations")
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
// check if each pack and operations are valid
|
2018-07-13 22:21:24 +03:00
|
|
|
for _, pack := range bug.packs {
|
2018-09-15 14:15:00 +03:00
|
|
|
if err := pack.Validate(); err != nil {
|
|
|
|
return err
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// check if staging is valid if needed
|
|
|
|
if !bug.staging.IsEmpty() {
|
2018-09-15 14:15:00 +03:00
|
|
|
if err := bug.staging.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "staging")
|
2018-07-13 17:48:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:28:37 +03:00
|
|
|
// The very first Op should be a CreateOp
|
2018-08-15 23:01:45 +03:00
|
|
|
firstOp := bug.FirstOp()
|
2018-09-28 21:39:39 +03:00
|
|
|
if firstOp == nil || firstOp.base().OperationType != CreateOp {
|
2018-09-15 14:15:00 +03:00
|
|
|
return fmt.Errorf("first operation should be a Create op")
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
// The bug Id should be the hash of the first commit
|
|
|
|
if len(bug.packs) > 0 && string(bug.packs[0].commitHash) != bug.id.String() {
|
2019-02-16 19:32:30 +03:00
|
|
|
return fmt.Errorf("bug id should be the first commit hash")
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:28:37 +03:00
|
|
|
// Check that there is no more CreateOp op
|
2019-08-27 13:11:51 +03:00
|
|
|
// Check that there is no colliding operation's ID
|
2018-07-13 17:13:40 +03:00
|
|
|
it := NewOperationIterator(bug)
|
|
|
|
createCount := 0
|
2019-08-27 13:11:51 +03:00
|
|
|
ids := make(map[entity.Id]struct{})
|
2018-07-13 17:13:40 +03:00
|
|
|
for it.Next() {
|
2018-09-28 21:39:39 +03:00
|
|
|
if it.Value().base().OperationType == CreateOp {
|
2018-07-13 17:13:40 +03:00
|
|
|
createCount++
|
|
|
|
}
|
2019-08-27 13:11:51 +03:00
|
|
|
if _, ok := ids[it.Value().Id()]; ok {
|
|
|
|
return fmt.Errorf("id collision: %s", it.Value().Id())
|
|
|
|
}
|
|
|
|
ids[it.Value().Id()] = struct{}{}
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if createCount != 1 {
|
2018-09-15 14:15:00 +03:00
|
|
|
return fmt.Errorf("only one Create op allowed")
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
return nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-08-09 15:45:02 +03:00
|
|
|
// Append an operation into the staging area, to be committed later
|
2018-07-13 17:13:40 +03:00
|
|
|
func (bug *Bug) Append(op Operation) {
|
2018-07-13 22:21:24 +03:00
|
|
|
bug.staging.Append(op)
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// Commit write the staging area in Git and move the operations to the packs
|
2018-09-21 19:18:51 +03:00
|
|
|
func (bug *Bug) Commit(repo repository.ClockedRepo) error {
|
2019-02-16 19:32:30 +03:00
|
|
|
|
|
|
|
if !bug.NeedCommit() {
|
2018-08-03 00:37:49 +03:00
|
|
|
return fmt.Errorf("can't commit a bug with no pending operation")
|
2018-07-13 22:21:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
if err := bug.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "can't commit a bug with invalid data")
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// Write the Ops as a Git blob containing the serialized array
|
|
|
|
hash, err := bug.staging.Write(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-19 18:58:15 +03:00
|
|
|
if bug.rootPack == "" {
|
|
|
|
bug.rootPack = hash
|
2018-07-14 07:42:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:23:51 +03:00
|
|
|
// Make a Git tree referencing this blob
|
2018-08-03 00:37:49 +03:00
|
|
|
tree := []repository.TreeEntry{
|
2018-07-19 18:58:15 +03:00
|
|
|
// the last pack of ops
|
2018-07-23 01:04:46 +03:00
|
|
|
{ObjectType: repository.Blob, Hash: hash, Name: opsEntryName},
|
2018-07-19 18:58:15 +03:00
|
|
|
// always the first pack of ops (might be the same)
|
2018-07-23 01:04:46 +03:00
|
|
|
{ObjectType: repository.Blob, Hash: bug.rootPack, Name: rootEntryName},
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
// Reference, if any, all the files required by the ops
|
2018-08-05 16:23:51 +03:00
|
|
|
// Git will check that they actually exist in the storage and will make sure
|
|
|
|
// to push/pull them as needed.
|
|
|
|
mediaTree := makeMediaTree(bug.staging)
|
|
|
|
if len(mediaTree) > 0 {
|
|
|
|
mediaTreeHash, err := repo.StoreTree(mediaTree)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
2018-08-05 16:23:51 +03:00
|
|
|
tree = append(tree, repository.TreeEntry{
|
|
|
|
ObjectType: repository.Tree,
|
|
|
|
Hash: mediaTreeHash,
|
|
|
|
Name: mediaEntryName,
|
|
|
|
})
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
2018-07-19 18:58:15 +03:00
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
// Store the logical clocks as well
|
|
|
|
// --> edit clock for each OperationPack/commits
|
|
|
|
// --> create clock only for the first OperationPack/commits
|
|
|
|
//
|
|
|
|
// To avoid having one blob for each clock value, clocks are serialized
|
|
|
|
// directly into the entry name
|
|
|
|
emptyBlobHash, err := repo.StoreData([]byte{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-03 22:33:34 +03:00
|
|
|
bug.editTime, err = repo.EditTimeIncrement()
|
2018-08-06 21:31:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = append(tree, repository.TreeEntry{
|
|
|
|
ObjectType: repository.Blob,
|
|
|
|
Hash: emptyBlobHash,
|
2018-09-03 22:33:34 +03:00
|
|
|
Name: fmt.Sprintf(editClockEntryPattern, bug.editTime),
|
2018-08-06 21:31:20 +03:00
|
|
|
})
|
|
|
|
if bug.lastCommit == "" {
|
2018-09-03 22:33:34 +03:00
|
|
|
bug.createTime, err = repo.CreateTimeIncrement()
|
2018-08-06 21:31:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = append(tree, repository.TreeEntry{
|
|
|
|
ObjectType: repository.Blob,
|
|
|
|
Hash: emptyBlobHash,
|
2018-09-03 22:33:34 +03:00
|
|
|
Name: fmt.Sprintf(createClockEntryPattern, bug.createTime),
|
2018-08-06 21:31:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:23:51 +03:00
|
|
|
// Store the tree
|
2018-08-03 00:37:49 +03:00
|
|
|
hash, err = repo.StoreTree(tree)
|
2018-07-13 22:21:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a Git commit referencing the tree, with the previous commit as parent
|
|
|
|
if bug.lastCommit != "" {
|
|
|
|
hash, err = repo.StoreCommitWithParent(hash, bug.lastCommit)
|
|
|
|
} else {
|
|
|
|
hash, err = repo.StoreCommit(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
bug.lastCommit = hash
|
|
|
|
|
2018-07-19 18:58:15 +03:00
|
|
|
// if it was the first commit, use the commit hash as bug id
|
|
|
|
if bug.id == "" {
|
2019-08-11 15:08:03 +03:00
|
|
|
bug.id = entity.Id(hash)
|
2018-07-19 18:58:15 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// Create or update the Git reference for this bug
|
2018-08-05 16:23:51 +03:00
|
|
|
// When pushing later, the remote will ensure that this ref update
|
|
|
|
// is fast-forward, that is no data has been overwritten
|
2018-07-23 01:04:46 +03:00
|
|
|
ref := fmt.Sprintf("%s%s", bugsRefPattern, bug.id)
|
2018-07-13 22:21:24 +03:00
|
|
|
err = repo.UpdateRef(ref, hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
bug.staging.commitHash = hash
|
2018-07-13 22:21:24 +03:00
|
|
|
bug.packs = append(bug.packs, bug.staging)
|
|
|
|
bug.staging = OperationPack{}
|
|
|
|
|
|
|
|
return nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
func (bug *Bug) CommitAsNeeded(repo repository.ClockedRepo) error {
|
|
|
|
if !bug.NeedCommit() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bug.Commit(repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bug *Bug) NeedCommit() bool {
|
|
|
|
return !bug.staging.IsEmpty()
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:23:51 +03:00
|
|
|
func makeMediaTree(pack OperationPack) []repository.TreeEntry {
|
|
|
|
var tree []repository.TreeEntry
|
|
|
|
counter := 0
|
2018-09-11 23:04:16 +03:00
|
|
|
added := make(map[git.Hash]interface{})
|
2018-08-05 16:23:51 +03:00
|
|
|
|
|
|
|
for _, ops := range pack.Operations {
|
2018-09-12 17:57:04 +03:00
|
|
|
for _, file := range ops.GetFiles() {
|
2018-08-05 16:23:51 +03:00
|
|
|
if _, has := added[file]; !has {
|
|
|
|
tree = append(tree, repository.TreeEntry{
|
|
|
|
ObjectType: repository.Blob,
|
|
|
|
Hash: file,
|
|
|
|
// The name is not important here, we only need to
|
|
|
|
// reference the blob.
|
|
|
|
Name: fmt.Sprintf("file%d", counter),
|
|
|
|
})
|
|
|
|
counter++
|
|
|
|
added[file] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// Merge a different version of the same bug by rebasing operations of this bug
|
|
|
|
// that are not present in the other on top of the chain of operations of the
|
|
|
|
// other version.
|
2018-08-23 20:11:38 +03:00
|
|
|
func (bug *Bug) Merge(repo repository.Repo, other Interface) (bool, error) {
|
|
|
|
var otherBug = bugFromInterface(other)
|
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
// Note: a faster merge should be possible without actually reading and parsing
|
|
|
|
// all operations pack of our side.
|
|
|
|
// Reading the other side is still necessary to validate remote data, at least
|
|
|
|
// for new operations
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
if bug.id != otherBug.id {
|
2018-07-17 02:52:56 +03:00
|
|
|
return false, errors.New("merging unrelated bugs is not supported")
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
if len(otherBug.staging.Operations) > 0 {
|
2018-07-17 02:52:56 +03:00
|
|
|
return false, errors.New("merging a bug with a non-empty staging is not supported")
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
if bug.lastCommit == "" || otherBug.lastCommit == "" {
|
2018-07-17 02:52:56 +03:00
|
|
|
return false, errors.New("can't merge a bug that has never been stored")
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
ancestor, err := repo.FindCommonAncestor(bug.lastCommit, otherBug.lastCommit)
|
2018-07-17 02:52:56 +03:00
|
|
|
if err != nil {
|
2019-02-07 00:06:42 +03:00
|
|
|
return false, errors.Wrap(err, "can't find common ancestor")
|
2018-07-17 02:52:56 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
ancestorIndex := 0
|
|
|
|
newPacks := make([]OperationPack, 0, len(bug.packs))
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
// Find the root of the rebase
|
2018-07-17 02:52:56 +03:00
|
|
|
for i, pack := range bug.packs {
|
2018-07-25 18:59:40 +03:00
|
|
|
newPacks = append(newPacks, pack)
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
if pack.commitHash == ancestor {
|
|
|
|
ancestorIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
if len(otherBug.packs) == ancestorIndex+1 {
|
2018-07-25 18:59:40 +03:00
|
|
|
// Nothing to rebase, return early
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
// get other bug's extra packs
|
2018-08-23 20:11:38 +03:00
|
|
|
for i := ancestorIndex + 1; i < len(otherBug.packs); i++ {
|
2018-07-25 18:59:40 +03:00
|
|
|
// clone is probably not necessary
|
2018-08-23 20:11:38 +03:00
|
|
|
newPack := otherBug.packs[i].Clone()
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
newPacks = append(newPacks, newPack)
|
|
|
|
bug.lastCommit = newPack.commitHash
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
// rebase our extra packs
|
|
|
|
for i := ancestorIndex + 1; i < len(bug.packs); i++ {
|
|
|
|
pack := bug.packs[i]
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// get the referenced git tree
|
|
|
|
treeHash, err := repo.GetTreeHash(pack.commitHash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new commit with the correct ancestor
|
|
|
|
hash, err := repo.StoreCommitWithParent(treeHash, bug.lastCommit)
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// replace the pack
|
2018-07-25 18:59:40 +03:00
|
|
|
newPack := pack.Clone()
|
|
|
|
newPack.commitHash = hash
|
|
|
|
newPacks = append(newPacks, newPack)
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// update the bug
|
|
|
|
bug.lastCommit = hash
|
|
|
|
}
|
|
|
|
|
2019-11-19 02:26:38 +03:00
|
|
|
bug.packs = newPacks
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// Update the git ref
|
2019-08-11 15:08:03 +03:00
|
|
|
err = repo.UpdateRef(bugsRefPattern+bug.id.String(), bug.lastCommit)
|
2018-07-25 18:59:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2018-07-17 02:52:56 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:59:40 +03:00
|
|
|
return true, nil
|
2018-07-17 02:52:56 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// Id return the Bug identifier
|
2019-08-11 15:08:03 +03:00
|
|
|
func (bug *Bug) Id() entity.Id {
|
2018-07-19 18:58:15 +03:00
|
|
|
if bug.id == "" {
|
|
|
|
// simply panic as it would be a coding error
|
|
|
|
// (using an id of a bug not stored yet)
|
|
|
|
panic("no id yet")
|
|
|
|
}
|
2018-07-14 23:48:54 +03:00
|
|
|
return bug.id
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:19:16 +03:00
|
|
|
// CreateLamportTime return the Lamport time of creation
|
2018-09-11 23:04:16 +03:00
|
|
|
func (bug *Bug) CreateLamportTime() lamport.Time {
|
2018-08-23 20:19:16 +03:00
|
|
|
return bug.createTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditLamportTime return the Lamport time of the last edit
|
2018-09-11 23:04:16 +03:00
|
|
|
func (bug *Bug) EditLamportTime() lamport.Time {
|
2018-08-23 20:19:16 +03:00
|
|
|
return bug.editTime
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// Lookup for the very first operation of the bug.
|
2018-07-17 20:28:37 +03:00
|
|
|
// For a valid Bug, this operation should be a CreateOp
|
2018-08-15 23:01:45 +03:00
|
|
|
func (bug *Bug) FirstOp() Operation {
|
2018-07-13 22:21:24 +03:00
|
|
|
for _, pack := range bug.packs {
|
2018-07-13 17:48:55 +03:00
|
|
|
for _, op := range pack.Operations {
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
if !bug.staging.IsEmpty() {
|
|
|
|
return bug.staging.Operations[0]
|
2018-07-13 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
// Lookup for the very last operation of the bug.
|
|
|
|
// For a valid Bug, should never be nil
|
2018-08-15 23:01:45 +03:00
|
|
|
func (bug *Bug) LastOp() Operation {
|
2018-08-06 21:31:20 +03:00
|
|
|
if !bug.staging.IsEmpty() {
|
|
|
|
return bug.staging.Operations[len(bug.staging.Operations)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bug.packs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPack := bug.packs[len(bug.packs)-1]
|
|
|
|
|
|
|
|
if len(lastPack.Operations) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastPack.Operations[len(lastPack.Operations)-1]
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// Compile a bug in a easily usable snapshot
|
|
|
|
func (bug *Bug) Compile() Snapshot {
|
2018-07-17 20:28:37 +03:00
|
|
|
snap := Snapshot{
|
|
|
|
id: bug.id,
|
|
|
|
Status: OpenStatus,
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
it := NewOperationIterator(bug)
|
|
|
|
|
|
|
|
for it.Next() {
|
2018-07-18 01:16:06 +03:00
|
|
|
op := it.Value()
|
2018-09-29 12:28:18 +03:00
|
|
|
op.Apply(&snap)
|
2018-07-18 01:16:06 +03:00
|
|
|
snap.Operations = append(snap.Operations, op)
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return snap
|
|
|
|
}
|