-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathrepository.go
More file actions
72 lines (59 loc) · 1.83 KB
/
repository.go
File metadata and controls
72 lines (59 loc) · 1.83 KB
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
package pike
import (
"fmt"
"os"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/rs/zerolog/log"
)
type gitCloneError struct {
repository string
destination string
err error
}
func (m *gitCloneError) Error() string {
return fmt.Sprintf("failed to clone repository %s %s %v", m.repository, m.destination, m.err)
}
type gitHeadError struct {
repository string
destination string
err error
}
func (m *gitHeadError) Error() string {
return fmt.Sprintf("failed to get head %s %s %v", m.repository, m.destination, m.err)
}
type gitCommitObjectError struct {
repository string
destination string
err error
}
func (m *gitCommitObjectError) Error() string {
return fmt.Sprintf("failed to get commit object %s %s %v", m.repository, m.destination, m.err)
}
func Repository(repository, destination, directory, output string, init, write, enableResources bool) error {
if _, err := os.Stat(destination); !os.IsNotExist(err) {
log.Info().Msgf("%s was not empty, removing", destination)
_ = os.RemoveAll(destination)
}
// Clone the given repository to the given directory
log.Info().Msgf("git clone %s %s --recursive", repository, destination)
r, err := git.PlainClone(destination, false, &git.CloneOptions{
URL: repository,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Depth: 1,
})
if err != nil {
return &gitCloneError{repository, destination, err}
}
// ... retrieving the branch being pointed by HEAD
ref, err := r.Head()
if err != nil {
return &gitHeadError{repository, destination, err}
}
// ... retrieving the commit object
_, err = r.CommitObject(ref.Hash())
if err != nil {
return &gitCommitObjectError{repository, destination, err}
}
return Scan(filepath.Join(destination, directory), output, nil, init, write, enableResources, "", "", "")
}