2018-11-21 20:56:12 +03:00
|
|
|
// Package identity contains the identity data model and low-level related functions
|
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
2019-01-16 23:23:49 +03:00
|
|
|
"encoding/json"
|
2018-11-21 20:56:12 +03:00
|
|
|
"fmt"
|
2020-01-15 00:01:44 +03:00
|
|
|
"reflect"
|
2019-02-03 21:55:35 +03:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2019-06-04 01:40:32 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/timestamp"
|
2018-11-21 20:56:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const identityRefPattern = "refs/identities/"
|
2019-02-03 21:55:35 +03:00
|
|
|
const identityRemoteRefPattern = "refs/remotes/%s/identities/"
|
2018-11-21 20:56:12 +03:00
|
|
|
const versionEntryName = "version"
|
|
|
|
const identityConfigKey = "git-bug.identity"
|
|
|
|
|
2019-02-16 15:48:46 +03:00
|
|
|
var ErrNonFastForwardMerge = errors.New("non fast-forward identity merge")
|
2019-12-08 23:15:06 +03:00
|
|
|
var ErrNoIdentitySet = errors.New("No identity is set.\n" +
|
|
|
|
"To interact with bugs, an identity first needs to be created using " +
|
|
|
|
"\"git bug user create\"")
|
2019-02-23 15:01:46 +03:00
|
|
|
var ErrMultipleIdentitiesSet = errors.New("multiple user identities set")
|
2019-02-16 15:48:46 +03:00
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
func NewErrMultipleMatchIdentity(matching []entity.Id) *entity.ErrMultipleMatch {
|
|
|
|
return entity.NewErrMultipleMatch("identity", matching)
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
var _ Interface = &Identity{}
|
2019-08-11 15:08:03 +03:00
|
|
|
var _ entity.Interface = &Identity{}
|
2019-01-19 18:01:06 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
type Identity struct {
|
2019-02-07 00:06:42 +03:00
|
|
|
// all the successive version of the identity
|
2020-11-08 21:13:55 +03:00
|
|
|
versions []*version
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func NewIdentity(repo repository.RepoClock, name string, email string) (*Identity, error) {
|
|
|
|
return NewIdentityFull(repo, name, email, "", "", nil)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func NewIdentityFull(repo repository.RepoClock, name string, email string, login string, avatarUrl string, keys []*Key) (*Identity, error) {
|
|
|
|
v, err := newVersion(repo, name, email, login, avatarUrl, keys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
2020-11-08 21:13:55 +03:00
|
|
|
return &Identity{
|
|
|
|
versions: []*version{v},
|
|
|
|
}, nil
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 21:06:20 +03:00
|
|
|
// NewFromGitUser will query the repository for user detail and
|
|
|
|
// build the corresponding Identity
|
2020-11-08 21:13:55 +03:00
|
|
|
func NewFromGitUser(repo repository.ClockedRepo) (*Identity, error) {
|
2020-07-16 21:06:20 +03:00
|
|
|
name, err := repo.GetUserName()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`")
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := repo.GetUserEmail()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if email == "" {
|
|
|
|
return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`")
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
return NewIdentity(repo, name, email)
|
2020-07-16 21:06:20 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:23:49 +03:00
|
|
|
// MarshalJSON will only serialize the id
|
|
|
|
func (i *Identity) MarshalJSON() ([]byte, error) {
|
2019-02-01 14:22:00 +03:00
|
|
|
return json.Marshal(&IdentityStub{
|
2020-11-08 21:13:55 +03:00
|
|
|
id: i.Id(),
|
2019-01-16 23:23:49 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON will only read the id
|
|
|
|
// Users of this package are expected to run Load() to load
|
|
|
|
// the remaining data from the identities data in git.
|
|
|
|
func (i *Identity) UnmarshalJSON(data []byte) error {
|
2019-02-01 14:22:00 +03:00
|
|
|
panic("identity should be loaded with identity.UnmarshalJSON")
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
// ReadLocal load a local Identity from the identities data available in git
|
2019-08-11 15:08:03 +03:00
|
|
|
func ReadLocal(repo repository.Repo, id entity.Id) (*Identity, error) {
|
2019-02-01 14:22:00 +03:00
|
|
|
ref := fmt.Sprintf("%s%s", identityRefPattern, id)
|
2019-02-03 21:55:35 +03:00
|
|
|
return read(repo, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadRemote load a remote Identity from the identities data available in git
|
|
|
|
func ReadRemote(repo repository.Repo, remote string, id string) (*Identity, error) {
|
|
|
|
ref := fmt.Sprintf(identityRemoteRefPattern, remote) + id
|
|
|
|
return read(repo, ref)
|
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// read will load and parse an identity from git
|
2019-02-03 21:55:35 +03:00
|
|
|
func read(repo repository.Repo, ref string) (*Identity, error) {
|
2020-11-08 21:15:06 +03:00
|
|
|
id := entity.RefToId(ref)
|
2019-01-16 23:23:49 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
if err := id.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid ref")
|
2019-02-24 16:17:52 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:23:49 +03:00
|
|
|
hashes, err := repo.ListCommits(ref)
|
|
|
|
if err != nil {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, ErrIdentityNotExist
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
2020-11-08 21:13:55 +03:00
|
|
|
if len(hashes) == 0 {
|
|
|
|
return nil, fmt.Errorf("empty identity")
|
2019-02-07 00:06:42 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
i := &Identity{}
|
|
|
|
|
2019-01-16 23:23:49 +03:00
|
|
|
for _, hash := range hashes {
|
2020-06-27 23:00:15 +03:00
|
|
|
entries, err := repo.ReadTree(hash)
|
2019-01-16 23:23:49 +03:00
|
|
|
if err != nil {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, errors.Wrap(err, "can't list git tree entries")
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
if len(entries) != 1 {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, fmt.Errorf("invalid identity data at hash %s", hash)
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := entries[0]
|
|
|
|
if entry.Name != versionEntryName {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, fmt.Errorf("invalid identity data at hash %s", hash)
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := repo.ReadData(entry.Hash)
|
|
|
|
if err != nil {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, errors.Wrap(err, "failed to read git blob data")
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
var version version
|
2019-01-16 23:23:49 +03:00
|
|
|
err = json.Unmarshal(data, &version)
|
|
|
|
if err != nil {
|
2019-02-01 14:22:00 +03:00
|
|
|
return nil, errors.Wrapf(err, "failed to decode Identity version json %s", hash)
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// tag the version with the commit hash
|
|
|
|
version.commitHash = hash
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
i.versions = append(i.versions, &version)
|
2019-01-16 23:23:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
if id != i.versions[0].Id() {
|
|
|
|
return nil, fmt.Errorf("identity ID doesn't math the first version ID")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
return i, nil
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
// ListLocalIds list all the available local identity ids
|
|
|
|
func ListLocalIds(repo repository.Repo) ([]entity.Id, error) {
|
|
|
|
refs, err := repo.ListRefs(identityRefPattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity.RefsToIds(refs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveIdentity will remove a local identity from its entity.Id
|
|
|
|
func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error {
|
|
|
|
var fullMatches []string
|
|
|
|
|
|
|
|
refs, err := repo.ListRefs(identityRefPattern + id.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(refs) > 1 {
|
|
|
|
return NewErrMultipleMatchIdentity(entity.RefsToIds(refs))
|
|
|
|
}
|
|
|
|
if len(refs) == 1 {
|
|
|
|
// we have the identity locally
|
|
|
|
fullMatches = append(fullMatches, refs[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
remotes, err := repo.GetRemotes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for remote := range remotes {
|
|
|
|
remotePrefix := fmt.Sprintf(identityRemoteRefPattern+id.String(), remote)
|
|
|
|
remoteRefs, err := repo.ListRefs(remotePrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(remoteRefs) > 1 {
|
|
|
|
return NewErrMultipleMatchIdentity(entity.RefsToIds(refs))
|
|
|
|
}
|
|
|
|
if len(remoteRefs) == 1 {
|
|
|
|
// found the identity in a remote
|
|
|
|
fullMatches = append(fullMatches, remoteRefs[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fullMatches) == 0 {
|
|
|
|
return ErrIdentityNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range fullMatches {
|
|
|
|
err = repo.RemoveRef(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:48:46 +03:00
|
|
|
type StreamedIdentity struct {
|
|
|
|
Identity *Identity
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
// ReadAllLocal read and parse all local Identity
|
|
|
|
func ReadAllLocal(repo repository.ClockedRepo) <-chan StreamedIdentity {
|
|
|
|
return readAll(repo, identityRefPattern)
|
2019-02-16 15:48:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
// ReadAllRemote read and parse all remote Identity for a given remote
|
|
|
|
func ReadAllRemote(repo repository.ClockedRepo, remote string) <-chan StreamedIdentity {
|
2019-02-16 15:48:46 +03:00
|
|
|
refPrefix := fmt.Sprintf(identityRemoteRefPattern, remote)
|
2020-09-16 17:22:02 +03:00
|
|
|
return readAll(repo, refPrefix)
|
2019-02-16 15:48:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
// readAll read and parse all available bug with a given ref prefix
|
|
|
|
func readAll(repo repository.ClockedRepo, refPrefix string) <-chan StreamedIdentity {
|
2019-02-16 15:48:46 +03:00
|
|
|
out := make(chan StreamedIdentity)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
refs, err := repo.ListRefs(refPrefix)
|
|
|
|
if err != nil {
|
|
|
|
out <- StreamedIdentity{Err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range refs {
|
|
|
|
b, err := read(repo, ref)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
out <- StreamedIdentity{Err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out <- StreamedIdentity{Identity: b}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:49:33 +03:00
|
|
|
type Mutator struct {
|
|
|
|
Name string
|
2020-02-25 23:35:57 +03:00
|
|
|
Login string
|
2020-01-21 20:49:33 +03:00
|
|
|
Email string
|
|
|
|
AvatarUrl string
|
2020-01-24 02:30:13 +03:00
|
|
|
Keys []*Key
|
2020-01-21 20:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 01:25:45 +03:00
|
|
|
// Mutate allow to create a new version of the Identity in one go
|
2020-11-08 21:13:55 +03:00
|
|
|
func (i *Identity) Mutate(repo repository.RepoClock, f func(orig *Mutator)) error {
|
|
|
|
copyKeys := func(keys []*Key) []*Key {
|
|
|
|
result := make([]*Key, len(keys))
|
|
|
|
for i, key := range keys {
|
|
|
|
result[i] = key.Clone()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:49:33 +03:00
|
|
|
orig := Mutator{
|
2020-01-15 00:01:44 +03:00
|
|
|
Name: i.Name(),
|
|
|
|
Email: i.Email(),
|
2020-02-25 23:35:57 +03:00
|
|
|
Login: i.Login(),
|
2020-01-15 00:01:44 +03:00
|
|
|
AvatarUrl: i.AvatarUrl(),
|
2020-11-08 21:13:55 +03:00
|
|
|
Keys: copyKeys(i.Keys()),
|
2020-01-15 00:01:44 +03:00
|
|
|
}
|
2020-11-08 21:13:55 +03:00
|
|
|
mutated := orig
|
|
|
|
mutated.Keys = copyKeys(orig.Keys)
|
|
|
|
|
|
|
|
f(&mutated)
|
|
|
|
|
2020-01-15 00:01:44 +03:00
|
|
|
if reflect.DeepEqual(orig, mutated) {
|
2020-11-08 21:13:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := newVersion(repo,
|
|
|
|
mutated.Name,
|
|
|
|
mutated.Email,
|
|
|
|
mutated.Login,
|
|
|
|
mutated.AvatarUrl,
|
|
|
|
mutated.Keys,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.versions = append(i.versions, v)
|
|
|
|
return nil
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
// Write the identity into the Repository. In particular, this ensure that
|
|
|
|
// the Id is properly set.
|
2019-02-19 02:19:27 +03:00
|
|
|
func (i *Identity) Commit(repo repository.ClockedRepo) error {
|
2019-02-16 19:32:30 +03:00
|
|
|
if !i.NeedCommit() {
|
2019-02-16 15:48:46 +03:00
|
|
|
return fmt.Errorf("can't commit an identity with no pending version")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "can't commit an identity with invalid data")
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
var lastCommit repository.Hash
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, v := range i.versions {
|
2018-11-21 20:56:12 +03:00
|
|
|
if v.commitHash != "" {
|
2020-11-08 21:13:55 +03:00
|
|
|
lastCommit = v.commitHash
|
2019-02-19 02:19:27 +03:00
|
|
|
// ignore already commit versions
|
2018-11-21 20:56:12 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
blobHash, err := v.Write(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a git tree referencing the blob
|
|
|
|
tree := []repository.TreeEntry{
|
|
|
|
{ObjectType: repository.Blob, Hash: blobHash, Name: versionEntryName},
|
|
|
|
}
|
|
|
|
|
|
|
|
treeHash, err := repo.StoreTree(tree)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
var commitHash repository.Hash
|
2020-11-08 21:13:55 +03:00
|
|
|
if lastCommit != "" {
|
2021-01-04 01:59:25 +03:00
|
|
|
commitHash, err = repo.StoreCommit(treeHash, lastCommit)
|
2018-11-21 20:56:12 +03:00
|
|
|
} else {
|
|
|
|
commitHash, err = repo.StoreCommit(treeHash)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
lastCommit = commitHash
|
2019-02-07 00:06:42 +03:00
|
|
|
v.commitHash = commitHash
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
ref := fmt.Sprintf("%s%s", identityRefPattern, i.Id().String())
|
|
|
|
return repo.UpdateRef(ref, lastCommit)
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2019-02-19 02:19:27 +03:00
|
|
|
func (i *Identity) CommitAsNeeded(repo repository.ClockedRepo) error {
|
2019-02-16 19:32:30 +03:00
|
|
|
if !i.NeedCommit() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return i.Commit(repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Identity) NeedCommit() bool {
|
|
|
|
for _, v := range i.versions {
|
|
|
|
if v.commitHash == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
// Merge will merge a different version of the same Identity
|
|
|
|
//
|
|
|
|
// To make sure that an Identity history can't be altered, a strict fast-forward
|
|
|
|
// only policy is applied here. As an Identity should be tied to a single user, this
|
|
|
|
// should work in practice but it does leave a possibility that a user would edit his
|
|
|
|
// Identity from two different repo concurrently and push the changes in a non-centralized
|
|
|
|
// network of repositories. In this case, it would result in some of the repo accepting one
|
|
|
|
// version and some other accepting another, preventing the network in general to converge
|
|
|
|
// to the same result. This would create a sort of partition of the network, and manual
|
|
|
|
// cleaning would be required.
|
|
|
|
//
|
|
|
|
// An alternative approach would be to have a determinist rebase:
|
|
|
|
// - any commits present in both local and remote version would be kept, never changed.
|
|
|
|
// - newer commits would be merged in a linear chain of commits, ordered based on the
|
|
|
|
// Lamport time
|
|
|
|
//
|
|
|
|
// However, this approach leave the possibility, in the case of a compromised crypto keys,
|
|
|
|
// of forging a new version with a bogus Lamport time to be inserted before a legit version,
|
|
|
|
// invalidating the correct version and hijacking the Identity. There would only be a short
|
|
|
|
// period of time where this would be possible (before the network converge) but I'm not
|
|
|
|
// confident enough to implement that. I choose the strict fast-forward only approach,
|
|
|
|
// despite it's potential problem with two different version as mentioned above.
|
|
|
|
func (i *Identity) Merge(repo repository.Repo, other *Identity) (bool, error) {
|
2020-11-08 21:13:55 +03:00
|
|
|
if i.Id() != other.Id() {
|
2019-02-07 00:06:42 +03:00
|
|
|
return false, errors.New("merging unrelated identities is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
modified := false
|
2020-11-08 21:13:55 +03:00
|
|
|
var lastCommit repository.Hash
|
2019-02-07 00:06:42 +03:00
|
|
|
for j, otherVersion := range other.versions {
|
|
|
|
// if there is more version in other, take them
|
|
|
|
if len(i.versions) == j {
|
|
|
|
i.versions = append(i.versions, otherVersion)
|
2020-11-08 21:13:55 +03:00
|
|
|
lastCommit = otherVersion.commitHash
|
2019-02-07 00:06:42 +03:00
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have a non fast-forward merge.
|
|
|
|
// as explained in the doc above, refusing to merge
|
|
|
|
if i.versions[j].commitHash != otherVersion.commitHash {
|
2019-02-16 15:48:46 +03:00
|
|
|
return false, ErrNonFastForwardMerge
|
2019-02-07 00:06:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified {
|
2020-11-08 21:13:55 +03:00
|
|
|
err := repo.UpdateRef(identityRefPattern+i.Id().String(), lastCommit)
|
2019-02-07 00:06:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// Validate check if the Identity data is valid
|
|
|
|
func (i *Identity) Validate() error {
|
2020-11-08 21:13:55 +03:00
|
|
|
lastTimes := make(map[string]lamport.Time)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
if len(i.versions) == 0 {
|
|
|
|
return fmt.Errorf("no version")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, v := range i.versions {
|
2018-11-21 20:56:12 +03:00
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// check for always increasing lamport time
|
|
|
|
// check that a new version didn't drop a clock
|
|
|
|
for name, previous := range lastTimes {
|
|
|
|
if now, ok := v.times[name]; ok {
|
|
|
|
if now < previous {
|
|
|
|
return fmt.Errorf("non-chronological lamport clock %s (%d --> %d)", name, previous, now)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("version has less lamport clocks than before (missing %s)", name)
|
|
|
|
}
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
for name, now := range v.times {
|
|
|
|
lastTimes[name] = now
|
|
|
|
}
|
2019-02-16 19:32:30 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func (i *Identity) lastVersion() *version {
|
2019-02-07 00:06:42 +03:00
|
|
|
if len(i.versions) <= 0 {
|
2018-11-21 20:56:12 +03:00
|
|
|
panic("no version at all")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
return i.versions[len(i.versions)-1]
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id return the Identity identifier
|
2019-08-11 15:08:03 +03:00
|
|
|
func (i *Identity) Id() entity.Id {
|
2020-11-08 21:13:55 +03:00
|
|
|
// id is the id of the first version
|
|
|
|
return i.versions[0].Id()
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name return the last version of the name
|
|
|
|
func (i *Identity) Name() string {
|
2019-02-07 00:06:42 +03:00
|
|
|
return i.lastVersion().name
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// DisplayName return a non-empty string to display, representing the
|
|
|
|
// identity, based on the non-empty values.
|
|
|
|
func (i *Identity) DisplayName() string {
|
|
|
|
switch {
|
|
|
|
case i.Name() == "" && i.Login() != "":
|
|
|
|
return i.Login()
|
|
|
|
case i.Name() != "" && i.Login() == "":
|
|
|
|
return i.Name()
|
|
|
|
case i.Name() != "" && i.Login() != "":
|
|
|
|
return fmt.Sprintf("%s (%s)", i.Name(), i.Login())
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("invalid person data")
|
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// Email return the last version of the email
|
|
|
|
func (i *Identity) Email() string {
|
2019-02-07 00:06:42 +03:00
|
|
|
return i.lastVersion().email
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 23:35:57 +03:00
|
|
|
// Login return the last version of the login
|
|
|
|
func (i *Identity) Login() string {
|
|
|
|
return i.lastVersion().login
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
// AvatarUrl return the last version of the Avatar URL
|
2018-11-21 20:56:12 +03:00
|
|
|
func (i *Identity) AvatarUrl() string {
|
2019-02-07 00:06:42 +03:00
|
|
|
return i.lastVersion().avatarURL
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
// Keys return the last version of the valid keys
|
2020-01-24 02:30:13 +03:00
|
|
|
func (i *Identity) Keys() []*Key {
|
2019-02-07 00:06:42 +03:00
|
|
|
return i.lastVersion().keys
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
// SigningKey return the key that should be used to sign new messages. If no key is available, return nil.
|
2021-01-24 21:45:21 +03:00
|
|
|
func (i *Identity) SigningKey(repo repository.RepoKeyring) (*Key, error) {
|
2021-01-04 01:59:25 +03:00
|
|
|
keys := i.Keys()
|
2021-01-24 21:45:21 +03:00
|
|
|
for _, key := range keys {
|
|
|
|
err := key.ensurePrivateKey(repo)
|
|
|
|
if err == errNoPrivateKey {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return key, nil
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
2021-01-24 21:45:21 +03:00
|
|
|
return nil, nil
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// ValidKeysAtTime return the set of keys valid at a given lamport time
|
2020-11-08 21:13:55 +03:00
|
|
|
func (i *Identity) ValidKeysAtTime(clockName string, time lamport.Time) []*Key {
|
2020-01-24 02:30:13 +03:00
|
|
|
var result []*Key
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
var lastTime lamport.Time
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, v := range i.versions {
|
2020-11-08 21:13:55 +03:00
|
|
|
refTime, ok := v.times[clockName]
|
|
|
|
if !ok {
|
|
|
|
refTime = lastTime
|
|
|
|
}
|
|
|
|
lastTime = refTime
|
|
|
|
|
|
|
|
if refTime > time {
|
2018-11-21 20:56:12 +03:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
result = v.keys
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// LastModification return the timestamp at which the last version of the identity became valid.
|
|
|
|
func (i *Identity) LastModification() timestamp.Timestamp {
|
|
|
|
return timestamp.Timestamp(i.lastVersion().unixTime)
|
|
|
|
}
|
2020-02-25 23:35:57 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// LastModificationLamports return the lamport times at which the last version of the identity became valid.
|
|
|
|
func (i *Identity) LastModificationLamports() map[string]lamport.Time {
|
|
|
|
return i.lastVersion().times
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2019-02-25 01:05:03 +03:00
|
|
|
// IsProtected return true if the chain of git commits started to be signed.
|
|
|
|
// If that's the case, only signed commit with a valid key for this identity can be added.
|
|
|
|
func (i *Identity) IsProtected() bool {
|
|
|
|
// Todo
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// SetMetadata store arbitrary metadata along the last not-commit version.
|
|
|
|
// If the version has been commit to git already, a new identical version is added and will need to be
|
2020-01-21 20:49:33 +03:00
|
|
|
// commit.
|
2019-01-17 04:05:50 +03:00
|
|
|
func (i *Identity) SetMetadata(key string, value string) {
|
2020-11-08 21:13:55 +03:00
|
|
|
// once commit, data is immutable so we create a new version
|
2020-01-21 20:49:33 +03:00
|
|
|
if i.lastVersion().commitHash != "" {
|
2020-01-24 02:30:13 +03:00
|
|
|
i.versions = append(i.versions, i.lastVersion().Clone())
|
2020-01-21 20:49:33 +03:00
|
|
|
}
|
2020-11-08 21:13:55 +03:00
|
|
|
// if Id() has been called, we can't change the first version anymore, so we create a new version
|
|
|
|
if len(i.versions) == 1 && i.versions[0].id != entity.UnsetId && i.versions[0].id != "" {
|
|
|
|
i.versions = append(i.versions, i.lastVersion().Clone())
|
|
|
|
}
|
|
|
|
|
2019-01-17 04:05:50 +03:00
|
|
|
i.lastVersion().SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// ImmutableMetadata return all metadata for this Identity, accumulated from each version.
|
2019-01-17 04:05:50 +03:00
|
|
|
// If multiple value are found, the first defined takes precedence.
|
|
|
|
func (i *Identity) ImmutableMetadata() map[string]string {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, version := range i.versions {
|
|
|
|
for key, value := range version.metadata {
|
2019-01-17 04:05:50 +03:00
|
|
|
if _, has := metadata[key]; !has {
|
|
|
|
metadata[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// MutableMetadata return all metadata for this Identity, accumulated from each version.
|
2019-01-17 04:05:50 +03:00
|
|
|
// If multiple value are found, the last defined takes precedence.
|
|
|
|
func (i *Identity) MutableMetadata() map[string]string {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, version := range i.versions {
|
|
|
|
for key, value := range version.metadata {
|
2019-01-17 04:05:50 +03:00
|
|
|
metadata[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|