# HG changeset patch
# Node ID e15b2f8de24e
# Parent  4027c61d978c
diff --git a/access/access.go b/access/access.go
--- a/access/access.go
+++ b/access/access.go
@@ -23,6 +23,8 @@ var (
 	adminRepoName string
 
 	hgwebConfigPath string
+
+	repositories map[string]bool
 )
 
 func Setup(repositoriesPath, adminRepo string) error {
@@ -65,6 +67,10 @@ func ReposPath() string {
 	return reposPath
 }
 
+func IsExistingRepo(repo string) bool {
+	return repositories[repo]
+}
+
 // Refresh will try to reload the casbin model and policies followed by SSH
 // keys.  If there is an error it's possible that the casbin model and polcies
 // could have been updated but the ssh keys were not.
@@ -119,3 +125,7 @@ func CanWrite(user, repo string) bool {
 func CanInit(user, repo string) bool {
 	return check(user, repo, "init")
 }
+
+func CanRemove(user, repo string) bool {
+	return check(user, repo, "remove")
+}
diff --git a/access/hgweb.go b/access/hgweb.go
--- a/access/hgweb.go
+++ b/access/hgweb.go
@@ -22,6 +22,8 @@ func refreshHgWeb() error {
 		return err
 	}
 
+	repositories = make(map[string]bool)
+
 	// walk the reposPath, looking for .hg directories, when one is found,
 	// check if it is publicly readable, and if so, add it to the config file.
 	filepath.WalkDir(absReposPath, func(filename string, entry os.DirEntry, err error) error {
@@ -40,6 +42,7 @@ func refreshHgWeb() error {
 		// include the parent directory.
 		repoPath := path.Dir(filename)
 		relativeRepoPath := strings.TrimPrefix(repoPath, absReposPath)
+		repositories[strings.Trim(relativeRepoPath, "\\/")] = true
 
 		// check if it's publicly readable
 		if CanRead("public", relativeRepoPath) {
diff --git a/hg/parser.go b/hg/parser.go
--- a/hg/parser.go
+++ b/hg/parser.go
@@ -15,6 +15,10 @@ type CommandArguments struct {
 			Repo string `kong:"arg"`
 		} `kong:"cmd"`
 	} `kong:"cmd"`
+
+	Rm struct {
+		Repo string `kong:"arg"`
+	} `kong:"cmd"`
 }
 
 func ParseCommandArguments(cmd string) (string, CommandArguments, error) {
diff --git a/setup/resources/policy.csv.template b/setup/resources/policy.csv.template
--- a/setup/resources/policy.csv.template
+++ b/setup/resources/policy.csv.template
@@ -24,6 +24,10 @@
 # give users in the admins group the ability to create repositories everywhere.
 p, admins, /*, init, allow
 
+# give users in the admins group the ability to remove repositories.
+# The admin repository /hgkeeper cannot be removed.
+#p, admins, /*, remove, allow
+
 # deny authenticated, but not explicitly defined users read access to the
 # {{.AdminRepo}} repo
 p, public, {{.AdminRepo}}, read, deny
@@ -53,5 +57,8 @@ g, {{.AdminUsername}}, admins
 # give the write action read permission
 g2, read, write
 
+# give the remove action read permission
+g2, read, remove
+
 # give the init action write permission (which has read)
 g2, write, init
diff --git a/ssh/commands/commands.go b/ssh/commands/commands.go
--- a/ssh/commands/commands.go
+++ b/ssh/commands/commands.go
@@ -39,6 +39,8 @@ func Find(cmd, reposPath string) (Comman
 		return normalize(reposPath, values.Hg.Repo, NewServe)
 	case "hg init <repo>":
 		return normalize(reposPath, values.Hg.Init.Repo, NewInit)
+	case "rm <repo>":
+		return normalize(reposPath, values.Rm.Repo, NewRemove)
 	default:
 		return nil, fmt.Errorf("unknown command %s", cmd)
 	}
diff --git a/ssh/commands/rm.go b/ssh/commands/rm.go
new file mode 100644
--- /dev/null
+++ b/ssh/commands/rm.go
@@ -0,0 +1,61 @@
+package commands
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+
+	"github.com/gliderlabs/ssh"
+	"go.uber.org/zap"
+
+	"keep.imfreedom.org/grim/hgkeeper/access"
+)
+
+type Remove struct {
+	repoPath string
+	repoName string
+}
+
+func NewRemove(reposPath, repoName string) Command {
+	return &Remove{
+		repoPath: filepath.Join(reposPath, repoName),
+		repoName: repoName,
+	}
+}
+
+func (i *Remove) Run(session ssh.Session, username string) error {
+	if !access.CanRemove(username, "/"+i.repoName) {
+		return fmt.Errorf("access denied")
+	}
+
+	err := error(nil)
+	if access.IsExistingRepo(i.repoName) {
+		err = i.removePath(session)
+	} else {
+		err = fmt.Errorf("repository not found: %s", i.repoName)
+	}
+
+	if err != nil {
+		sendResponse(err.Error(), session)
+	}
+	return err
+}
+
+func (i *Remove) removePath(session ssh.Session) error {
+	if i.repoName == access.AdminRepo() {
+		return fmt.Errorf("repository protected: %s", i.repoName)
+	}
+
+	err := os.RemoveAll(i.repoPath)
+	if err == nil {
+		sendResponse("repository removed: "+i.repoName, session)
+		zap.S().Info("refreshing access control for removed repo")
+		return access.Refresh()
+	}
+	return err
+}
+
+func sendResponse(content string, session ssh.Session) {
+	session.Write([]byte(content))
+	session.Write([]byte("\n"))
+}
