This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
96 lines (82 loc) · 2.54 KB
/
git.go
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
package main
/*
#cgo pkg-config: libgit2
#include<git2.h>
// C macros seem not to be accessible in cgo
git_clone_options clone_opts_init = GIT_CLONE_OPTIONS_INIT;
int just_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
{
return git_remote_lookup(out, repo, name);
}
int just_return_repo(git_repository **out, const char *path, int bare, void *payload)
{
return git_submodule_open(out, (git_submodule*)payload);
}
*/
import "C"
import (
"errors"
"path/filepath"
"unsafe"
)
func init() {
C.git_libgit2_init()
}
func addSubmodule(url, repoDir, importPath string) error {
var repo, r *C.git_repository
var module *C.git_submodule
curDir := C.CString(".")
defer C.free(unsafe.Pointer(curDir))
defer C.git_repository_free(repo)
if C.git_repository_open(&repo, curDir) < 0 {
return convertErr(C.giterr_last())
}
cURL := C.CString(url)
defer C.free(unsafe.Pointer(cURL))
submoduleSubpath := filepath.Join("vendor", importPath)
cSubmoduleSubpath := C.CString(submoduleSubpath)
defer C.free(unsafe.Pointer(cSubmoduleSubpath))
defer C.git_submodule_free(module)
if rc := C.git_submodule_add_setup(&module, repo, cURL, cSubmoduleSubpath, 1); rc < 0 {
if rc == -4 {
// submodule exists; job is done
return nil
}
return convertErr(C.giterr_last())
}
cloneOpts := C.clone_opts_init
cloneOpts.repository_cb = (C.git_repository_create_cb)(unsafe.Pointer(C.just_return_repo))
cloneOpts.remote_cb = (C.git_remote_create_cb)(unsafe.Pointer(C.just_return_origin))
cloneOpts.repository_cb_payload = unsafe.Pointer(module)
//cloneOpts.remote_cb_payload = unsafe.Pointer(module);
cSubmoduleRepoPath := C.CString(filepath.Join(repoDir, submoduleSubpath))
defer C.free(unsafe.Pointer(cSubmoduleRepoPath))
if C.git_clone(&r, cURL, cSubmoduleRepoPath, &cloneOpts) < 0 {
return convertErr(C.giterr_last())
}
C.git_repository_free(r)
if C.git_submodule_add_finalize(module) < 0 {
return convertErr(C.giterr_last())
}
return nil
}
func findGitRoot(path string) (string, error) {
buf := &C.git_buf{}
p := C.CString(path)
defer C.free(unsafe.Pointer(p))
if C.git_repository_discover(buf, p, 0, nil) < 0 {
return "", convertErr(C.giterr_last())
}
var repo *C.git_repository
defer C.git_repository_free(repo)
if C.git_repository_open(&repo, buf.ptr) < 0 {
return "", convertErr(C.giterr_last())
}
return C.GoString(C.git_repository_workdir(repo)), nil
}
func convertErr(err *C.git_error) error {
if err != nil {
return errors.New(C.GoString(err.message))
}
return errors.New("unknown error")
}