small pixel drawing of a pufferfish zoa

git/git.go

package git

import (
	"os"
	"strings"

	"j3s.sh/zoa/utils"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
)

const (
	clonePath = "/var/lib/zoa/repo"
)

func GitMode(command string) bool {
	// TODO: support file-based git remotes
	// all current remote git url types:
	//   ssh://[user@]host.xz[:port]/path/to/repo.git/
	//   git://host.xz[:port]/path/to/repo.git/
	//   http[s]://host.xz[:port]/path/to/repo.git/
	//   ftp[s]://host.xz[:port]/path/to/repo.git/
	//   [user@]host.xz:path/to/repo.git/
	if strings.Contains(command, "://") {
		// this assumption totally won't bite my ass later
		return true
	}

	// scp-mode
	if strings.Contains(command, "@") {
		return true
	}

	// todo: rewrite this and make it less shit lmao
	return false
}

func Clone(repo string, branch string) error {
	// for now, take the uber naive blow + clone
	// technique
	os.RemoveAll(clonePath)
	// Tempdir to clone the repository
	cloneopts := &git.CloneOptions{
		URL: repo,
	}
	msg := "-> cloning " + repo
	if branch != "" {
		msg += "@" + branch
		cloneopts.ReferenceName = plumbing.ReferenceName(branch)
	}
	utils.BluePrintln(msg)
	_, err := git.PlainClone(clonePath, false, cloneopts)

	return err
}