all repos — glog @ 90d360c23c40a30bf8b137c991ac16f79577ae5f

git.go (view raw)

 1package main
 2
 3import (
 4	"errors"
 5	"io/fs"
 6	"os"
 7
 8	"github.com/charmbracelet/log"
 9	"github.com/go-git/go-billy/v5/osfs"
10	"github.com/go-git/go-git/v5"
11	"github.com/go-git/go-git/v5/plumbing/cache"
12	"github.com/go-git/go-git/v5/storage/filesystem"
13)
14
15func getRepo(url string, path string) (*git.Repository, error) {
16	_, err := os.Stat(path)
17
18	if err == nil {
19		log.Info("Retrieve existing repo")
20		fs := osfs.New(path)
21		if _, err := fs.Stat(git.GitDirName); err == nil {
22			fs, err = fs.Chroot(git.GitDirName)
23			CheckIfError(err)
24		}
25
26		s := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true})
27		repo, err := git.Open(s, fs)
28		defer s.Close()
29		return repo, err
30
31	} else if errors.Is(err, fs.ErrNotExist) {
32		if url == "" {
33			log.Fatal("No git URL provided")
34		}
35		log.Info("Clone repo from " + url + " to " + path)
36		repo, err := git.PlainClone(path, false, &git.CloneOptions{
37			URL: url,
38		})
39		CheckIfError(err)
40		worktree, err := repo.Worktree()
41		_ = worktree.Pull(&git.PullOptions{RemoteName: "origin"})
42		return repo, err
43
44	} else {
45		return nil, err
46	}
47}