| // Copyright 2016 Google Inc. All rights reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package cache |
| |
| import ( |
| "encoding/json" |
| "fmt" |
| "io/ioutil" |
| "os" |
| "path/filepath" |
| |
| "github.com/google/gitfs/gitiles" |
| git "github.com/libgit2/git2go" |
| ) |
| |
| // A TreeCache caches recursively expanded trees by their git commit and tree IDs. |
| type TreeCache struct { |
| dir string |
| } |
| |
| // NewTreeCache constructs a new TreeCache. |
| func NewTreeCache(d string) (*TreeCache, error) { |
| if err := os.MkdirAll(d, 0700); err != nil { |
| return nil, err |
| } |
| return &TreeCache{dir: d}, nil |
| } |
| |
| func (c *TreeCache) path(id *git.Oid) string { |
| str := id.String() |
| return fmt.Sprintf("%s/%s/%s", c.dir, str[:3], str[3:]) |
| } |
| |
| // Get returns a tree, if available. |
| func (c *TreeCache) Get(id *git.Oid) (*gitiles.Tree, error) { |
| content, err := ioutil.ReadFile(c.path(id)) |
| if err != nil { |
| return nil, err |
| } |
| var t gitiles.Tree |
| if err := json.Unmarshal(content, &t); err != nil { |
| return nil, err |
| } |
| |
| return &t, nil |
| } |
| |
| // Add adds a Tree to the cache |
| func (c *TreeCache) Add(id *git.Oid, tree *gitiles.Tree) error { |
| if err := c.add(id, tree); err != nil { |
| return err |
| } |
| |
| if id.String() != tree.ID { |
| treeID, err := git.NewOid(tree.ID) |
| if err != nil { |
| return err |
| } |
| return c.add(treeID, tree) |
| } |
| return nil |
| } |
| |
| func (c *TreeCache) add(id *git.Oid, tree *gitiles.Tree) error { |
| f, err := ioutil.TempFile(c.dir, "tmp") |
| if err != nil { |
| return err |
| } |
| |
| content, err := json.Marshal(tree) |
| if err != nil { |
| return err |
| } |
| if _, err := f.Write(content); err != nil { |
| return err |
| } |
| |
| if err := f.Close(); err != nil { |
| return err |
| |
| } |
| |
| dir := filepath.Dir(c.path(id)) |
| if err := os.MkdirAll(dir, 0700); err != nil { |
| return err |
| } |
| if err := os.Rename(f.Name(), c.path(id)); err != nil { |
| return err |
| } |
| return nil |
| } |
| |
| // GetTree loads the Tree from an on-disk Git repository. |
| func GetTree(repo *git.Repository, id *git.Oid) (*gitiles.Tree, error) { |
| obj, err := repo.Lookup(id) |
| if err != nil { |
| return nil, err |
| } |
| |
| obj, err = obj.Peel(git.ObjectTree) |
| if err != nil { |
| return nil, err |
| } |
| |
| asTree, err := obj.AsTree() |
| if err != nil { |
| return nil, err |
| } |
| |
| var tree gitiles.Tree |
| tree.ID = obj.Id().String() |
| |
| odb, err := repo.Odb() |
| |
| if err != nil { |
| return nil, err |
| } |
| |
| cb := func(n string, e *git.TreeEntry) int { |
| t := "" |
| var size *int |
| switch e.Type { |
| case git.ObjectTree: |
| return 0 |
| case git.ObjectCommit: |
| t = "commit" |
| case git.ObjectBlob: |
| t = "blob" |
| sz, _, rhErr := odb.ReadHeader(e.Id) |
| if rhErr != nil { |
| err = rhErr |
| return -1 |
| } |
| size = new(int) |
| *size = int(sz) |
| |
| default: |
| err = fmt.Errorf("illegal object %d for %s", e.Type, n) |
| } |
| |
| gEntry := gitiles.TreeEntry{ |
| Name: filepath.Join(n, e.Name), |
| ID: e.Id.String(), |
| Mode: int(e.Filemode), |
| Size: size, |
| Type: t, |
| } |
| if e.Filemode == git.FilemodeLink { |
| obj, lookErr := repo.Lookup(e.Id) |
| if err != nil { |
| err = lookErr |
| return -1 |
| } |
| |
| blob, blobErr := obj.AsBlob() |
| if blobErr != nil { |
| err = blobErr |
| return -1 |
| } |
| |
| target := string(blob.Contents()) |
| gEntry.Target = &target |
| } |
| |
| tree.Entries = append(tree.Entries, gEntry) |
| return 0 |
| } |
| |
| if err := asTree.Walk(cb); err != nil { |
| return nil, err |
| } |
| |
| return &tree, nil |
| } |