2024-01-15 22:26:51 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/serverinfo"
|
|
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
|
|
|
)
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// ReadWriteContexter is the interface required to operate on git protocols
|
2024-01-15 22:26:51 +00:00
|
|
|
type ReadWriteContexter interface {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
Context() context.Context
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:14:05 +00:00
|
|
|
type Protocoler interface {
|
|
|
|
HTTPInfoRefs(ReadWriteContexter) error
|
|
|
|
HTTPUploadPack(ReadWriteContexter) error
|
|
|
|
SSHUploadPack(ReadWriteContexter) error
|
|
|
|
SSHReceivePack(ReadWriteContexter, *Repo) error
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 19:14:05 +00:00
|
|
|
// UpdateServerInfo handles updating server info for the git repo
|
|
|
|
func UpdateServerInfo(repo string) error {
|
|
|
|
r, err := git.PlainOpen(repo)
|
2024-01-15 22:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-22 19:14:05 +00:00
|
|
|
fs := r.Storer.(*filesystem.Storage).Filesystem()
|
|
|
|
return serverinfo.UpdateServerInfo(r.Storer, fs)
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 19:14:05 +00:00
|
|
|
// HandlePushOptions handles all relevant push options for a [Repo] and saves the new [RepoMeta]
|
|
|
|
func HandlePushOptions(repo *Repo, opts []*packp.Option) error {
|
2024-01-15 22:26:51 +00:00
|
|
|
var changed bool
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch strings.ToLower(opt.Key) {
|
|
|
|
case "desc", "description":
|
|
|
|
changed = repo.Meta.Description != opt.Value
|
|
|
|
repo.Meta.Description = opt.Value
|
|
|
|
case "private":
|
|
|
|
private, err := strconv.ParseBool(opt.Value)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
changed = repo.Meta.Private != private
|
|
|
|
repo.Meta.Private = private
|
2024-07-16 17:33:41 +00:00
|
|
|
case "tags":
|
|
|
|
tagValues := strings.Split(opt.Value, ",")
|
|
|
|
for _, tagValue := range tagValues {
|
|
|
|
var remove bool
|
|
|
|
if strings.HasPrefix(tagValue, "-") {
|
|
|
|
remove = true
|
|
|
|
tagValue = strings.TrimPrefix(tagValue, "-")
|
|
|
|
}
|
|
|
|
for idx, tag := range repo.Meta.Tags {
|
|
|
|
if strings.EqualFold(tag, tagValue) {
|
|
|
|
if remove {
|
|
|
|
repo.Meta.Tags = append(repo.Meta.Tags[:idx], repo.Meta.Tags[idx+1:]...)
|
|
|
|
} else {
|
|
|
|
repo.Meta.Tags = append(repo.Meta.Tags, strings.ToLower(tagValue))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
return repo.SaveMeta()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|