git-bug/identity/identity_actions.go

133 lines
3.3 KiB
Go
Raw Normal View History

2019-02-03 21:55:35 +03:00
package identity
import (
"fmt"
"strings"
2020-09-08 15:31:40 +03:00
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/entity"
2019-02-03 21:55:35 +03:00
"github.com/MichaelMure/git-bug/repository"
)
// Fetch retrieve updates from a remote
// This does not change the local identities state
func Fetch(repo repository.Repo, remote string) (string, error) {
// "refs/identities/*:refs/remotes/<remote>/identities/*"
2019-02-03 21:55:35 +03:00
remoteRefSpec := fmt.Sprintf(identityRemoteRefPattern, remote)
fetchRefSpec := fmt.Sprintf("%s*:%s*", identityRefPattern, remoteRefSpec)
2019-02-03 21:55:35 +03:00
return repo.FetchRefs(remote, fetchRefSpec)
}
// Push update a remote with the local changes
func Push(repo repository.Repo, remote string) (string, error) {
// "refs/identities/*:refs/identities/*"
refspec := fmt.Sprintf("%s*:%s*", identityRefPattern, identityRefPattern)
return repo.PushRefs(remote, refspec)
2019-02-03 21:55:35 +03:00
}
// Pull will do a Fetch + MergeAll
// This function will return an error if a merge fail
2019-02-03 21:55:35 +03:00
func Pull(repo repository.ClockedRepo, remote string) error {
_, err := Fetch(repo, remote)
if err != nil {
return err
}
for merge := range MergeAll(repo, remote) {
if merge.Err != nil {
return merge.Err
}
if merge.Status == entity.MergeStatusInvalid {
return errors.Errorf("merge failure: %s", merge.Reason)
2019-02-03 21:55:35 +03:00
}
}
return nil
}
// MergeAll will merge all the available remote identity
func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeResult {
out := make(chan entity.MergeResult)
2019-02-03 21:55:35 +03:00
go func() {
defer close(out)
remoteRefSpec := fmt.Sprintf(identityRemoteRefPattern, remote)
remoteRefs, err := repo.ListRefs(remoteRefSpec)
if err != nil {
out <- entity.MergeResult{Err: err}
2019-02-03 21:55:35 +03:00
return
}
for _, remoteRef := range remoteRefs {
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
}
2019-02-03 21:55:35 +03:00
remoteIdentity, err := read(repo, remoteRef)
2019-02-03 21:55:35 +03:00
if err != nil {
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote identity is not readable").Error())
2019-02-03 21:55:35 +03:00
continue
}
// Check for error in remote data
2019-02-07 00:06:42 +03:00
if err := remoteIdentity.Validate(); err != nil {
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "remote identity is invalid").Error())
2019-02-03 21:55:35 +03:00
continue
}
localRef := identityRefPattern + remoteIdentity.Id().String()
2019-02-03 21:55:35 +03:00
localExist, err := repo.RefExist(localRef)
if err != nil {
out <- entity.NewMergeError(err, id)
2019-02-03 21:55:35 +03:00
continue
}
2019-02-07 00:06:42 +03:00
// the identity is not local yet, simply create the reference
2019-02-03 21:55:35 +03:00
if !localExist {
err := repo.CopyRef(remoteRef, localRef)
if err != nil {
out <- entity.NewMergeError(err, id)
2019-02-03 21:55:35 +03:00
return
}
out <- entity.NewMergeNewStatus(id, remoteIdentity)
2019-02-03 21:55:35 +03:00
continue
}
2019-02-07 00:06:42 +03:00
localIdentity, err := read(repo, localRef)
2019-02-03 21:55:35 +03:00
if err != nil {
out <- entity.NewMergeError(errors.Wrap(err, "local identity is not readable"), id)
2019-02-03 21:55:35 +03:00
return
}
2019-02-07 00:06:42 +03:00
updated, err := localIdentity.Merge(repo, remoteIdentity)
2019-02-03 21:55:35 +03:00
if err != nil {
out <- entity.NewMergeInvalidStatus(id, errors.Wrap(err, "merge failed").Error())
2019-02-03 21:55:35 +03:00
return
}
if updated {
out <- entity.NewMergeUpdatedStatus(id, localIdentity)
2019-02-03 21:55:35 +03:00
} else {
out <- entity.NewMergeNothingStatus(id)
2019-02-03 21:55:35 +03:00
}
}
}()
return out
}