75 lines
3.1 KiB
Go
75 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"syscall"
|
|
|
|
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
|
|
dforgekey "git.novox.be/novox/mesh-delivery/sdk/forgekey"
|
|
)
|
|
|
|
// mirrorLocks hold one mutex per mirror of this process; the lock file beside the mirror holds off another process.
|
|
var mirrorLocks sync.Map
|
|
|
|
// mirrorDir is where the bare mirror of a repository lies: one per repository in the module's cache (ADR 0536 §2).
|
|
func (m *Module) mirrorDir(repository string) string {
|
|
return filepath.Join(m.Cache, "mirror", filepath.FromSlash(repository)+".git")
|
|
}
|
|
|
|
// fetchOptions are the options a fetch of repository runs with: the module's own, and the mirror of the repository
|
|
// brought to the commit, so git transfers only the objects the mirror lacks. A mirror that cannot be brought to the
|
|
// commit is said and left out: the fetch is then the full one, as it was.
|
|
func (m *Module) fetchOptions(ctx context.Context, repository, commit, ref string) []buildstep.Option {
|
|
opts := m.FetchOptions
|
|
if m.Cache == "" || !repositoryOK.MatchString(repository) {
|
|
return opts
|
|
}
|
|
mirror := m.mirrorDir(repository)
|
|
if err := m.updateMirror(ctx, mirror, m.remote(repository), commit, ref); err != nil {
|
|
if m.Say != nil {
|
|
m.Say("the mirror of %s is not used: %v", repository, err)
|
|
}
|
|
return opts
|
|
}
|
|
return append(append([]buildstep.Option{}, opts...), buildstep.WithMirror(mirror))
|
|
}
|
|
|
|
// updateMirror brings the mirror to the commit under the mirror's lock: a mutex for the checks of this process, a lock
|
|
// file for another process of the node.
|
|
func (m *Module) updateMirror(ctx context.Context, mirror, remote, commit, ref string) error {
|
|
mu, _ := mirrorLocks.LoadOrStore(mirror, &sync.Mutex{})
|
|
mu.(*sync.Mutex).Lock()
|
|
defer mu.(*sync.Mutex).Unlock()
|
|
if err := os.MkdirAll(mirror, 0o700); err != nil { // git init --bare takes an empty directory
|
|
return err
|
|
}
|
|
lock, err := os.OpenFile(mirror+".lock", os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer lock.Close()
|
|
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil {
|
|
return fmt.Errorf("lock %s: %w", mirror, err)
|
|
}
|
|
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
|
|
return buildstep.UpdateMirror(ctx, m.Run, mirror, remote, commit, ref, m.FetchOptions...)
|
|
}
|
|
|
|
// dissociate makes the repository in dir self-contained again, as git clone --dissociate does: the objects it borrowed
|
|
// from the mirror are packed into it and its alternates file goes, so a step shut off with the workspace alone (the
|
|
// mirror is not bound into it) finds every object. The network saving stays: the borrowing was for the transfer.
|
|
func (m *Module) dissociate(ctx context.Context, dir string) error {
|
|
alternates := filepath.Join(dir, ".git", "objects", "info", "alternates")
|
|
if _, err := os.Stat(alternates); err != nil {
|
|
return nil // no mirror was used
|
|
}
|
|
if r := m.Run(ctx, []string{"GIT_TERMINAL_PROMPT=0"}, "git", "-C", dir, "repack", "-a", "-d", "-q"); r.Status != 0 || r.Err != "" {
|
|
return fmt.Errorf("git repack in %s: %s", dir, dforgekey.Said(r.Err+"\n"+r.Stderr))
|
|
}
|
|
return os.Remove(alternates)
|
|
}
|