-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
491 lines (440 loc) · 15.6 KB
/
repo.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package git
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/Nivl/git-go/backend"
"github.com/Nivl/git-go/ginternals"
"github.com/Nivl/git-go/ginternals/config"
"github.com/Nivl/git-go/ginternals/object"
"github.com/spf13/afero"
)
// List of errors returned by the Repository struct
var (
ErrRepositoryNotExist = errors.New("repository does not exist")
ErrRepositoryUnsupportedVersion = errors.New("repository nor supported")
ErrTagNotFound = errors.New("tag not found")
ErrTagExists = errors.New("tag already exists")
ErrNotADirectory = errors.New("not a directory")
ErrInvalidBranchName = errors.New("invalid branch name")
)
// Repository represent a git repository
// A Git repository is the .git/ folder inside a project.
// This repository tracks all changes made to files in your project,
// building a history over time.
// https://blog.axosoft.com/learning-git-repository/
type Repository struct {
Config *config.Config
workTree afero.Fs
dotGit *backend.Backend
shouldCleanBackend bool
}
// InitOptions contains all the optional data used to initialized a
// repository
type InitOptions struct {
// GitBackend represents the underlying backend to use to init the
// repository and interact with the odb
// By default the filesystem will be used
GitBackend *backend.Backend
// WorkingTreeBackend represents the underlying backend to use to
// interact with the working tree.
// By default the filesystem will be used
// Setting this is useless if IsBare is set to true
WorkingTreeBackend afero.Fs
// InitialBranchName represents the name of the default branch to use
// Defaults to master
InitialBranchName string
// IsBare represents whether a bare repository will be created or not
IsBare bool
// Symlink will create a .git text file in the working tree that points
// toward the actual repository
Symlink bool
}
// InitRepository initialize a new git repository by creating the .git
// directory in the given path, which is where almost everything that
// Git stores and manipulates is located.
// https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals
//
// This assumes:
// - The repo is not bare (see WithOptions)
// - We're not interested in env vars (see WithParams)
// - The git dir is in the working tree under .git
func InitRepository(workTreePath string) (*Repository, error) {
return InitRepositoryWithOptions(workTreePath, InitOptions{})
}
// InitRepositoryWithOptions initialize a new git repository by creating
// the .git directory in the given path, which is where almost everything
// that Git stores and manipulates is located.
// https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals
//
// This assumes:
// - We're not interested in env vars (see WithParams)
// - The git dir is in the working tree under .git
func InitRepositoryWithOptions(rootPath string, opts InitOptions) (r *Repository, err error) {
WorkTreePath := rootPath
GitDirPath := filepath.Join(rootPath, config.DefaultDotGitDirName)
if opts.IsBare {
WorkTreePath = ""
GitDirPath = rootPath
}
params, err := config.LoadConfigSkipEnv(config.LoadConfigOptions{
WorkTreePath: WorkTreePath,
GitDirPath: GitDirPath,
IsBare: opts.IsBare,
})
if err != nil {
return nil, fmt.Errorf("could not get the repo params: %w", err)
}
return InitRepositoryWithParams(params, opts)
}
// InitRepositoryWithParams initialize a new git repository by creating the .git
// directory in the given path, which is where almost everything that
// Git stores and manipulates is located.
// https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals
//
// This method makes no assumptions
func InitRepositoryWithParams(cfg *config.Config, opts InitOptions) (r *Repository, err error) {
r = &Repository{
Config: cfg,
}
// Validate the branch name
branchName := opts.InitialBranchName
if branchName == "" {
branchName, _ = cfg.FromFile().DefaultBranch()
if branchName == "" {
branchName = ginternals.Master
}
}
if !ginternals.IsRefNameValid(branchName) {
return nil, ErrInvalidBranchName
}
// if the repo is not bare, then we need to make sure to create
// the working tree
if !opts.IsBare {
info, err := os.Stat(cfg.WorkTreePath)
switch err { //nolint:errorlint // we only want nil or not nil
case nil:
if !info.IsDir() {
return nil, fmt.Errorf("invalid path: %w", ErrNotADirectory)
}
default:
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("could not check %s: %w", cfg.WorkTreePath, err)
}
err = os.MkdirAll(cfg.WorkTreePath, 0o755)
if err != nil {
return nil, fmt.Errorf("could not create %s: %w", cfg.WorkTreePath, err)
}
}
r.workTree = opts.WorkingTreeBackend
if r.workTree == nil {
r.workTree = afero.NewOsFs()
}
}
if opts.GitBackend == nil {
r.dotGit, err = backend.NewFS(cfg)
if err != nil {
return nil, fmt.Errorf("could not create backend: %w", err)
}
r.shouldCleanBackend = true
// we pass the repo by copy because in case of error the pointer
// will be changed to nil
defer func(r *Repository) {
if err != nil {
r.dotGit.Close() //nolint:errcheck // it already failed
}
}(r)
}
err = r.dotGit.InitWithOptions(branchName, backend.InitOptions{
CreateSymlink: opts.Symlink,
})
if err != nil {
return nil, err
}
return r, err
}
// OpenOptions contains all the optional data used to open a
// repository
type OpenOptions struct {
// GitBackend represents the underlying backend to use to init the
// repository and interact with the odb
// By default the filesystem will be used
GitBackend *backend.Backend
// WorkingTreeBackend represents the underlying backend to use to
// interact with the working tree.
// By default the filesystem will be used
// Setting this is useless if IsBare is set to true
WorkingTreeBackend afero.Fs
// GitDirPath represents the path to the .git directory
// Defaults to .git
// IsBare represents whether a bare repository will be created or not
IsBare bool
}
// OpenRepository loads an existing git repository by reading its
// config file, and returns a Repository instance
//
// This assumes:
// - The repo is not bare (see WithOptions)
// - We're not interested in env vars (see WithParams)
// - The git dir is in the working tree under .git
func OpenRepository(workTreePath string) (*Repository, error) {
return OpenRepositoryWithOptions(workTreePath, OpenOptions{})
}
// OpenRepositoryWithOptions loads an existing git repository by reading
// its config file, and returns a Repository instance
//
// This assumes:
// - We're not interested in env vars (see WithParams)
// - The git dir is in the working tree under .git
func OpenRepositoryWithOptions(rootPath string, opts OpenOptions) (r *Repository, err error) {
WorkTreePath := rootPath
GitDirPath := filepath.Join(rootPath, config.DefaultDotGitDirName)
if opts.IsBare {
WorkTreePath = ""
GitDirPath = rootPath
}
params, err := config.LoadConfigSkipEnv(config.LoadConfigOptions{
WorkTreePath: WorkTreePath,
GitDirPath: GitDirPath,
IsBare: opts.IsBare,
})
if err != nil {
return nil, fmt.Errorf("could not get the repo params: %w", err)
}
return OpenRepositoryWithParams(params, opts)
}
// OpenRepositoryWithParams loads an existing git repository by reading
// its config file, and returns a Repository instance
//
// This method makes no assumptions
func OpenRepositoryWithParams(cfg *config.Config, opts OpenOptions) (r *Repository, err error) {
r = &Repository{
Config: cfg,
}
if !opts.IsBare {
r.workTree = opts.WorkingTreeBackend
if r.workTree == nil {
r.workTree = afero.NewOsFs()
}
}
if opts.GitBackend == nil {
r.dotGit, err = backend.NewFS(cfg)
if err != nil {
return nil, fmt.Errorf("could not create backend: %w", err)
}
r.shouldCleanBackend = true
// we pass the repo by copy because in case of error the pointer
// will be chaged to nil
defer func(r *Repository) {
if err != nil {
r.dotGit.Close() //nolint:errcheck // it already failed
}
}(r)
}
// since we can't check if the directory exists on disk to
// validate if the repo exists, we're instead going to see if HEAD
// exists (since it should always be there)
_, err = r.dotGit.Reference(ginternals.Head)
if err != nil {
return nil, ErrRepositoryNotExist
}
return r, nil
}
// IsBare returns whether the repo is bare or not.
// A bare repo doesn't have a workign tree
func (r *Repository) IsBare() bool {
return r.workTree == nil
}
// Object returns the object matching the given ID
func (r *Repository) Object(oid ginternals.Oid) (*object.Object, error) {
return r.dotGit.Object(oid)
}
// NewCommit creates, stores, and returns a new Commit object
// The head of the reference $refname will be updated to this
// new commit.
// An empty refName will create a detached (loose) commit
// If the reference doesn't exists, it will be created
func (r *Repository) NewCommit(refname string, tree *object.Tree, author object.Signature, opts *object.CommitOptions) (*object.Commit, error) {
// We first validate the parents actually exists
for _, id := range opts.ParentsID {
parent, err := r.dotGit.Object(id)
if err != nil {
return nil, fmt.Errorf("could not retrieve parent %s: %w", id.String(), err)
}
if parent.Type() != object.TypeCommit {
return nil, fmt.Errorf("invalid type for parent %s. got %d, expected %d: %w", id.String(), parent.Type(), parent.Type(), object.ErrObjectInvalid)
}
}
c := object.NewCommit(tree.ID(), author, opts)
o := c.ToObject()
if _, err := r.dotGit.WriteObject(o); err != nil {
return nil, fmt.Errorf("could not write the object to the odb: %w", err)
}
// If we have a refname then we update it
if refname != "" {
ref := ginternals.NewReference(refname, o.ID())
if err := r.dotGit.WriteReference(ref); err != nil {
return nil, fmt.Errorf("could not update the HEAD of %s: %w", refname, err)
}
}
return o.AsCommit()
}
// NewDetachedCommit creates, stores, and returns a new Commit object
// not attached to any reference
func (r *Repository) NewDetachedCommit(tree *object.Tree, author object.Signature, opts *object.CommitOptions) (*object.Commit, error) {
return r.NewCommit("", tree, author, opts)
}
// Commit returns the commit matching the given SHA
func (r *Repository) Commit(oid ginternals.Oid) (*object.Commit, error) {
o, err := r.dotGit.Object(oid)
if err != nil {
return nil, fmt.Errorf("could not get object: %w", err)
}
return o.AsCommit()
}
// Tree returns the tree matching the given SHA
func (r *Repository) Tree(oid ginternals.Oid) (*object.Tree, error) {
o, err := r.dotGit.Object(oid)
if err != nil {
return nil, fmt.Errorf("could not get object: %w", err)
}
return o.AsTree()
}
// NewTag creates, stores, and returns a new annoted tag
func (r *Repository) NewTag(p *object.TagParams) (*object.Tag, error) {
found, err := r.dotGit.HasObject(p.Target.ID())
if err != nil {
return nil, fmt.Errorf("could not check if target exists: %w", err)
}
if !found {
return nil, fmt.Errorf("target doesn't exists: %w", object.ErrObjectInvalid)
}
// We first make sure the tag doesn't already exist
refname := ginternals.LocalTagFullName(p.Name)
_, err = r.dotGit.Reference(refname)
if err == nil {
return nil, ErrTagExists
}
if !errors.Is(err, ginternals.ErrRefNotFound) {
return nil, fmt.Errorf("could not check if tag already exists: %w", err)
}
// We create the tag and persist it to the object database
o := object.NewTag(p).ToObject()
if _, err := r.dotGit.WriteObject(o); err != nil {
return nil, fmt.Errorf("could not write the object to the odb: %w", err)
}
// We create the reference for the tag
ref := ginternals.NewReference(refname, o.ID())
if err := r.dotGit.WriteReference(ref); err != nil {
return nil, fmt.Errorf("could not write the ref at %s: %w", refname, err)
}
return o.AsTag()
}
// NewLightweightTag creates, stores, and returns a lightweight tag
func (r *Repository) NewLightweightTag(tag string, targetID ginternals.Oid) (*ginternals.Reference, error) {
// let's make sure the object exists
found, err := r.dotGit.HasObject(targetID)
if err != nil {
return nil, fmt.Errorf("could not retrieve targeted object: %w", err)
}
if !found {
return nil, fmt.Errorf("target : %w", object.ErrObjectInvalid)
}
refname := ginternals.LocalTagFullName(tag)
_, err = r.dotGit.Reference(refname)
if err == nil {
return nil, ErrTagExists
}
if !errors.Is(err, ginternals.ErrRefNotFound) {
return nil, fmt.Errorf("could not check if tag already exists: %w", err)
}
ref := ginternals.NewReference(refname, targetID)
if err := r.dotGit.WriteReference(ref); err != nil {
return nil, fmt.Errorf("could not write the ref at %s: %w", refname, err)
}
return ref, nil
}
// Tag returns the reference for the given tag
// To know if the tag is annoted or lightweight, call repo.GetObject()
// on the reference's target ad make sure that the returned object is
// not a tag with the same name (note that it's technically possible for
// a tag to target another tag)
func (r *Repository) Tag(name string) (*ginternals.Reference, error) {
ref, err := r.dotGit.Reference(ginternals.LocalTagFullName(name))
if err != nil {
return nil, ErrTagNotFound
}
return ref, nil
}
// NewReference creates, stores, and returns a new reference
// If the reference already exists, it will be overwritten
func (r *Repository) NewReference(name string, target ginternals.Oid) (*ginternals.Reference, error) {
ref := ginternals.NewReference(name, target)
err := r.dotGit.WriteReference(ref)
if err != nil {
return nil, err
}
return ref, nil
}
// NewReferenceSafe creates, stores, and returns a new reference
// If the reference already exists, the method will fail
func (r *Repository) NewReferenceSafe(name string, target ginternals.Oid) (*ginternals.Reference, error) {
ref := ginternals.NewReference(name, target)
err := r.dotGit.WriteReferenceSafe(ref)
if err != nil {
return nil, err
}
return ref, nil
}
// NewSymbolicReference creates, stores, and returns a new symbolic reference
// If the reference already exists, it will be overwritten
func (r *Repository) NewSymbolicReference(name, target string) (*ginternals.Reference, error) {
ref := ginternals.NewSymbolicReference(name, target)
err := r.dotGit.WriteReference(ref)
if err != nil {
return nil, err
}
return ref, nil
}
// NewSymbolicReferenceSafe creates, stores, and returns a new symbolic
// reference.
// If the reference already exists, the method will fail
func (r *Repository) NewSymbolicReferenceSafe(name, target string) (*ginternals.Reference, error) {
ref := ginternals.NewSymbolicReference(name, target)
err := r.dotGit.WriteReferenceSafe(ref)
if err != nil {
return nil, err
}
return ref, nil
}
// Reference returns the reference matching the given name
func (r *Repository) Reference(name string) (*ginternals.Reference, error) {
return r.dotGit.Reference(name)
}
// NewBlob creates, stores, and returns a new Blob object
func (r *Repository) NewBlob(data []byte) (*object.Blob, error) {
o := object.New(object.TypeBlob, data)
if _, err := r.dotGit.WriteObject(o); err != nil {
return nil, fmt.Errorf("could not write object: %w", err)
}
return object.NewBlob(o), nil
}
// Blob returns the blob matching the given ID
// This method will always work as long as the OID points to a valid
// object. Calling Blob with a commit OID, will return the raw data
// of the commit.
func (r *Repository) Blob(oid ginternals.Oid) (*object.Blob, error) {
o, err := r.dotGit.Object(oid)
if err != nil {
return nil, fmt.Errorf("could not get object: %w", err)
}
return o.AsBlob(), nil
}
// Close frees the resources used by the repository
func (r *Repository) Close() error {
if r.shouldCleanBackend {
return r.dotGit.Close()
}
return nil
}