all repos

mugit @ 6ba4dfde44441460d5325c6c6a10c2329cc99da1

馃惍 git server that your cow will love
3 files changed, 56 insertions(+), 11 deletions(-)
cli: create private repo; toggle private status
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-02-06 23:49:13 +0200
Authored at: 2026-02-06 23:19:55 +0200
Change ID: tsouwxmxnuvxnnnoxqxotlruzzvpqtsz
Parent: e6fe822
M internal/cli/cli.go
路路路
        63
        63
         								Name:  "mirror",

      
        64
        64
         								Usage: "remote URL(only http/https) to mirror from",

      
        65
        65
         							},

      
        
        66
        +							&cli.BoolFlag{

      
        
        67
        +								Name:  "private",

      
        
        68
        +								Usage: "make the repository private",

      
        
        69
        +							},

      
        66
        70
         						},

      
        67
        71
         					},

      
        68
        72
         					{

      
        69
        73
         						Name:   "description",

      
        70
        74
         						Usage:  "get or set repo description",

      
        71
        75
         						Action: c.repoDescriptionAction,

      
        
        76
        +						Arguments: []cli.Argument{

      
        
        77
        +							&cli.StringArg{Name: "name"},

      
        
        78
        +						},

      
        
        79
        +					},

      
        
        80
        +					{

      
        
        81
        +						Name:   "private",

      
        
        82
        +						Usage:  "toggle private status of a repo",

      
        
        83
        +						Action: c.repoPrivateAction,

      
        72
        84
         						Arguments: []cli.Argument{

      
        73
        85
         							&cli.StringArg{Name: "name"},

      
        74
        86
         						},

      
M internal/cli/repo.go
路路路
        3
        3
         import (

      
        4
        4
         	"context"

      
        5
        5
         	"fmt"

      
        
        6
        +	"log/slog"

      
        6
        7
         	"os"

      
        7
        8
         	"strings"

      
        8
        9
         

      路路路
        24
        25
         		return err

      
        25
        26
         	}

      
        26
        27
         

      
        27
        
        -	if _, err := os.Stat(path); err == nil {

      
        
        28
        +	if _, err = os.Stat(path); err == nil {

      
        28
        29
         		return fmt.Errorf("repository already exists: %s", name)

      
        29
        30
         	}

      
        30
        31
         

      
        31
        
        -	if err := git.Init(path); err != nil {

      
        
        32
        +	if err = git.Init(path); err != nil {

      
        32
        33
         		return err

      
        33
        34
         	}

      
        34
        35
         

      
        
        36
        +	repo, err := git.Open(path, "")

      
        
        37
        +	if err != nil {

      
        
        38
        +		return fmt.Errorf("failed to open repo: %w", err)

      
        
        39
        +	}

      
        
        40
        +

      
        
        41
        +	if err := repo.SetPrivate(true); err != nil {

      
        
        42
        +		return fmt.Errorf("failed to set private status: %w", err)

      
        
        43
        +	}

      
        
        44
        +

      
        35
        45
         	mirrorURL := cmd.String("mirror")

      
        36
        46
         	if mirrorURL != "" {

      
        37
        47
         		if !strings.HasPrefix(mirrorURL, "http") {

      
        38
        48
         			return fmt.Errorf("only http and https remotes are supported")

      
        39
        
        -		}

      
        40
        
        -		repo, err := git.Open(path, "")

      
        41
        
        -		if err != nil {

      
        42
        
        -			return fmt.Errorf("failed to open repo for mirror setup: %w", err)

      
        43
        49
         		}

      
        44
        50
         		if err := repo.SetMirrorRemote(mirrorURL); err != nil {

      
        45
        51
         			return fmt.Errorf("failed to set mirror remote: %w", err)

      路路路
        64
        70
         

      
        65
        71
         	newDesc := cmd.Args().Get(0)

      
        66
        72
         	if newDesc != "" {

      
        67
        
        -		if err := repo.SetDescription(newDesc); err != nil {

      
        
        73
        +		if err = repo.SetDescription(newDesc); err != nil {

      
        68
        74
         			return fmt.Errorf("failed to set description: %w", err)

      
        69
        75
         		}

      
        70
        76
         	}

      路路路
        74
        80
         		return fmt.Errorf("failed to get description: %w", err)

      
        75
        81
         	}

      
        76
        82
         

      
        77
        
        -	if desc == "" {

      
        78
        
        -		fmt.Println("No description set")

      
        79
        
        -	} else {

      
        80
        
        -		fmt.Println(desc)

      
        
        83
        +	slog.Info("changed repo description", "repo", name, "new_description", desc)

      
        
        84
        +	return nil

      
        
        85
        +}

      
        
        86
        +

      
        
        87
        +func (c *Cli) repoPrivateAction(ctx context.Context, cmd *cli.Command) error {

      
        
        88
        +	name := cmd.StringArg("name")

      
        
        89
        +	if name == "" {

      
        
        90
        +		return fmt.Errorf("no name provided")

      
        81
        91
         	}

      
        82
        92
         

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

      
        
        94
        +

      
        
        95
        +	repo, err := c.openRepo(name)

      
        
        96
        +	if err != nil {

      
        
        97
        +		return fmt.Errorf("failed to open repo: %w", err)

      
        
        98
        +	}

      
        
        99
        +

      
        
        100
        +	isPrivate, err := repo.IsPrivate()

      
        
        101
        +	if err != nil {

      
        
        102
        +		return fmt.Errorf("failed to get private status: %w", err)

      
        
        103
        +	}

      
        
        104
        +

      
        
        105
        +	newStatus := !isPrivate

      
        
        106
        +	if err := repo.SetPrivate(newStatus); err != nil {

      
        
        107
        +		return fmt.Errorf("failed to set private status: %w", err)

      
        
        108
        +	}

      
        
        109
        +

      
        
        110
        +	slog.Info("new repo private status", "repo", name, "is_private", newStatus)

      
        83
        111
         	return nil

      
        84
        112
         }

      
M internal/git/config.go
路路路
        4
        4
         	"fmt"

      
        5
        5
         	"os"

      
        6
        6
         	"path/filepath"

      
        
        7
        +	"strconv"

      
        7
        8
         	"strings"

      
        8
        9
         	"time"

      
        9
        10
         

      路路路
        16
        17
         		return false, err

      
        17
        18
         	}

      
        18
        19
         	return v == "true", nil

      
        
        20
        +}

      
        
        21
        +

      
        
        22
        +func (g *Repo) SetPrivate(isPrivate bool) error {

      
        
        23
        +	return g.setOption("private", strconv.FormatBool(isPrivate))

      
        19
        24
         }

      
        20
        25
         

      
        21
        26
         const originRemote = "origin"