From acb1dc80d93f45a055b14903679abca3a6e994f6 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Sun, 29 Jan 2017 02:27:16 +0100 Subject: example: using new constructors --- examples/common_test.go | 6 ++--- examples/custom_http/main.go | 6 ++--- examples/log/main.go | 9 +++----- examples/open/main.go | 2 +- examples/push/main.go | 12 +++++----- examples/remotes/main.go | 7 +++--- examples/showcase/main.go | 9 ++++---- examples/storage/aerospike/storage.go | 41 ++++++++++++++++++++++++++++++++++- examples/storage/main.go | 21 +++++++++--------- 9 files changed, 73 insertions(+), 40 deletions(-) (limited to 'examples') diff --git a/examples/common_test.go b/examples/common_test.go index 9ec1f05..0baf2f1 100644 --- a/examples/common_test.go +++ b/examples/common_test.go @@ -16,12 +16,12 @@ var examplesTest = flag.Bool("examples", false, "run the examples tests") var defaultURL = "https://github.com/mcuadros/basic.git" var args = map[string][]string{ - "showcase": []string{defaultURL}, + "showcase": []string{defaultURL, tempFolder()}, "custom_http": []string{defaultURL}, "clone": []string{defaultURL, tempFolder()}, "progress": []string{defaultURL, tempFolder()}, - "open": []string{filepath.Join(cloneRepository(defaultURL, tempFolder()), ".git")}, - "push": []string{setEmptyRemote(filepath.Join(cloneRepository(defaultURL, tempFolder()), ".git"))}, + "open": []string{cloneRepository(defaultURL, tempFolder())}, + "push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))}, } var ignored = map[string]bool{ diff --git a/examples/custom_http/main.go b/examples/custom_http/main.go index 0853569..19f2ca3 100644 --- a/examples/custom_http/main.go +++ b/examples/custom_http/main.go @@ -11,6 +11,7 @@ import ( . "gopkg.in/src-d/go-git.v4/examples" "gopkg.in/src-d/go-git.v4/plumbing/transport/client" githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" + "gopkg.in/src-d/go-git.v4/storage/memory" ) // Here is an example to configure http client according to our own needs. @@ -37,13 +38,10 @@ func main() { // Override http(s) default protocol to use our custom client client.InstallProtocol("https", githttp.NewClient(customClient)) - // Create an in-memory repository - r := git.NewMemoryRepository() - // Clone repository using the new client if the protocol is https:// Info("git clone %s", url) - err := r.Clone(&git.CloneOptions{URL: url}) + r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url}) CheckIfError(err) // Retrieve the branch pointed by HEAD diff --git a/examples/log/main.go b/examples/log/main.go index 6ab0bb7..eb41010 100644 --- a/examples/log/main.go +++ b/examples/log/main.go @@ -5,17 +5,14 @@ import ( "gopkg.in/src-d/go-git.v4" . "gopkg.in/src-d/go-git.v4/examples" + "gopkg.in/src-d/go-git.v4/storage/memory" ) func main() { - // Instances an in-memory git repository - r := git.NewMemoryRepository() - // Clones the given repository, creating the remote, the local branches - // and fetching the objects, exactly as: + // and fetching the objects, everything in memory: Info("git clone https://github.com/src-d/go-siva") - - err := r.Clone(&git.CloneOptions{URL: "https://github.com/src-d/go-siva"}) + r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: "https://github.com/src-d/go-siva"}) CheckIfError(err) // Gets the HEAD history from HEAD, just like does: diff --git a/examples/open/main.go b/examples/open/main.go index 90fd406..1a9ea64 100644 --- a/examples/open/main.go +++ b/examples/open/main.go @@ -13,7 +13,7 @@ func main() { path := os.Args[1] // We instance a new repository targeting the given path (the .git folder) - r, err := git.NewFilesystemRepository(path) + r, err := git.PlainOpen(path) CheckIfError(err) // Length of the HEAD history diff --git a/examples/push/main.go b/examples/push/main.go index 9a30599..250e5fe 100644 --- a/examples/push/main.go +++ b/examples/push/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "gopkg.in/src-d/go-git.v4" @@ -9,14 +8,13 @@ import ( ) func main() { - CheckArgs("") - repoPath := os.Args[1] + CheckArgs("") + path := os.Args[1] - repo, err := git.NewFilesystemRepository(repoPath) + r, err := git.PlainOpen(path) CheckIfError(err) - err = repo.Push(&git.PushOptions{}) + Info("git push") + err = r.Push(&git.PushOptions{}) CheckIfError(err) - - fmt.Print("pushed") } diff --git a/examples/remotes/main.go b/examples/remotes/main.go index c0d71f5..6c9593a 100644 --- a/examples/remotes/main.go +++ b/examples/remotes/main.go @@ -7,17 +7,18 @@ import ( "gopkg.in/src-d/go-git.v4/config" . "gopkg.in/src-d/go-git.v4/examples" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/storage/memory" ) func main() { // Create a new repository Info("git init") - r := git.NewMemoryRepository() + r, err := git.Init(memory.NewStorage(), nil) + CheckIfError(err) // Add a new remote, with the default fetch refspec Info("git remote add example https://github.com/git-fixtures/basic.git") - - _, err := r.CreateRemote(&config.RemoteConfig{ + _, err = r.CreateRemote(&config.RemoteConfig{ Name: "example", URL: "https://github.com/git-fixtures/basic.git", }) diff --git a/examples/showcase/main.go b/examples/showcase/main.go index 4d1206d..0b9d82e 100644 --- a/examples/showcase/main.go +++ b/examples/showcase/main.go @@ -12,16 +12,15 @@ import ( ) func main() { - CheckArgs("") + CheckArgs(" ") url := os.Args[1] - - r := git.NewMemoryRepository() + path := os.Args[2] // Clone the given repository, creating the remote, the local branches // and fetching the objects, exactly as: - Info("git clone %s", url) + Info("git clone %s %s", url, path) - err := r.Clone(&git.CloneOptions{URL: url}) + r, err := git.PlainClone(path, false, &git.CloneOptions{URL: url}) CheckIfError(err) // Getting the latest commit on the current branch diff --git a/examples/storage/aerospike/storage.go b/examples/storage/aerospike/storage.go index 3ce5f5b..712c329 100644 --- a/examples/storage/aerospike/storage.go +++ b/examples/storage/aerospike/storage.go @@ -8,6 +8,7 @@ import ( "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" @@ -248,6 +249,44 @@ 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 { @@ -283,7 +322,7 @@ func (s *Storage) SetShallow(hash []plumbing.Hash) error { } func (s *Storage) buildShallowKey() (*driver.Key, error) { - return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|config", s.url)) + return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|shallow", s.url)) } func createIndexes(c *driver.Client, ns string) error { diff --git a/examples/storage/main.go b/examples/storage/main.go index b047e43..05752a1 100644 --- a/examples/storage/main.go +++ b/examples/storage/main.go @@ -26,30 +26,31 @@ func main() { s, err := aerospike.NewStorage(client, "test", url) CheckIfError(err) - // A new repository instance using as storage the custom implementation - r, err := git.NewRepository(s) - CheckIfError(err) - switch action { case "clone": - clone(r, url) + clone(s, url) case "log": - log(r) + log(s) default: panic("unknown option") } } -func clone(r *git.Repository, url string) { +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. + // configuration sush as remotes, are save into the Aerospike database + // using the custom storer Info("git clone %s", url) - err := r.Clone(&git.CloneOptions{URL: url}) + _, err := git.Clone(s, nil, &git.CloneOptions{URL: url}) CheckIfError(err) } -func log(r *git.Repository) { +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") -- cgit