aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/git.go
blob: ad3a65341f33b0f1f983dd720e05d41ae50f4a60 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package service

import (
	"errors"
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"path"
	"strings"
)

const gitDescriptionFile = "description"

func mapExecError(err error) error {
	if ee, ok := err.(*exec.ExitError); ok {
		return errors.New(string(ee.Stderr))
	}
	return err
}

// Set the description  for the projects repository.
func setDescription(repo string, desc string) error {
	descPath := path.Join(repo, gitDescriptionFile)

	var curDesc string
	buf, err := os.ReadFile(descPath)
	if os.IsNotExist(err) {
		// empty
	} else if err != nil {
		return err
	} else {
		curDesc = string(buf)
	}

	if curDesc != desc {
		err = os.WriteFile(descPath, []byte(desc), 0750)
		if err != nil {
			return err
		}
	}

	return nil
}

// Set the remote origin's URL  for the projects repository.
func setRemoteOrigin(repo string, origin *url.URL) error {
	cmd := exec.Command("git", "remote", "get-url", "origin")
	cmd.Dir = repo
	buf, err := cmd.Output()
	if err != nil {
		return fmt.Errorf("getting current project remote origin: %w", err)
	}
	currentOrigin := strings.TrimSpace(string(buf))

	if currentOrigin != origin.String() {
		cmd = exec.Command("git", "remote", "set-url", "origin", origin.String())
		cmd.Dir = repo
		err = cmd.Run()
		if err != nil {
			err = mapExecError(err)
			return fmt.Errorf("setting project remote origin: %w", err)
		}
	}

	return nil
}

func getRemoteHeadReference(repo string) (string, error) {
	cmd := exec.Command("git", "ls-remote", "--symref", "origin", "HEAD")
	cmd.Dir = repo
	buf, err := cmd.Output()
	if err != nil {
		return "", mapExecError(err)
	}

	for _, l := range strings.Split(string(buf), "\n") {
		fields := strings.Fields(l)
		if len(fields) != 3 {
			return "", errors.New("unexpected output from 'git ls-remote'")
		}
		if fields[0] == "ref:" {
			return strings.TrimPrefix(fields[1], "refs/heads/"), nil
		}
	}

	return "", errors.New("not found")
}

func MirrorGit(dst *url.URL, src *url.URL, description string) error {
	if dst.Scheme != "" && dst.Scheme != "file://" {
		return fmt.Errorf("'%s' scheme not supported", dst.Scheme)
	}

	if _, err := os.Stat(dst.Path); os.IsNotExist(err) {
		err = os.MkdirAll(path.Join(dst.Path, ".."), 0750)
		if err != nil {
			return fmt.Errorf("creating new mirror: %w", err)
		}

		cmd := exec.Command("git", "clone", "--bare", "--single-branch", src.String(), dst.String())
		cmd.Dir = path.Join(dst.Path, "..")
		err := cmd.Run()
		if err != nil {
			err = mapExecError(err)
			return fmt.Errorf("cloning: %s", err)
		}
	}

	err := setDescription(dst.Path, description)
	if err != nil {
		return err
	}

	err = setRemoteOrigin(dst.Path, src)
	if err != nil {
		return err
	}

	branch, err := getRemoteHeadReference(dst.Path)
	if err != nil {
		return fmt.Errorf("guessing remote default branch: %w", err)
	}

	cmd := exec.Command("git", "fetch", "--tags", "origin", branch+":"+branch)
	cmd.Dir = dst.Path
	err = cmd.Run()
	if err != nil {
		err = mapExecError(err)
		return fmt.Errorf("fetching project: %s", err)
	}

	return nil
}