| 1 |
package transactional |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 7 |
"github.com/go-git/go-git/v5/storage" |
| 8 |
) |
| 9 |
|
| 10 |
// Storage is a transactional implementation of git.Storer, it demux the write |
| 11 |
// and read operation of two separate storers, allowing to merge content calling |
| 12 |
// Storage.Commit. |
| 13 |
// |
| 14 |
// The API and functionality of this package are considered EXPERIMENTAL and is |
| 15 |
// not considered stable nor production ready. |
| 16 |
type Storage interface { |
| 17 |
storage.Storer |
| 18 |
Commit() error |
| 19 |
} |
| 20 |
|
| 21 |
// basic implements the Storage interface. |
| 22 |
type basic struct { |
| 23 |
s, temporal storage.Storer |
| 24 |
|
| 25 |
*ObjectStorage |
| 26 |
*ReferenceStorage |
| 27 |
*IndexStorage |
| 28 |
*ShallowStorage |
| 29 |
*ConfigStorage |
| 30 |
} |
| 31 |
|
| 32 |
// packageWriter implements storer.PackfileWriter interface over |
| 33 |
// a Storage with a temporal storer that supports it. |
| 34 |
type packageWriter struct { |
| 35 |
*basic |
| 36 |
pw storer.PackfileWriter |
| 37 |
} |
| 38 |
|
| 39 |
// NewStorage returns a new Storage based on two repositories, base is the base |
| 40 |
// repository where the read operations are read and temporal is were all |
| 41 |
// the write operations are stored. |
| 42 |
func NewStorage(base, temporal storage.Storer) Storage { |
| 43 |
st := &basic{ |
| 44 |
s: base, |
| 45 |
temporal: temporal, |
| 46 |
|
| 47 |
ObjectStorage: NewObjectStorage(base, temporal), |
| 48 |
ReferenceStorage: NewReferenceStorage(base, temporal), |
| 49 |
IndexStorage: NewIndexStorage(base, temporal), |
| 50 |
ShallowStorage: NewShallowStorage(base, temporal), |
| 51 |
ConfigStorage: NewConfigStorage(base, temporal), |
| 52 |
} |
| 53 |
|
| 54 |
pw, ok := temporal.(storer.PackfileWriter) |
| 55 |
if ok { |
| 56 |
return &packageWriter{ |
| 57 |
basic: st, |
| 58 |
pw: pw, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return st |
| 63 |
} |
| 64 |
|
| 65 |
// Module it honors the storage.ModuleStorer interface. |
| 66 |
func (s *basic) Module(name string) (storage.Storer, error) { |
| 67 |
base, err := s.s.Module(name) |
| 68 |
if err != nil { |
| 69 |
return nil, err |
| 70 |
} |
| 71 |
|
| 72 |
temporal, err := s.temporal.Module(name) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
|
| 77 |
return NewStorage(base, temporal), nil |
| 78 |
} |
| 79 |
|
| 80 |
// Commit it copies the content of the temporal storage into the base storage. |
| 81 |
func (s *basic) Commit() error { |
| 82 |
for _, c := range []interface{ Commit() error }{ |
| 83 |
s.ObjectStorage, |
| 84 |
s.ReferenceStorage, |
| 85 |
s.IndexStorage, |
| 86 |
s.ShallowStorage, |
| 87 |
s.ConfigStorage, |
| 88 |
} { |
| 89 |
if err := c.Commit(); err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
// PackfileWriter honors storage.PackfileWriter. |
| 98 |
func (s *packageWriter) PackfileWriter() (io.WriteCloser, error) { |
| 99 |
return s.pw.PackfileWriter() |
| 100 |
} |