| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package gitiles |
| 6 |
| 7 import ( |
| 8 "io" |
| 9 "net/http" |
| 10 "reflect" |
| 11 "strings" |
| 12 |
| 13 "infra/libs/git" |
| 14 ) |
| 15 |
| 16 // Types /////////////////////////////////////////////////////////////////////// |
| 17 |
| 18 // Gitiles wraps one Gitiles server. Use NewGitiles to create one. |
| 19 type Gitiles struct { |
| 20 url string |
| 21 requests chan<- request |
| 22 } |
| 23 |
| 24 // Constructors /////////////////////////////////////////////////////////////// |
| 25 |
| 26 // NewGitiles creates a new Gitiles instance for the given |url|, and a maximum |
| 27 // number of concurrent requests. |
| 28 func NewGitiles(url string, maxConnections int) *Gitiles { |
| 29 // TODO(iannucci): make a separate http.Client? |
| 30 // TODO(iannucci): have a way to destroy the Gitiles instance? |
| 31 requestChan := make(chan request, maxConnections) |
| 32 ret := &Gitiles{ |
| 33 url: strings.TrimRight(url, "/"), |
| 34 requests: requestChan, |
| 35 } |
| 36 for i := 0; i < maxConnections; i++ { |
| 37 go ret.requestProcessor(requestChan) |
| 38 } |
| 39 return ret |
| 40 } |
| 41 |
| 42 // Member functions //////////////////////////////////////////////////////////// |
| 43 |
| 44 // URL returns the base url for this Gitiles service wrapper |
| 45 func (g *Gitiles) URL() string { return g.url } |
| 46 |
| 47 // JSON Returns an instance of target or an error. |
| 48 // |
| 49 // Example: |
| 50 // data := map[string]int{} |
| 51 // result, err := g.JSON(data, "some", "url", "pieces") |
| 52 // if err != nil { panic(err) } |
| 53 // data = *result.(*map[string]int) |
| 54 func (g *Gitiles) JSON(target interface{}, pieces ...string) (interface{}, error
) { |
| 55 reply := make(chan jsonResult, 1) |
| 56 g.requests <- jsonRequest{ |
| 57 strings.Join(pieces, "/"), |
| 58 reflect.TypeOf(target), |
| 59 reply, |
| 60 } |
| 61 rslt := <-reply |
| 62 return rslt.dataPtr, rslt.err |
| 63 } |
| 64 |
| 65 // Text returns the decoded text data or an error. It will load the given path |
| 66 // with format=TEXT and apply the base64 decoding. |
| 67 func (g *Gitiles) Text(pieces ...string) ([]byte, error) { |
| 68 reply := make(chan textResult, 1) |
| 69 g.requests <- textRequest{ |
| 70 strings.Join(pieces, "/"), |
| 71 reply, |
| 72 } |
| 73 rslt := <-reply |
| 74 return rslt.data, rslt.err |
| 75 } |
| 76 |
| 77 // GetObjectFromPath returns the git Object at the given commit:path, or an erro
r. |
| 78 // This will hit the url: |
| 79 // {g.url}/+/{committish}/path/pieces...?format=TEXT |
| 80 // Passing NO pieces will return a git.Commit |
| 81 // Passing A blank piece (e.g. empty string) will return a git.Tree for the root |
| 82 // tree. |
| 83 // Passing non-blank pieces will return a git.Tree or git.Blob depending on |
| 84 // the path. |
| 85 func (g *Gitiles) GetObjectFromPath(committish string, pathPieces ...string) (gi
t.InternableObject, error) { |
| 86 ret := make(chan objectResult, 1) |
| 87 g.requests <- objRequest{ |
| 88 textRequest{ |
| 89 strings.Join(append([]string{"+", committish}, pathPiece
s...), "/"), |
| 90 nil, |
| 91 }, |
| 92 ret, |
| 93 } |
| 94 rslt := <-ret |
| 95 return rslt.object, rslt.err |
| 96 } |
| 97 |
| 98 // GetCommitDiff returns the git.TreeDiff from the given committish. |
| 99 func (g *Gitiles) GetCommitDiff(committish string) (git.TreeDiff, error) { |
| 100 rslt, err := g.JSON(jsonCommitDiff{}, "+", committish) |
| 101 if err != nil { |
| 102 return nil, err |
| 103 } |
| 104 diff := *rslt.(*jsonCommitDiff) |
| 105 return diff.ToResult() |
| 106 } |
| 107 |
| 108 func (g *Gitiles) GetTextCommitDiff(id *git.ObjectID) (string, error) { |
| 109 rslt, err := g.Text("+", id.String()+"^!") |
| 110 if err != nil { |
| 111 return "", err |
| 112 } |
| 113 return string(rslt), nil |
| 114 } |
| 115 |
| 116 // Private ///////////////////////////////////////////////////////////////////// |
| 117 |
| 118 type request interface { |
| 119 Process(rsp *http.Response, err error) |
| 120 Method() string |
| 121 URLPath() string |
| 122 Body() io.Reader |
| 123 } |
| 124 |
| 125 func (g *Gitiles) requestProcessor(queue <-chan request) { |
| 126 // Launched as a goroutine to avoid blocking the request processor. |
| 127 for r := range queue { |
| 128 r := r |
| 129 if r == nil { |
| 130 continue |
| 131 } |
| 132 |
| 133 var req *http.Request |
| 134 req, err := http.NewRequest(r.Method(), g.url+"/"+r.URLPath(), r
.Body()) |
| 135 if err != nil { |
| 136 r.Process(nil, err) |
| 137 continue |
| 138 } |
| 139 |
| 140 rsp, err := http.DefaultClient.Do(req) |
| 141 if err != nil { |
| 142 r.Process(nil, err) |
| 143 continue |
| 144 } |
| 145 |
| 146 e := StatusError(rsp.StatusCode) |
| 147 if e.Bad() { |
| 148 err = e |
| 149 } |
| 150 func() { |
| 151 defer rsp.Body.Close() |
| 152 r.Process(rsp, err) |
| 153 }() |
| 154 } |
| 155 } |
| OLD | NEW |