Adding upstream version 3.10.8.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
37e9b6d587
commit
03bfe4079e
356 changed files with 28857 additions and 0 deletions
103
forges/helpers/repository/git.go
Normal file
103
forges/helpers/repository/git.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
)
|
||||
|
||||
func GitGetSha(ctx context.Context, log logger.MessageInterface, repo, ref string) string {
|
||||
sha := util.Command(ctx, log, "git", "-C", repo, "rev-parse", ref)
|
||||
return strings.TrimSuffix(sha, "\n")
|
||||
}
|
||||
|
||||
func disableHooks(ctx context.Context, log logger.MessageInterface, dir string) func() {
|
||||
if !util.FileExists(dir) {
|
||||
return func() {}
|
||||
}
|
||||
util.Command(ctx, log, "git", "-C", dir, "config", "core.hooksPath", "/dev/null")
|
||||
return func() {
|
||||
util.Command(ctx, log, "git", "-C", dir, "config", "--unset", "core.hooksPath")
|
||||
}
|
||||
}
|
||||
|
||||
func GitMirrorRef(ctx context.Context, log logger.MessageInterface, originURL, originRef, destinationURL, destinationRef string) {
|
||||
if log == nil {
|
||||
log = logger.NewLogger()
|
||||
}
|
||||
if originURL == destinationURL {
|
||||
log.Log(1, logger.Trace, "do nothing because origin & destination are the same %s\n", originURL)
|
||||
return
|
||||
}
|
||||
log.Log(1, logger.Trace, "%s:%s => %s:%s\n", originURL, originRef, destinationURL, destinationRef)
|
||||
defer disableHooks(ctx, log, destinationURL)()
|
||||
if util.FileExists(originURL) {
|
||||
util.Command(ctx, log, "git", "-C", originURL, "push", destinationURL, "+"+originRef+":"+destinationRef)
|
||||
} else {
|
||||
if util.FileExists(destinationURL) {
|
||||
util.Command(ctx, log, "git", "-C", destinationURL, "fetch", originURL, "+"+originRef+":"+destinationRef)
|
||||
} else {
|
||||
directory, err := os.MkdirTemp("", "driverRepository")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(directory)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
util.Command(ctx, log, "git", "clone", "--bare", "--depth", "1", originURL, directory)
|
||||
util.Command(ctx, log, "git", "-C", directory, "fetch", "origin", originRef)
|
||||
util.Command(ctx, log, "git", "-C", directory, "push", destinationURL, "+FETCH_HEAD:"+destinationRef)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GitMirror(ctx context.Context, log logger.MessageInterface, origin, destination string, internalRefs []string) {
|
||||
if log == nil {
|
||||
log = logger.NewLogger()
|
||||
}
|
||||
if origin == destination {
|
||||
log.Log(1, logger.Trace, "do nothing because origin & destination are the same %s\n", origin)
|
||||
return
|
||||
}
|
||||
log.Log(1, logger.Trace, "%s => %s\n", origin, destination)
|
||||
defer disableHooks(ctx, log, destination)()
|
||||
excludeInternalRefs := make([]string, 0, len(internalRefs))
|
||||
for _, ref := range internalRefs {
|
||||
excludeInternalRefs = append(excludeInternalRefs, fmt.Sprintf("^%s", ref))
|
||||
}
|
||||
if util.FileExists(origin) {
|
||||
args := append([]string{"-C", origin, "push", destination, "+refs/*:refs/*"}, excludeInternalRefs...)
|
||||
util.Command(ctx, log, "git", args...)
|
||||
} else {
|
||||
if util.FileExists(destination) {
|
||||
util.Command(ctx, log, "git", "-C", destination, "remote", "add", "--mirror=fetch", "fetchMirror", origin)
|
||||
defer func() { util.Command(ctx, log, "git", "-C", destination, "remote", "remove", "fetchMirror") }()
|
||||
util.Command(ctx, log, "git", "-C", destination, "fetch", "fetchMirror")
|
||||
} else {
|
||||
directory, err := os.MkdirTemp("", "driverRepository")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(directory)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
util.Command(ctx, log, "git", "clone", "--mirror", origin, directory)
|
||||
args := append([]string{"-C", directory, "push", destination, "+refs/*:refs/*"}, excludeInternalRefs...)
|
||||
util.Command(ctx, log, "git", args...)
|
||||
}
|
||||
}
|
||||
}
|
33
forges/helpers/repository/git_test.go
Normal file
33
forges/helpers/repository/git_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_disableHooks(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
ctx := context.Background()
|
||||
log := logger.NewLogger()
|
||||
util.Command(ctx, log, "git", "-C", dir, "init")
|
||||
unset := "unset"
|
||||
get := func() string {
|
||||
out := util.Command(ctx, log, "git", "-C", dir, "config", "--get", "--default", unset, "core.hooksPath")
|
||||
return strings.TrimSuffix(out, "\n")
|
||||
}
|
||||
require.Equal(t, unset, get())
|
||||
enable := disableHooks(ctx, log, dir)
|
||||
require.NotEqual(t, unset, get())
|
||||
enable()
|
||||
require.Equal(t, unset, get())
|
||||
}
|
98
forges/helpers/repository/helper.go
Normal file
98
forges/helpers/repository/helper.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/f3"
|
||||
"code.forgejo.org/f3/gof3/v3/id"
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
|
||||
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
f3_tree.RepositoryDriverInterface
|
||||
Upsert(context.Context, *f3.Repository) id.NodeID
|
||||
Get(context.Context) bool
|
||||
Fetch(context.Context) string
|
||||
}
|
||||
|
||||
type repositoryInterface interface {
|
||||
logger.MessageInterface
|
||||
f3_tree.RepositoryDriverInterface
|
||||
GetNode() generic.NodeInterface
|
||||
ToFormat() f3.Interface
|
||||
SetFetchFunc(func(ctx context.Context, destination string, internalRefs []string))
|
||||
}
|
||||
|
||||
type helper struct {
|
||||
r repositoryInterface
|
||||
dir *string
|
||||
}
|
||||
|
||||
func (o *helper) getDir() string {
|
||||
if o.dir == nil {
|
||||
dir, err := os.MkdirTemp("", "repositoryHelper")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
runtime.SetFinalizer(o, func(o *helper) {
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
o.dir = &dir
|
||||
}
|
||||
return *o.dir
|
||||
}
|
||||
|
||||
func (o *helper) GetRepositoryURL() string { return o.r.GetRepositoryURL() }
|
||||
func (o *helper) GetRepositoryPushURL() string { return o.r.GetRepositoryPushURL() }
|
||||
func (o *helper) GetRepositoryInternalRefs() []string { return o.r.GetRepositoryInternalRefs() }
|
||||
|
||||
func (o *helper) Fetch(ctx context.Context) string {
|
||||
to := o.getDir()
|
||||
o.r.Trace("%s", to)
|
||||
if err := util.CommandWithErr(ctx, util.CommandOptions{}, "git", "-C", to, "rev-parse", "--is-bare-repository"); err != nil {
|
||||
o.r.Trace(util.Command(ctx, o.r, "git", "-C", to, "init", "--bare"))
|
||||
}
|
||||
from := o.r.GetRepositoryURL()
|
||||
GitMirror(ctx, o.r, from, to, []string{})
|
||||
return to
|
||||
}
|
||||
|
||||
func (o *helper) Get(ctx context.Context) bool {
|
||||
from := o.r.GetRepositoryURL()
|
||||
o.r.Trace("%s", from)
|
||||
o.r.SetFetchFunc(func(ctx context.Context, destination string, internalRefs []string) {
|
||||
o.r.Trace("git clone %s %s", from, destination)
|
||||
GitMirror(ctx, o.r, from, destination, internalRefs)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
func (o *helper) Upsert(ctx context.Context, f *f3.Repository) id.NodeID {
|
||||
if f.FetchFunc != nil {
|
||||
to := o.r.GetRepositoryPushURL()
|
||||
internalRefs := o.r.GetRepositoryInternalRefs()
|
||||
o.r.Trace("%s", to)
|
||||
f.FetchFunc(ctx, to, internalRefs)
|
||||
} else {
|
||||
o.r.Trace("NO FETCH %s", o.r.GetRepositoryPushURL())
|
||||
panic("")
|
||||
}
|
||||
return o.r.GetNode().GetID()
|
||||
}
|
||||
|
||||
func NewHelper(r repositoryInterface) Interface {
|
||||
h := &helper{r: r}
|
||||
return h
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue