git-bug/bug/bug_actions.go

163 lines
3.2 KiB
Go
Raw Normal View History

package bug
import (
"fmt"
"io"
2018-08-19 21:40:50 +03:00
"io/ioutil"
"strings"
"github.com/MichaelMure/git-bug/repository"
)
const MsgMergeNew = "new"
const MsgMergeInvalid = "invalid data"
const MsgMergeUpdated = "updated"
const MsgMergeNothing = "nothing to do"
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) {
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) {
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
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
}
fmt.Fprintf(out, "Fetching remote ...\n")
2018-08-12 22:09:30 +03:00
stdout, err := Fetch(repo, remote)
if err != nil {
return err
}
2018-08-12 22:09:30 +03:00
out.Write([]byte(stdout))
fmt.Fprintf(out, "Merging data ...\n")
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
return nil
}
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() {
out <- newMergeStatus(id, MsgMergeInvalid)
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
}
// 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
}
out <- newMergeStatus(id, MsgMergeNew)
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 {
out <- newMergeStatus(id, MsgMergeUpdated)
} else {
out <- newMergeStatus(id, MsgMergeNothing)
}
}
}()
return out
}