diff options
Diffstat (limited to 'bug/bug.go')
-rw-r--r-- | bug/bug.go | 55 |
1 files changed, 48 insertions, 7 deletions
@@ -109,8 +109,8 @@ func ReadLocalBug(repo repository.ClockedRepo, id entity.Id) (*Bug, error) { } // ReadRemoteBug will read a remote bug from its hash -func ReadRemoteBug(repo repository.ClockedRepo, remote string, id string) (*Bug, error) { - ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id +func ReadRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) (*Bug, error) { + ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() return readBug(repo, ref) } @@ -242,18 +242,55 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { return &bug, nil } -// RemoveLocalBug will remove a local bug from its hash +// RemoveBug will remove a local bug from its entity.Id func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { - refs, err := repo.ListRefs(id.String()) + matching := entity.Id("") + fullMatches := []string{} + + refs, err := repo.ListRefs(bugsRefPattern + id.String()) + if err != nil { + return err + } else if num := len(refs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + matching = refToId(refs[0]) + fullMatches = []string{refs[0]} + } + + remotes, err := repo.GetRemotes() if err != nil { return err } - for _, ref := range refs { + + for remote := range remotes { + remotePrefix := fmt.Sprintf(bugsRemoteRefPattern+id.String(), remote) + remoteRefs, err := repo.ListRefs(remotePrefix) + if err != nil { + return err + } else if num := len(remoteRefs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + id := refToId(remoteRefs[0]) + fullMatches = append(fullMatches, remoteRefs[0]) + if matching.String() == "" { + matching = id + } else if id != matching { + return NewErrMultipleMatchBug([]entity.Id{matching, id}) + } + } + } + + if matching == "" { + return ErrBugNotExist + } + + for _, ref := range fullMatches { err = repo.RemoveRef(ref) if err != nil { return err } } + return nil } @@ -315,13 +352,17 @@ func refsToIds(refs []string) []entity.Id { ids := make([]entity.Id, len(refs)) for i, ref := range refs { - split := strings.Split(ref, "/") - ids[i] = entity.Id(split[len(split)-1]) + ids[i] = refToId(ref) } return ids } +func refToId(ref string) entity.Id { + split := strings.Split(ref, "/") + return entity.Id(split[len(split)-1]) +} + // Validate check if the Bug data is valid func (bug *Bug) Validate() error { // non-empty |