Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Unified Diff: go/src/infra/libs/gitiles/object.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0b2dd51cea1a0b21aea55907c9ce94802dd20c12
--- /dev/null
+++ b/go/src/infra/libs/gitiles/object.go
@@ -0,0 +1,73 @@
+package gitiles
+
+import "fmt"
+import "net/http"
+import "strings"
+
+import "infra/libs/git"
+
+// ObjectResult is the error/git.{Blob|Commit|Tree} result you get back in
+// a channel from GetBlobFromPath
+type ObjectResult struct {
+ Err error
+ Object git.InternableObject
+}
+
+// GetObjectFromPath returns a channel yielding an ObjectResult from this Gitiles
+// service. 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) <-chan ObjectResult {
+ ret := make(chan ObjectResult, 1)
+ g.requests <- objRequest{
+ textRequest{
+ strings.Join(append([]string{"+", committish}, pathPieces...), "/"),
+ nil,
+ },
+ ret,
+ }
+ return ret
+}
+
+// Private
+
+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(riannucci): 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
+}

Powered by Google App Engine
This is Rietveld 408576698