Repository navigation
Expand file tree
/
Copy pathpushpull.go
More file actions
157 lines (147 loc) · 3.62 KB
/
Copy pathpushpull.go
File metadata and controls
157 lines (147 loc) · 3.62 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"crypto/sha256"
"errors"
"fmt"
"log"
"os"
"path/filepath"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
func pullPushRepo(srcURL, srcUser, srcToken, dstURL, dstUser, dstToken, headRef string, verbose bool) error {
// Create a temporary repository location
id := fmt.Sprintf("%x", sha256.Sum256([]byte(srcURL)))[:8]
path := filepath.Join(os.TempDir(), id) + ".git"
if verbose {
log.Println("Local repo path is", path)
}
// Clone or fetch the original repo
var repo *git.Repository
var err error
if _, err = os.Stat(path); os.IsNotExist(err) {
repo, err = cloneRepo(path, srcURL, srcUser, srcToken, headRef, verbose)
} else {
repo, err = fetchRepo(path, srcUser, srcToken, verbose)
}
if err != nil {
return err
}
// Make sure we have a remote pointing at the destination
if err := ensureRemote(repo, "dest", dstURL, verbose); err != nil {
return err
}
// Push all refs
if err := pushAllRefs(repo, "dest", dstUser, dstToken, verbose); err != nil {
return err
}
return nil
}
func cloneRepo(path, url, user, token, headRef string, verbose bool) (*git.Repository, error) {
if verbose {
log.Println("Cloning", url, "...")
}
opts := &git.CloneOptions{
URL: url,
ReferenceName: plumbing.ReferenceName(headRef),
}
if user != "" || token != "" {
opts.Auth = &http.BasicAuth{
Username: user,
Password: token,
}
}
repo, err := git.PlainClone(path, true, opts)
if err != nil {
return nil, fmt.Errorf("git clone: %w", err)
}
return repo, nil
}
func fetchRepo(path, user, token string, verbose bool) (*git.Repository, error) {
if verbose {
log.Println("Fetching ...")
}
repo, err := git.PlainOpen(path)
if err != nil {
return nil, fmt.Errorf("open repo: %w", err)
}
opts := &git.FetchOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
"+refs/heads/*:refs/heads/*",
},
Tags: git.AllTags,
}
if user != "" || token != "" {
opts.Auth = &http.BasicAuth{
Username: user,
Password: token,
}
}
err = repo.Fetch(opts)
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, fmt.Errorf("git fetch: %w", err)
}
return repo, nil
}
func ensureRemote(repo *git.Repository, name, url string, verbose bool) error {
remotes, _ := repo.Remotes()
exists := false
for _, remote := range remotes {
cfg := remote.Config()
if cfg.Name == name {
if len(cfg.URLs) != 1 || cfg.URLs[0] != url {
if verbose {
log.Printf("Remote %q has different URL, removing", name)
}
if err := repo.DeleteRemote(name); err != nil {
return fmt.Errorf("delete remote: %w", err)
}
break
}
exists = true
break
}
}
if !exists {
if verbose {
log.Printf("Adding remote %q for URL %s", name, url)
}
_, err := repo.CreateRemote(&config.RemoteConfig{
Name: name,
URLs: []string{url},
Fetch: []config.RefSpec{
config.RefSpec("+refs/heads/*:refs/remotes/" + name + "/*"),
},
})
if err != nil {
return fmt.Errorf("create remote: %w", err)
}
}
return nil
}
func pushAllRefs(repo *git.Repository, remote, user, token string, verbose bool) error {
if verbose {
log.Println("Pushing ...")
}
opts := &git.PushOptions{
RemoteName: remote,
RefSpecs: []config.RefSpec{
"+refs/heads/*:refs/heads/*",
"+refs/tags/*:refs/tags/*",
},
}
if user != "" || token != "" {
opts.Auth = &http.BasicAuth{
Username: user,
Password: token,
}
}
err := repo.Push(opts)
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("git push: %w", err)
}
return nil
}