diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-30 22:03:19 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-30 22:03:19 +0100 |
commit | 80179adaab815eaf75099a5ff31e48c6bd07d1dc (patch) | |
tree | 8973e44f42a5dc71054a4a45997a29103530ae17 /examples | |
parent | e80d7b7267ba9f2057f259be331c4de927a60ecb (diff) | |
download | go-git-80179adaab815eaf75099a5ff31e48c6bd07d1dc.tar.gz |
rename billy imports
Diffstat (limited to 'examples')
-rw-r--r-- | examples/common_test.go | 4 | ||||
-rw-r--r-- | examples/storage/aerospike/storage.go | 356 | ||||
-rw-r--r-- | examples/storage/main.go | 70 |
3 files changed, 1 insertions, 429 deletions
diff --git a/examples/common_test.go b/examples/common_test.go index 0baf2f1..357d400 100644 --- a/examples/common_test.go +++ b/examples/common_test.go @@ -24,9 +24,7 @@ var args = map[string][]string{ "push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))}, } -var ignored = map[string]bool{ - "storage": true, -} +var ignored = map[string]bool{} var tempFolders = []string{} diff --git a/examples/storage/aerospike/storage.go b/examples/storage/aerospike/storage.go deleted file mode 100644 index 712c329..0000000 --- a/examples/storage/aerospike/storage.go +++ /dev/null @@ -1,356 +0,0 @@ -package aerospike - -import ( - "encoding/json" - "fmt" - "io" - "io/ioutil" - - "gopkg.in/src-d/go-git.v4/config" - "gopkg.in/src-d/go-git.v4/plumbing" - "gopkg.in/src-d/go-git.v4/plumbing/format/index" - "gopkg.in/src-d/go-git.v4/plumbing/storer" - - driver "github.com/aerospike/aerospike-client-go" -) - -const ( - urlField = "url" - referencesSet = "reference" - configSet = "config" -) - -type Storage struct { - client *driver.Client - ns string - url string -} - -func NewStorage(client *driver.Client, ns, url string) (*Storage, error) { - if err := createIndexes(client, ns); err != nil { - return nil, err - } - - return &Storage{client: client, ns: ns, url: url}, nil -} - -func (s *Storage) NewEncodedObject() plumbing.EncodedObject { - return &plumbing.MemoryObject{} -} - -func (s *Storage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) { - key, err := s.buildKey(obj.Hash(), obj.Type()) - if err != nil { - return obj.Hash(), err - } - - r, err := obj.Reader() - if err != nil { - return obj.Hash(), err - } - - c, err := ioutil.ReadAll(r) - if err != nil { - return obj.Hash(), err - } - - bins := driver.BinMap{ - urlField: s.url, - "hash": obj.Hash().String(), - "type": obj.Type().String(), - "blob": c, - } - - err = s.client.Put(nil, key, bins) - return obj.Hash(), err -} - -func (s *Storage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { - key, err := s.buildKey(h, t) - if err != nil { - return nil, err - } - - rec, err := s.client.Get(nil, key) - if err != nil { - return nil, err - } - - if rec == nil { - return nil, plumbing.ErrObjectNotFound - } - - return objectFromRecord(rec, t) -} - -func (s *Storage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) { - stmnt := driver.NewStatement(s.ns, t.String()) - err := stmnt.Addfilter(driver.NewEqualFilter(urlField, s.url)) - - rs, err := s.client.Query(nil, stmnt) - if err != nil { - return nil, err - } - - return &EncodedObjectIter{t, rs.Records}, nil -} - -func (s *Storage) buildKey(h plumbing.Hash, t plumbing.ObjectType) (*driver.Key, error) { - return driver.NewKey(s.ns, t.String(), fmt.Sprintf("%s|%s", s.url, h.String())) -} - -type EncodedObjectIter struct { - t plumbing.ObjectType - ch chan *driver.Record -} - -func (i *EncodedObjectIter) Next() (plumbing.EncodedObject, error) { - r := <-i.ch - if r == nil { - return nil, io.EOF - } - - return objectFromRecord(r, i.t) -} - -func (i *EncodedObjectIter) ForEach(cb func(obj plumbing.EncodedObject) error) error { - for { - obj, err := i.Next() - if err != nil { - if err == io.EOF { - return nil - } - - return err - } - - if err := cb(obj); err != nil { - if err == storer.ErrStop { - return nil - } - - return err - } - } -} - -func (i *EncodedObjectIter) Close() {} - -func objectFromRecord(r *driver.Record, t plumbing.ObjectType) (plumbing.EncodedObject, error) { - content := r.Bins["blob"].([]byte) - - o := &plumbing.MemoryObject{} - o.SetType(t) - o.SetSize(int64(len(content))) - - _, err := o.Write(content) - if err != nil { - return nil, err - } - - return o, nil -} - -func (s *Storage) SetReference(ref *plumbing.Reference) error { - key, err := s.buildReferenceKey(ref.Name()) - if err != nil { - return err - } - - raw := ref.Strings() - bins := driver.BinMap{ - urlField: s.url, - "name": raw[0], - "target": raw[1], - } - - return s.client.Put(nil, key, bins) -} - -func (s *Storage) Reference(n plumbing.ReferenceName) (*plumbing.Reference, error) { - key, err := s.buildReferenceKey(n) - if err != nil { - return nil, err - } - - rec, err := s.client.Get(nil, key) - if err != nil { - return nil, err - } - - return plumbing.NewReferenceFromStrings( - rec.Bins["name"].(string), - rec.Bins["target"].(string), - ), nil -} - -func (s *Storage) buildReferenceKey(n plumbing.ReferenceName) (*driver.Key, error) { - return driver.NewKey(s.ns, referencesSet, fmt.Sprintf("%s|%s", s.url, n)) -} - -func (s *Storage) IterReferences() (storer.ReferenceIter, error) { - stmnt := driver.NewStatement(s.ns, referencesSet) - err := stmnt.Addfilter(driver.NewEqualFilter(urlField, s.url)) - if err != nil { - return nil, err - } - - rs, err := s.client.Query(nil, stmnt) - if err != nil { - return nil, err - } - - var refs []*plumbing.Reference - for r := range rs.Records { - refs = append(refs, plumbing.NewReferenceFromStrings( - r.Bins["name"].(string), - r.Bins["target"].(string), - )) - } - - return storer.NewReferenceSliceIter(refs), nil -} - -func (s *Storage) Config() (*config.Config, error) { - key, err := s.buildConfigKey() - if err != nil { - return nil, err - } - - rec, err := s.client.Get(nil, key) - if err != nil { - return nil, err - } - - c := &config.Config{} - return c, json.Unmarshal(rec.Bins["blob"].([]byte), c) -} - -func (s *Storage) SetConfig(r *config.Config) error { - key, err := s.buildConfigKey() - if err != nil { - return err - } - - json, err := json.Marshal(r) - if err != nil { - return err - } - - bins := driver.BinMap{ - urlField: s.url, - "blob": json, - } - - return s.client.Put(nil, key, bins) -} - -func (s *Storage) buildConfigKey() (*driver.Key, error) { - return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|config", s.url)) -} - -func (s *Storage) Index() (*index.Index, error) { - key, err := s.buildIndexKey() - if err != nil { - return nil, err - } - - rec, err := s.client.Get(nil, key) - if err != nil { - return nil, err - } - - idx := &index.Index{} - return idx, json.Unmarshal(rec.Bins["blob"].([]byte), idx) -} - -func (s *Storage) SetIndex(idx *index.Index) error { - key, err := s.buildIndexKey() - if err != nil { - return err - } - - json, err := json.Marshal(idx) - if err != nil { - return err - } - - bins := driver.BinMap{ - urlField: s.url, - "blob": json, - } - - return s.client.Put(nil, key, bins) -} - -func (s *Storage) buildIndexKey() (*driver.Key, error) { - return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|index", s.url)) -} - -func (s *Storage) Shallow() ([]plumbing.Hash, error) { - key, err := s.buildShallowKey() - if err != nil { - return nil, err - } - - rec, err := s.client.Get(nil, key) - if err != nil { - return nil, err - } - - var h []plumbing.Hash - return h, json.Unmarshal(rec.Bins["blob"].([]byte), h) -} - -func (s *Storage) SetShallow(hash []plumbing.Hash) error { - key, err := s.buildShallowKey() - if err != nil { - return err - } - - json, err := json.Marshal(hash) - if err != nil { - return err - } - - bins := driver.BinMap{ - urlField: s.url, - "blob": json, - } - - return s.client.Put(nil, key, bins) -} - -func (s *Storage) buildShallowKey() (*driver.Key, error) { - return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|shallow", s.url)) -} - -func createIndexes(c *driver.Client, ns string) error { - for _, set := range [...]string{ - referencesSet, - configSet, - plumbing.BlobObject.String(), - plumbing.TagObject.String(), - plumbing.TreeObject.String(), - plumbing.CommitObject.String(), - } { - if err := createIndex(c, ns, set); err != nil { - return err - } - } - - return nil -} - -func createIndex(c *driver.Client, ns, set string) error { - task, err := c.CreateIndex(nil, ns, set, set, urlField, driver.STRING) - if err != nil { - if err.Error() == "Index already exists" { - return nil - } - - return err - } - - return <-task.OnComplete() -} diff --git a/examples/storage/main.go b/examples/storage/main.go deleted file mode 100644 index 05752a1..0000000 --- a/examples/storage/main.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" - - "gopkg.in/src-d/go-git.v4" - . "gopkg.in/src-d/go-git.v4/examples" - "gopkg.in/src-d/go-git.v4/examples/storage/aerospike" - - driver "github.com/aerospike/aerospike-client-go" -) - -func main() { - CheckArgs("<clone|log>", "<url>") - action := os.Args[1] - url := os.Args[2] - - // Aerospike client to be used by the custom storage - client, err := driver.NewClient("127.0.0.1", 3000) - CheckIfError(err) - - // New instance of the custom aerospike storage, all the objects, - // references and configuration is saved to aerospike - s, err := aerospike.NewStorage(client, "test", url) - CheckIfError(err) - - switch action { - case "clone": - clone(s, url) - case "log": - log(s) - default: - panic("unknown option") - } -} - -func clone(s git.Storer, url string) { - // Clone the given repository, all the objects, references and - // configuration sush as remotes, are save into the Aerospike database - // using the custom storer - Info("git clone %s", url) - - _, err := git.Clone(s, nil, &git.CloneOptions{URL: url}) - CheckIfError(err) -} - -func log(s git.Storer) { - // We open the repository using as storer the custom implementation - r, err := git.Open(s, nil) - CheckIfError(err) - - // Prints the history of the repository starting in the current HEAD, the - // objects are retrieved from Aerospike database. - Info("git log --oneline") - - ref, err := r.Head() - CheckIfError(err) - commit, err := r.Commit(ref.Hash()) - CheckIfError(err) - commits, err := commit.History() - CheckIfError(err) - - for _, c := range commits { - hash := c.Hash.String() - line := strings.Split(c.Message, "\n") - fmt.Println(hash[:7], line[0]) - } -} |