forked from fluxcd/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking.go
174 lines (147 loc) · 4.32 KB
/
working.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
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
package git
import (
"context"
"errors"
"os"
"path/filepath"
)
var (
ErrReadOnly = errors.New("cannot make a working clone of a read-only git repo")
)
// Config holds some values we use when working in the working clone of
// a repo.
type Config struct {
Branch string // branch we're syncing to
Path string // path within the repo containing files we care about
SyncTag string
NotesRef string
UserName string
UserEmail string
SetAuthor bool
SkipMessage string
}
// Checkout is a local working clone of the remote repo. It is
// intended to be used for one-off "transactions", e.g,. committing
// changes then pushing upstream. It has no locking.
type Checkout struct {
dir string
config Config
upstream Remote
realNotesRef string // cache the notes ref, since we use it to push as well
}
type Commit struct {
Revision string
Message string
}
// CommitAction - struct holding commit information
type CommitAction struct {
Author string
Message string
}
// Clone returns a local working clone of the sync'ed `*Repo`, using
// the config given.
func (r *Repo) Clone(ctx context.Context, conf Config) (*Checkout, error) {
if r.readonly {
return nil, ErrReadOnly
}
upstream := r.Origin()
repoDir, err := r.workingClone(ctx, conf.Branch)
if err != nil {
return nil, err
}
if err := config(ctx, repoDir, conf.UserName, conf.UserEmail); err != nil {
os.RemoveAll(repoDir)
return nil, err
}
// We'll need the notes ref for pushing it, so make sure we have
// it. This assumes we're syncing it (otherwise we'll likely get conflicts)
realNotesRef, err := getNotesRef(ctx, repoDir, conf.NotesRef)
if err != nil {
os.RemoveAll(repoDir)
return nil, err
}
r.mu.RLock()
if err := fetch(ctx, repoDir, r.dir, realNotesRef+":"+realNotesRef); err != nil {
os.RemoveAll(repoDir)
r.mu.RUnlock()
return nil, err
}
r.mu.RUnlock()
return &Checkout{
dir: repoDir,
upstream: upstream,
realNotesRef: realNotesRef,
config: conf,
}, nil
}
// Clean a Checkout up (remove the clone)
func (c *Checkout) Clean() {
if c.dir != "" {
os.RemoveAll(c.dir)
}
}
// Dir returns the path to the repo
func (c *Checkout) Dir() string {
return c.dir
}
// ManifestDir returns the path to the manifests files
func (c *Checkout) ManifestDir() string {
return filepath.Join(c.dir, c.config.Path)
}
// CommitAndPush commits changes made in this checkout, along with any
// extra data as a note, and pushes the commit and note to the remote repo.
func (c *Checkout) CommitAndPush(ctx context.Context, commitAction CommitAction, note interface{}) error {
if !check(ctx, c.dir, c.config.Path) {
return ErrNoChanges
}
commitAction.Message += c.config.SkipMessage
if err := commit(ctx, c.dir, commitAction); err != nil {
return err
}
if note != nil {
rev, err := refRevision(ctx, c.dir, "HEAD")
if err != nil {
return err
}
if err := addNote(ctx, c.dir, rev, c.config.NotesRef, note); err != nil {
return err
}
}
refs := []string{c.config.Branch}
ok, err := refExists(ctx, c.dir, c.realNotesRef)
if ok {
refs = append(refs, c.realNotesRef)
} else if err != nil {
return err
}
if err := push(ctx, c.dir, c.upstream.URL, refs); err != nil {
return PushError(c.upstream.URL, err)
}
return nil
}
// GetNote gets a note for the revision specified, or nil if there is no such note.
func (c *Checkout) GetNote(ctx context.Context, rev string, note interface{}) (bool, error) {
return getNote(ctx, c.dir, c.realNotesRef, rev, note)
}
func (c *Checkout) HeadRevision(ctx context.Context) (string, error) {
return refRevision(ctx, c.dir, "HEAD")
}
func (c *Checkout) SyncRevision(ctx context.Context) (string, error) {
return refRevision(ctx, c.dir, c.config.SyncTag)
}
func (c *Checkout) MoveSyncTagAndPush(ctx context.Context, ref, msg string) error {
return moveTagAndPush(ctx, c.dir, c.config.SyncTag, ref, msg, c.upstream.URL)
}
// ChangedFiles does a git diff listing changed files
func (c *Checkout) ChangedFiles(ctx context.Context, ref string) ([]string, error) {
list, err := changedFiles(ctx, c.dir, c.config.Path, ref)
if err == nil {
for i, file := range list {
list[i] = filepath.Join(c.dir, file)
}
}
return list, err
}
func (c *Checkout) NoteRevList(ctx context.Context) (map[string]struct{}, error) {
return noteRevList(ctx, c.dir, c.realNotesRef)
}