summaryrefslogtreecommitdiffstats
path: root/git-shell-commands.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2026-07-09 17:37:42 -0700
committerRose Hogenson <rosehogenson@posteo.net>2026-07-09 17:37:42 -0700
commit79b2c0ecb76528f6002e078741dd173cfc6f2514 (patch)
tree35ce8fb72a72d70d145868d801797aab3e5215dc /git-shell-commands.go
downloadgit-shell-commands-79b2c0ecb76528f6002e078741dd173cfc6f2514.tar.zst
Initial commit
Diffstat (limited to 'git-shell-commands.go')
-rw-r--r--git-shell-commands.go105
1 files changed, 105 insertions, 0 deletions
diff --git a/git-shell-commands.go b/git-shell-commands.go
new file mode 100644
index 0000000..0e9ea44
--- /dev/null
+++ b/git-shell-commands.go
@@ -0,0 +1,105 @@
+package main
+
+import (
+ "errors"
+ "flag"
+ "fmt"
+ "io/fs"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+)
+
+const codeDir = "/nezuko/code/"
+
+func checkRepoName(name string) (string, error) {
+ if strings.Contains(name, "/") {
+ return "", fmt.Errorf("invalid name %q: contains slashes")
+ }
+ if !strings.HasSuffix(name, ".git") {
+ name += ".git"
+ }
+ return name, nil
+}
+
+func create() error {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, `Usage: create [REPO]
+Create a new git repo named REPO.
+`)
+ }
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ return fmt.Errorf("missing positional argument")
+ }
+ if len(args) > 1 {
+ return fmt.Errorf("too many positional arguments")
+ }
+ name, err := checkRepoName(args[0])
+ if err != nil {
+ return err
+ }
+ dir := filepath.Join(codeDir, name)
+ fmt.Printf("mkdir %s\n", dir)
+ if err := os.Mkdir(dir, 0755); err != nil {
+ return err
+ }
+ fmt.Printf("cd %s && git init --bare\n", dir)
+ git := exec.Command("git", "init", "--bare")
+ git.Dir = dir
+ git.Stdout = os.Stdout
+ git.Stderr = os.Stderr
+ if err := git.Run(); err != nil {
+ return err
+ }
+ return nil
+}
+
+func fileExists(path string) bool {
+ _, err := os.Stat(path)
+ return !errors.Is(err, fs.ErrNotExist)
+}
+
+func delete() error {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, `Usage: delete [REPO]
+Delete the git repo named REPO.
+`)
+ }
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ return fmt.Errorf("missing positional argument")
+ }
+ if len(args) > 1 {
+ return fmt.Errorf("too many positional arguments")
+ }
+ name, err := checkRepoName(args[0])
+ if err != nil {
+ return err
+ }
+ dir := filepath.Join(codeDir, name)
+ fmt.Printf("rm -rf %s\n", dir)
+ if !fileExists(dir) {
+ return fmt.Errorf("repo %q does not exist", name)
+ }
+ if err := os.RemoveAll(dir); err != nil {
+ return err
+ }
+ return nil
+}
+
+func main() {
+ var err error
+ if filepath.Base(os.Args[0]) == "delete" {
+ err = delete()
+ } else {
+ err = create()
+ }
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+}