2018-07-23 01:04:46 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-08-05 16:23:51 +03:00
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-08-05 16:23:51 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-19 22:41:46 +03:00
|
|
|
"github.com/pkg/errors"
|
2018-07-23 01:04:46 +03:00
|
|
|
)
|
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
// Fetch retrieve updates from a remote
|
2018-09-02 16:37:28 +03:00
|
|
|
// This does not change the local bugs state
|
2018-08-12 22:09:30 +03:00
|
|
|
func Fetch(repo repository.Repo, remote string) (string, error) {
|
2018-07-23 01:04:46 +03:00
|
|
|
remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
|
|
|
|
fetchRefSpec := fmt.Sprintf("%s*:%s*", bugsRefPattern, remoteRefSpec)
|
|
|
|
|
2019-03-25 23:09:56 +03:00
|
|
|
return repo.FetchRefs(remote, fetchRefSpec)
|
2018-07-23 01:04:46 +03:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// Push update a remote with the local changes
|
2018-08-12 22:09:30 +03:00
|
|
|
func Push(repo repository.Repo, remote string) (string, error) {
|
2019-03-25 23:09:56 +03:00
|
|
|
return repo.PushRefs(remote, bugsRefPattern+"*")
|
2018-07-23 01:04:46 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 18:10:40 +03:00
|
|
|
// Pull will do a Fetch + MergeAll
|
2019-02-16 15:48:46 +03:00
|
|
|
// This function will return an error if a merge fail
|
2018-09-21 19:18:51 +03:00
|
|
|
func Pull(repo repository.ClockedRepo, remote string) error {
|
2019-03-25 23:09:56 +03:00
|
|
|
_, err := Fetch(repo, remote)
|
2018-08-12 22:09:30 +03:00
|
|
|
if err != nil {
|
2018-07-25 19:01:32 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for merge := range MergeAll(repo, remote) {
|
|
|
|
if merge.Err != nil {
|
|
|
|
return merge.Err
|
|
|
|
}
|
2019-03-27 23:44:11 +03:00
|
|
|
if merge.Status == entity.MergeStatusInvalid {
|
2019-02-16 15:48:46 +03:00
|
|
|
return errors.Errorf("merge failure: %s", merge.Reason)
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
2018-07-25 19:01:32 +03:00
|
|
|
}
|
2018-08-12 22:09:30 +03:00
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
// MergeAll will merge all the available remote bug:
|
|
|
|
//
|
|
|
|
// - If the remote has new commit, the local bug is updated to match the same history
|
|
|
|
// (fast-forward update)
|
|
|
|
// - if the local bug has new commits but the remote don't, nothing is changed
|
|
|
|
// - if both local and remote bug have new commits (that is, we have a concurrent edition),
|
|
|
|
// new local commits are rewritten at the head of the remote history (that is, a rebase)
|
2019-03-27 23:44:11 +03:00
|
|
|
func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeResult {
|
|
|
|
out := make(chan entity.MergeResult)
|
2018-07-23 01:04:46 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
|
|
|
|
remoteRefs, err := repo.ListRefs(remoteRefSpec)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.MergeResult{Err: err}
|
2018-07-23 01:04:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, remoteRef := range remoteRefs {
|
2019-08-12 17:12:14 +03:00
|
|
|
refSplit := strings.Split(remoteRef, "/")
|
|
|
|
id := entity.Id(refSplit[len(refSplit)-1])
|
|
|
|
|
|
|
|
if err := id.Validate(); err != nil {
|
|
|
|
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "invalid ref").Error())
|
|
|
|
continue
|
|
|
|
}
|
2018-07-23 01:04:46 +03:00
|
|
|
|
|
|
|
remoteBug, err := readBug(repo, remoteRef)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote bug is not readable").Error())
|
2018-07-23 01:04:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for error in remote data
|
2018-09-15 14:15:00 +03:00
|
|
|
if err := remoteBug.Validate(); err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote bug is invalid").Error())
|
2018-07-23 01:04:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
localRef := bugsRefPattern + remoteBug.Id().String()
|
2018-07-23 01:04:46 +03:00
|
|
|
localExist, err := repo.RefExist(localRef)
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeError(err, id)
|
2018-08-13 16:28:16 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
// the bug is not local yet, simply create the reference
|
|
|
|
if !localExist {
|
|
|
|
err := repo.CopyRef(remoteRef, localRef)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeError(err, id)
|
2018-07-23 01:04:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeStatus(entity.MergeStatusNew, id, remoteBug)
|
2018-07-23 01:04:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
localBug, err := readBug(repo, localRef)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeError(errors.Wrap(err, "local bug is not readable"), id)
|
2018-07-23 01:04:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := localBug.Merge(repo, remoteBug)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error())
|
2018-07-23 01:04:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeStatus(entity.MergeStatusUpdated, id, localBug)
|
2018-07-23 01:04:46 +03:00
|
|
|
} else {
|
2019-03-27 23:44:11 +03:00
|
|
|
out <- entity.NewMergeStatus(entity.MergeStatusNothing, id, localBug)
|
2018-07-23 01:04:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|