-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserve.go
More file actions
206 lines (163 loc) · 4.08 KB
/
Copy pathserve.go
File metadata and controls
206 lines (163 loc) · 4.08 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"github.com/fatih/color"
"github.com/libgit2/git2go"
"net/url"
"os"
"os/exec"
"strings"
)
func gitPushXimera(ref string) error {
args := []string{"push", "ximera"}
if len(ref) > 0 {
args = []string{"push", "ximera", ref}
}
if ref == "master" {
args = []string{"push", "--set-upstream", "ximera", ref}
}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func gitResetHard(ref string) error {
args := []string{"reset", "--hard"}
if len(ref) > 0 {
args = []string{"reset", "--hard", ref}
}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func gitResetSoft(ref string) error {
args := []string{"reset", "--soft"}
if len(ref) > 0 {
args = []string{"reset", "--soft", ref}
}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func gitResetHead() error {
args := []string{"reset", "HEAD"}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func gitFetchXimera() error {
args := []string{"fetch", "--tags", "ximera"}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func gitCheckout(ref string) error {
args := []string{"checkout", "-f"}
if len(ref) > 0 {
args = []string{"checkout", "-f", ref}
}
yellow := color.New(color.FgYellow)
yellow.Println("git " + strings.Join(args, " "))
command := exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Start()
return command.Wait()
}
func Serve() error {
log.Debug("Opening repository " + repository)
repo, err := git.OpenRepository(repository)
if err != nil {
return err
}
headReference, err := repo.Head()
if err != nil {
return err
}
sourceOid := headReference.Target()
tagName := "refs/tags/publications/" + (sourceOid.String())
_, err = repo.References.Lookup(tagName)
if err != nil {
log.Error("There is no publication tag corresponding to the repository HEAD.")
log.Error("Did you forget to perform a `xake frost` ?")
return err
}
remote, err := repo.Remotes.Lookup("ximera")
if err != nil {
log.Error("I could not find a ximera remote.")
log.Error("Did you forget to perform a `xake name` ?")
return err
}
err = gitPushXimera("")
if err != nil {
return err
}
err = gitPushXimera(tagName)
if err != nil {
return nil
}
// Produce a URL pointing to all the repo's content
u, err := url.Parse(remote.Url())
if err != nil {
return nil
}
u.User = nil
u.Path = strings.TrimSuffix(u.Path, ".git")
log.Info("The xake is served at ", u)
return nil
}
func ServePull() error {
log.Debug("Opening repository " + repository)
repo, err := git.OpenRepository(repository)
if err != nil {
return err
}
headReference, err := repo.Head()
if err != nil {
return err
}
sourceOid := headReference.Target()
_, err = repo.Remotes.Lookup("ximera")
if err != nil {
log.Error("I could not find a ximera remote.")
log.Error("Did you forget to perform a `xake name` ?")
return err
}
err = gitFetchXimera()
if err != nil {
return nil
}
tagName := "refs/tags/publications/" + (sourceOid.String())
err = gitResetHard(tagName)
if err != nil {
return nil
}
err = gitResetSoft(sourceOid.String())
if err != nil {
return nil
}
err = gitResetHead()
if err != nil {
return nil
}
log.Info("Copied published content into your working tree.")
return nil
}