aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2021-02-10 18:22:21 +0100
committerMichael Muré <batolettre@gmail.com>2021-02-14 12:19:03 +0100
commitef05c15f87468e0f4f1c688b0b9359cee2181c68 (patch)
treefba60b3d288ae6f6c2b8d8c18006e222c71807f5
parentf74166914c344329f08823770982f12966c79a77 (diff)
downloadgit-bug-ef05c15f87468e0f4f1c688b0b9359cee2181c68.tar.gz
entity: implement remove
-rw-r--r--entity/dag/entity_actions.go28
-rw-r--r--entity/dag/entity_actions_test.go25
-rw-r--r--repository/repo.go1
-rw-r--r--repository/repo_testing.go4
4 files changed, 56 insertions, 2 deletions
diff --git a/entity/dag/entity_actions.go b/entity/dag/entity_actions.go
index 6f6fe45c..fa50473c 100644
--- a/entity/dag/entity_actions.go
+++ b/entity/dag/entity_actions.go
@@ -228,6 +228,30 @@ func merge(def Definition, repo repository.ClockedRepo, remoteRef string, author
return entity.NewMergeUpdatedStatus(id, localEntity)
}
-func Remove() error {
- panic("")
+// Remove delete an Entity.
+// Remove is idempotent.
+func Remove(def Definition, repo repository.ClockedRepo, id entity.Id) error {
+ var matches []string
+
+ ref := fmt.Sprintf("refs/%s/%s", def.namespace, id.String())
+ matches = append(matches, ref)
+
+ remotes, err := repo.GetRemotes()
+ if err != nil {
+ return err
+ }
+
+ for remote := range remotes {
+ ref = fmt.Sprintf("refs/remotes/%s/%s/%s", remote, def.namespace, id.String())
+ matches = append(matches, ref)
+ }
+
+ for _, ref = range matches {
+ err = repo.RemoveRef(ref)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
}
diff --git a/entity/dag/entity_actions_test.go b/entity/dag/entity_actions_test.go
index 78baf41f..79afe525 100644
--- a/entity/dag/entity_actions_test.go
+++ b/entity/dag/entity_actions_test.go
@@ -385,3 +385,28 @@ func TestMerge(t *testing.T) {
// fast-forward
assertEqualRefs(t, repoA, repoB, "refs/"+def.namespace)
}
+
+func TestRemove(t *testing.T) {
+ repoA, repoB, remote, id1, _, def := makeTestContextRemote(t)
+ defer repository.CleanupTestRepos(repoA, repoB, remote)
+
+ e := New(def)
+ e.Append(newOp1(id1, "foo"))
+ require.NoError(t, e.Commit(repoA))
+
+ _, err := Push(def, repoA, "remote")
+ require.NoError(t, err)
+
+ err = Remove(def, repoA, e.Id())
+ require.NoError(t, err)
+
+ _, err = Read(def, repoA, e.Id())
+ require.Error(t, err)
+
+ _, err = readRemote(def, repoA, "remote", e.Id())
+ require.Error(t, err)
+
+ // Remove is idempotent
+ err = Remove(def, repoA, e.Id())
+ require.NoError(t, err)
+}
diff --git a/repository/repo.go b/repository/repo.go
index 8d162c6d..80bb7ce7 100644
--- a/repository/repo.go
+++ b/repository/repo.go
@@ -147,6 +147,7 @@ type RepoData interface {
UpdateRef(ref string, hash Hash) error
// RemoveRef will remove a Git reference
+ // RemoveRef is idempotent.
RemoveRef(ref string) error
// ListRefs will return a list of Git ref matching the given refspec
diff --git a/repository/repo_testing.go b/repository/repo_testing.go
index cdcb9008..1f80d898 100644
--- a/repository/repo_testing.go
+++ b/repository/repo_testing.go
@@ -202,6 +202,10 @@ func RepoDataTest(t *testing.T, repo RepoData) {
err = repo.RemoveRef("refs/bugs/ref1")
require.NoError(t, err)
+
+ // RemoveRef is idempotent
+ err = repo.RemoveRef("refs/bugs/ref1")
+ require.NoError(t, err)
}
func RepoDataSignatureTest(t *testing.T, repo RepoData) {