| Index: go/src/infra/libs/gitiles/object.go
|
| diff --git a/go/src/infra/libs/gitiles/object.go b/go/src/infra/libs/gitiles/object.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8160b9a9512f8ecab8b9ed1e24851deefcd2b4a7
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/gitiles/object.go
|
| @@ -0,0 +1,77 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +package gitiles
|
| +
|
| +import (
|
| + "fmt"
|
| + "net/http"
|
| + "strings"
|
| +
|
| + "infra/libs/git"
|
| +)
|
| +
|
| +// GetObjectFromPath returns the git Object at the given commit:path, or an error.
|
| +// This will hit the url:
|
| +// {g.url}/+/{committish}/path/pieces...?format=TEXT
|
| +// Passing NO pieces will return a git.Commit
|
| +// Passing A blank piece (e.g. empty string) will return a git.Tree for the root
|
| +// tree.
|
| +// Passing non-blank pieces will return a git.Tree or git.Blob depending on
|
| +// the path.
|
| +func (g *Gitiles) GetObjectFromPath(committish string, pathPieces ...string) (git.InternableObject, error) {
|
| + ret := make(chan objectResult, 1)
|
| + g.requests <- objRequest{
|
| + textRequest{
|
| + strings.Join(append([]string{"+", committish}, pathPieces...), "/"),
|
| + nil,
|
| + },
|
| + ret,
|
| + }
|
| + rslt := <-ret
|
| + return rslt.object, rslt.err
|
| +}
|
| +
|
| +// Private
|
| +
|
| +type objectResult struct {
|
| + err error
|
| + object git.InternableObject
|
| +}
|
| +
|
| +type objRequest struct {
|
| + textRequest
|
| + resultChan chan<- objectResult
|
| +}
|
| +
|
| +func (o objRequest) Process(rsp *http.Response, err error) {
|
| + var rslt objectResult
|
| +
|
| + data, err := o.internalProcess(rsp, err)
|
| + if err != nil {
|
| + rslt.err = err
|
| + } else {
|
| + typ := rsp.Header.Get("X-Gitiles-Object-Type")
|
| + switch typ {
|
| + case "blob":
|
| + rslt.object = git.BlobFromRaw(data)
|
| + case "tree":
|
| + // TODO(iannucci): When gitiles supports format=RAW, use that instead
|
| + if tree, err := git.TreeFromText(data); err != nil {
|
| + rslt.err = err
|
| + } else {
|
| + rslt.object = tree
|
| + }
|
| + case "commit":
|
| + if c, err := git.CommitFromRaw(data); err != nil {
|
| + rslt.err = err
|
| + } else {
|
| + rslt.object = c
|
| + }
|
| + default:
|
| + rslt.err = fmt.Errorf("Unknown object type %#v", typ)
|
| + }
|
| + }
|
| +
|
| + o.resultChan <- rslt
|
| +}
|
|
|