// Copyright (C) 2024 Umorpha Systems // SPDX-License-Identifier: AGPL-3.0-or-later package gitcache import ( "errors" "os" "os/exec" "path/filepath" "sync" "time" "io" "fmt" ) const tsDir = "x-gitcache-ts" type Cache struct { Dir string MinPeriod time.Duration initOnce sync.Once initErr error } // "ll" stands for "low level" ///////////////////////////////////////////////// func (cache *Cache) llInit(stderr io.Writer) error { fmt.Fprintf(stderr, "[gitcache] Initializing cache...\n") cmd := exec.Command("git", "init", "--bare", cache.Dir) cmd.Stderr = stderr if err := cmd.Run(); err != nil { return err } if err := os.Mkdir(filepath.Join(cache.Dir, tsDir), 0o777); err != nil && !errors.Is(err, os.ErrExist) { return err } return nil } func (cache *Cache) llFetch(stderr io.Writer, namespace, url string) error { fmt.Fprintf(stderr, "[gitcache] Fetching %q...\n", url) cmd := exec.Command("git", "fetch", "--no-write-fetch-head", "--no-recurse-submodules", "--no-tags", "--prune", "--", url, "*:refs/namespaces/"+namespace+"/*") cmd.Dir = cache.Dir cmd.Stderr = stderr return cmd.Run() } //////////////////////////////////////////////////////////////////////////////// func (cache *Cache) init(stderr io.Writer) error { cache.initOnce.Do(func() { cache.initErr = cache.llInit(stderr) }) return cache.initErr } func (cache *Cache) Fetch(stderr io.Writer, url string) error { if err := cache.init(stderr); err != nil { return err } id := url2id(url) tsFile := filepath.Join(cache.Dir, tsDir, id) if cache.MinPeriod != 0 { ts, err := mtime(tsFile) if err == nil && ts.Add(cache.MinPeriod).After(time.Now()) { return nil } } if err := cache.llFetch(stderr, id, url); err != nil { return err } return touch(tsFile) } func (cache *Cache) Clone(stderr io.Writer, url, dir string, flags ...string) error { if err := cache.Fetch(stderr, url); err != nil { return err } cacheDir, err := filepath.Abs(cache.Dir) if err != nil { return err } cacheURL := "ext::git --namespace " + url2id(url) + " %s " + cacheDir fmt.Fprintf(stderr, "[gitcache] Cloning %q to %q...\n", url, dir) cmd := exec.Command("git", append(append([]string{ "-c", "protocol.ext.allow=user", "-c", "url." + cacheURL + ".insteadOf=" + url, "clone", }, flags...), "--", url, dir)...) cmd.Stderr = stderr if err := cmd.Run(); err != nil { return err } return nil } func (cache *Cache) RevParse(stderr io.Writer, url, rev string) (string, error) { if err := cache.Fetch(stderr, url); err != nil { return "", err } // `git rev-parse` doesn't obey `--namespace`, so we have to // create a temporary clone to do this :( tmpdir, err := os.MkdirTemp("", "gitcache-revparse") if err != nil { return "", err } defer os.RemoveAll(tmpdir) gitdir := filepath.Join(tmpdir, "clone.git") if err := cache.Clone(stderr, url, gitdir, "--mirror"); err != nil { return "", err } cmd := exec.Command("git", "rev-parse", "--verify", rev) cmd.Dir = gitdir cmd.Stderr = stderr out, err := cmd.Output() if err != nil { return "", err } return string(out[:len(out)-1]), nil }