2018-07-12 13:44:46 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
import (
|
2018-07-14 23:48:54 +03:00
|
|
|
"crypto/sha256"
|
2018-07-14 23:17:37 +03:00
|
|
|
"errors"
|
2018-07-13 22:21:24 +03:00
|
|
|
"fmt"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util"
|
2018-07-13 17:13:40 +03:00
|
|
|
"github.com/kevinburke/go.uuid"
|
2018-07-15 10:25:29 +03:00
|
|
|
"strings"
|
2018-07-13 17:13:40 +03:00
|
|
|
)
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
const BugsRefPattern = "refs/bugs/"
|
2018-07-13 22:51:27 +03:00
|
|
|
const BugsRemoteRefPattern = "refs/remote/%s/bugs/"
|
2018-07-14 23:17:37 +03:00
|
|
|
const OpsEntryName = "ops"
|
|
|
|
const RootEntryName = "root"
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Bug hold the data of a bug thread, organized in a way close to
|
|
|
|
// how it will be persisted inside Git. This is the datastructure
|
|
|
|
// used for merge of two different version.
|
|
|
|
type Bug struct {
|
|
|
|
// Id used as unique identifier
|
2018-07-14 23:48:54 +03:00
|
|
|
id string
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
lastCommit util.Hash
|
2018-07-14 07:42:13 +03:00
|
|
|
root util.Hash
|
2018-07-13 17:13:40 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// TODO: need a way to order bugs, probably a Lamport clock
|
2018-07-13 17:13:40 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
packs []OperationPack
|
2018-07-13 17:13:40 +03:00
|
|
|
|
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-07-13 17:13:40 +03:00
|
|
|
// Create a new Bug
|
|
|
|
func NewBug() (*Bug, error) {
|
2018-07-16 16:21:21 +03:00
|
|
|
// TODO: replace with commit hash of (first commit + some random)
|
2018-07-15 10:25:29 +03:00
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Creating UUID Version 4
|
2018-07-14 23:48:54 +03:00
|
|
|
unique, err := uuid.ID4()
|
2018-07-13 17:13:40 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:48:54 +03:00
|
|
|
// Use it as source of uniqueness
|
|
|
|
hash := sha256.New().Sum(unique.Bytes())
|
|
|
|
|
|
|
|
// format in hex and truncate to 40 char
|
|
|
|
id := fmt.Sprintf("%.40s", fmt.Sprintf("%x", hash))
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
return &Bug{
|
2018-07-14 23:48:54 +03:00
|
|
|
id: id,
|
2018-07-13 17:13:40 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:25:29 +03:00
|
|
|
// Find an existing Bug matching a prefix
|
|
|
|
func FindBug(repo repository.Repo, prefix string) (*Bug, error) {
|
2018-07-17 02:52:56 +03:00
|
|
|
ids, err := repo.ListRefs(BugsRefPattern)
|
2018-07-15 10:25:29 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// preallocate but empty
|
|
|
|
matching := make([]string, 0, 5)
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
for _, id := range ids {
|
|
|
|
if strings.HasPrefix(id, prefix) {
|
|
|
|
matching = append(matching, id)
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) == 0 {
|
|
|
|
return nil, errors.New("No matching bug found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
|
|
|
return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
return ReadBug(repo, BugsRefPattern+matching[0])
|
2018-07-15 10:25:29 +03:00
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// Read and parse a Bug from git
|
2018-07-17 02:52:56 +03:00
|
|
|
func ReadBug(repo repository.Repo, ref string) (*Bug, error) {
|
|
|
|
hashes, err := repo.ListCommits(ref)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
refSplitted := strings.Split(ref, "/")
|
|
|
|
id := refSplitted[len(refSplitted)-1]
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
bug := Bug{
|
2018-07-14 23:48:54 +03:00
|
|
|
id: id,
|
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)
|
|
|
|
|
|
|
|
bug.lastCommit = hash
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var opsEntry repository.TreeEntry
|
|
|
|
opsFound := false
|
|
|
|
var rootEntry repository.TreeEntry
|
|
|
|
rootFound := false
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.Name == OpsEntryName {
|
|
|
|
opsEntry = entry
|
|
|
|
opsFound = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if entry.Name == RootEntryName {
|
|
|
|
rootEntry = entry
|
|
|
|
rootFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opsFound {
|
|
|
|
return nil, errors.New("Invalid tree, missing the ops entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rootFound {
|
|
|
|
return nil, errors.New("Invalid tree, missing the root entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if bug.root == "" {
|
|
|
|
bug.root = rootEntry.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := repo.ReadData(opsEntry.Hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := ParseOperationPack(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// tag the pack with the commit hash
|
|
|
|
op.commitHash = hash
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
bug.packs = append(bug.packs, *op)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bug, nil
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// IsValid check if the Bug data is valid
|
|
|
|
func (bug *Bug) IsValid() bool {
|
|
|
|
// non-empty
|
2018-07-13 22:21:24 +03:00
|
|
|
if len(bug.packs) == 0 && bug.staging.IsEmpty() {
|
2018-07-13 17:13:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if each pack is valid
|
2018-07-13 22:21:24 +03:00
|
|
|
for _, pack := range bug.packs {
|
2018-07-13 17:13:40 +03:00
|
|
|
if !pack.IsValid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// check if staging is valid if needed
|
|
|
|
if !bug.staging.IsEmpty() {
|
|
|
|
if !bug.staging.IsValid() {
|
2018-07-13 17:48:55 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:28:37 +03:00
|
|
|
// The very first Op should be a CreateOp
|
2018-07-13 17:48:55 +03:00
|
|
|
firstOp := bug.firstOp()
|
2018-07-17 20:28:37 +03:00
|
|
|
if firstOp == nil || firstOp.OpType() != CreateOp {
|
2018-07-13 17:13:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:28:37 +03:00
|
|
|
// Check that there is no more CreateOp op
|
2018-07-13 17:13:40 +03:00
|
|
|
it := NewOperationIterator(bug)
|
|
|
|
createCount := 0
|
|
|
|
for it.Next() {
|
2018-07-17 20:28:37 +03:00
|
|
|
if it.Value().OpType() == CreateOp {
|
2018-07-13 17:13:40 +03:00
|
|
|
createCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if createCount != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-14 07:42:13 +03:00
|
|
|
// Write the staging area in Git and move the operations to the packs
|
2018-07-13 22:21:24 +03:00
|
|
|
func (bug *Bug) Commit(repo repository.Repo) error {
|
|
|
|
if bug.staging.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the Ops as a Git blob containing the serialized array
|
|
|
|
hash, err := bug.staging.Write(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-14 07:42:13 +03:00
|
|
|
root := bug.root
|
|
|
|
if root == "" {
|
|
|
|
root = hash
|
2018-07-14 23:17:37 +03:00
|
|
|
bug.root = hash
|
2018-07-14 07:42:13 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// Write a Git tree referencing this blob
|
2018-07-14 23:17:37 +03:00
|
|
|
hash, err = repo.StoreTree([]repository.TreeEntry{
|
|
|
|
{repository.Blob, hash, OpsEntryName}, // the last pack of ops
|
|
|
|
{repository.Blob, root, RootEntryName}, // always the first pack of ops (might be the same)
|
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-13 22:21:24 +03:00
|
|
|
// Create or update the Git reference for this bug
|
2018-07-14 23:48:54 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
bug.packs = append(bug.packs, bug.staging)
|
|
|
|
bug.staging = OperationPack{}
|
|
|
|
|
|
|
|
return nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (bug *Bug) Merge(repo repository.Repo, other *Bug) (bool, error) {
|
|
|
|
|
|
|
|
if bug.id != other.id {
|
|
|
|
return false, errors.New("merging unrelated bugs is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(other.staging.Operations) > 0 {
|
|
|
|
return false, errors.New("merging a bug with a non-empty staging is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
if bug.lastCommit == "" || other.lastCommit == "" {
|
|
|
|
return false, errors.New("can't merge a bug that has never been stored")
|
|
|
|
}
|
|
|
|
|
|
|
|
ancestor, err := repo.FindCommonAncestor(bug.lastCommit, other.lastCommit)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rebaseStarted := false
|
|
|
|
updated := false
|
|
|
|
|
|
|
|
for i, pack := range bug.packs {
|
|
|
|
if pack.commitHash == ancestor {
|
|
|
|
rebaseStarted = true
|
|
|
|
|
|
|
|
// get other bug's extra pack
|
|
|
|
for j := i + 1; j < len(other.packs); j++ {
|
|
|
|
// clone is probably not necessary
|
|
|
|
newPack := other.packs[j].Clone()
|
|
|
|
|
|
|
|
bug.packs = append(bug.packs, newPack)
|
|
|
|
bug.lastCommit = newPack.commitHash
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rebaseStarted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
updated = true
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// replace the pack
|
|
|
|
bug.packs[i] = pack.Clone()
|
|
|
|
bug.packs[i].commitHash = hash
|
|
|
|
|
|
|
|
// update the bug
|
|
|
|
bug.lastCommit = hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the git ref
|
|
|
|
if updated {
|
|
|
|
err := repo.UpdateRef(BugsRefPattern+bug.id, bug.lastCommit)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the Bug identifier
|
2018-07-14 23:17:37 +03:00
|
|
|
func (bug *Bug) Id() string {
|
2018-07-14 23:48:54 +03:00
|
|
|
return bug.id
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// Return the Bug identifier truncated for human consumption
|
2018-07-13 17:13:40 +03:00
|
|
|
func (bug *Bug) HumanId() string {
|
2018-07-14 23:48:54 +03:00
|
|
|
return fmt.Sprintf("%.8s", bug.id)
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
2018-07-13 17:48:55 +03:00
|
|
|
|
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-07-13 17:48:55 +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
|
|
|
|
|
|
|
// 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() {
|
|
|
|
snap = it.Value().Apply(snap)
|
|
|
|
}
|
|
|
|
|
|
|
|
return snap
|
|
|
|
}
|