summaryrefslogtreecommitdiff
path: root/run.go
blob: 8a9b30452aedafc0079a3704d7b0066dbeb88902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package forgepb

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// git clone (also downloads git notes)
// newdir = helloworld
// basedir = /home/jcarr/go/src/go.wit.com/apps
// giturl = https://gitea.wit.com/gui/helloworld
func RunGitClone(newdir, basedir, giturl string) error {
	log.Info("runGitClone() newdir  =", newdir)
	log.Info("runGitClone() basedir =", basedir)
	log.Info("runGitClone() giturl  =", giturl)
	if !shell.IsDir(basedir) {
		os.MkdirAll(basedir, 0755)
	}
	err := os.Chdir(basedir)
	if err != nil {
		// log.Warn("chdir failed", basedir, err)
		return err
	}

	cmd := []string{"git", "clone", "--verbose", "--progress", giturl, newdir}
	log.Info("Running:", basedir, cmd)
	r := shell.PathRunRealtime(basedir, cmd)
	if r.Error != nil {
		// log.Warn("git clone error", r.Error)
		return r.Error
	}

	fullpath := filepath.Join(basedir, newdir)
	if !shell.IsDir(fullpath) {
		// log.Info("git clone failed", giturl)
		return fmt.Errorf("git clone %s failed to create a directory", giturl)
	}
	gitdir := filepath.Join(fullpath, ".git")
	if shell.IsDir(gitdir) {
		// log.Info("git cloned worked to", fullpath)
		// also clone notes -- this can store the go.mod and go.sum files
		cmd := []string{"git", "fetch", "origin", "refs/notes/*:refs/notes/*"}
		shell.PathRunRealtime(fullpath, cmd)
		return nil
	}
	// git clone didn't really work but did make a directory
	log.Info("fullpath is probably empty", fullpath)
	return errors.New("crapnuts. rmdir fullpath here? " + fullpath)
}