all repos

mugit @ cd1222ad04b72871506f59b70e65470a46a8cc75

🐮 git server that your cow will love

mugit/internal/cli/repo.go(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cli

import (
	"context"
	"fmt"
	"log/slog"
	"os"
	"strings"

	securejoin "github.com/cyphar/filepath-securejoin"
	"github.com/urfave/cli/v3"
	"olexsmir.xyz/mugit/internal/git"
)

func (c *Cli) repoNewAction(ctx context.Context, cmd *cli.Command) error {
	name := cmd.StringArg("name")
	if name == "" {
		return fmt.Errorf("no name provided")
	}

	name = strings.TrimRight(name, ".git") + ".git"

	path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name)
	if err != nil {
		return err
	}

	if _, err = os.Stat(path); err == nil {
		return fmt.Errorf("repository already exists: %s", name)
	}

	if err = git.Init(path); err != nil {
		return err
	}

	repo, err := git.Open(path, "")
	if err != nil {
		return fmt.Errorf("failed to open repo: %w", err)
	}

	if err := repo.SetPrivate(cmd.Bool("private")); err != nil {
		return fmt.Errorf("failed to set private status: %w", err)
	}

	mirrorURL := cmd.String("mirror")
	if mirrorURL != "" {
		if !strings.HasPrefix(mirrorURL, "http") {
			return fmt.Errorf("only http and https remotes are supported")
		}
		if err := repo.SetMirrorRemote(mirrorURL); err != nil {
			return fmt.Errorf("failed to set mirror remote: %w", err)
		}
	}

	return nil
}

func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error {
	name := cmd.StringArg("name")
	if name == "" {
		return fmt.Errorf("no name provided")
	}

	name = strings.TrimRight(name, ".git") + ".git"

	repo, err := c.openRepo(name)
	if err != nil {
		return fmt.Errorf("failed to open repo: %w", err)
	}

	newDesc := cmd.Args().Get(0)
	if newDesc != "" {
		if err = repo.SetDescription(newDesc); err != nil {
			return fmt.Errorf("failed to set description: %w", err)
		}
	}

	desc, err := repo.Description()
	if err != nil {
		return fmt.Errorf("failed to get description: %w", err)
	}

	slog.Info("changed repo description", "repo", name, "new_description", desc)
	return nil
}

func (c *Cli) repoPrivateAction(ctx context.Context, cmd *cli.Command) error {
	name := cmd.StringArg("name")
	if name == "" {
		return fmt.Errorf("no name provided")
	}

	name = strings.TrimRight(name, ".git") + ".git"

	repo, err := c.openRepo(name)
	if err != nil {
		return fmt.Errorf("failed to open repo: %w", err)
	}

	isPrivate, err := repo.IsPrivate()
	if err != nil {
		return fmt.Errorf("failed to get private status: %w", err)
	}

	newStatus := !isPrivate
	if err := repo.SetPrivate(newStatus); err != nil {
		return fmt.Errorf("failed to set private status: %w", err)
	}

	slog.Info("new repo private status", "repo", name, "is_private", newStatus)
	return nil
}