2018-07-23 01:04:46 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-25 19:01:32 +03:00
|
|
|
"io"
|
2018-08-19 21:40:50 +03:00
|
|
|
"io/ioutil"
|
2018-07-23 01:04:46 +03:00
|
|
|
"strings"
|
2018-08-05 16:23:51 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-07-23 01:04:46 +03:00
|
|
|
)
|
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
const MsgMergeNew = "new"
|
|
|
|
const MsgMergeInvalid = "invalid data"
|
|
|
|
const MsgMergeUpdated = "updated"
|
|
|
|
const MsgMergeNothing = "nothing to do"
|
2018-07-23 01:04:46 +03:00
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// Fetch retrieve update from a remote
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
return repo.FetchRefs(remote, fetchRefSpec)
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-07-23 01:04:46 +03:00
|
|
|
return repo.PushRefs(remote, bugsRefPattern+"*")
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// Pull does a Fetch and merge the updates into the local bug states
|
2018-07-25 19:01:32 +03:00
|
|
|
func Pull(repo repository.Repo, out io.Writer, remote string) error {
|
2018-09-02 16:37:28 +03:00
|
|
|
// TODO: return a chan of changes for the cache to be updated properly
|
|
|
|
|
2018-08-19 21:40:50 +03:00
|
|
|
if out == nil {
|
|
|
|
out = ioutil.Discard
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
fmt.Fprintf(out, "Fetching remote ...\n")
|
|
|
|
|
2018-08-12 22:09:30 +03:00
|
|
|
stdout, err := Fetch(repo, remote)
|
|
|
|
if err != nil {
|
2018-07-25 19:01:32 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:09:30 +03:00
|
|
|
out.Write([]byte(stdout))
|
|
|
|
|
|
|
|
fmt.Fprintf(out, "Merging data ...\n")
|
2018-07-25 19:01:32 +03:00
|
|
|
|
|
|
|
for merge := range MergeAll(repo, remote) {
|
|
|
|
if merge.Err != nil {
|
|
|
|
return merge.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
if merge.Status != MsgMergeNothing {
|
|
|
|
fmt.Fprintf(out, "%s: %s\n", merge.HumanId, merge.Status)
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 22:09:30 +03:00
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:04:46 +03:00
|
|
|
type MergeResult struct {
|
|
|
|
Err error
|
|
|
|
|
|
|
|
Id string
|
|
|
|
HumanId string
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMergeError(id string, err error) MergeResult {
|
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
HumanId: formatHumanId(id),
|
|
|
|
Status: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMergeStatus(id string, status string) MergeResult {
|
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
HumanId: formatHumanId(id),
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
|
|
|
|
out := make(chan MergeResult)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
|
|
|
|
remoteRefs, err := repo.ListRefs(remoteRefSpec)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- MergeResult{Err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, remoteRef := range remoteRefs {
|
|
|
|
refSplitted := strings.Split(remoteRef, "/")
|
|
|
|
id := refSplitted[len(refSplitted)-1]
|
|
|
|
|
|
|
|
remoteBug, err := readBug(repo, remoteRef)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- newMergeError(id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for error in remote data
|
|
|
|
if !remoteBug.IsValid() {
|
2018-07-25 19:01:32 +03:00
|
|
|
out <- newMergeStatus(id, MsgMergeInvalid)
|
2018-07-23 01:04:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
localRef := bugsRefPattern + remoteBug.Id()
|
|
|
|
localExist, err := repo.RefExist(localRef)
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
if err != nil {
|
|
|
|
out <- newMergeError(id, err)
|
|
|
|
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 {
|
|
|
|
out <- newMergeError(id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
out <- newMergeStatus(id, MsgMergeNew)
|
2018-07-23 01:04:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
localBug, err := readBug(repo, localRef)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- newMergeError(id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := localBug.Merge(repo, remoteBug)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- newMergeError(id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
2018-07-25 19:01:32 +03:00
|
|
|
out <- newMergeStatus(id, MsgMergeUpdated)
|
2018-07-23 01:04:46 +03:00
|
|
|
} else {
|
2018-07-25 19:01:32 +03:00
|
|
|
out <- newMergeStatus(id, MsgMergeNothing)
|
2018-07-23 01:04:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|