Commit f30edb68 authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab)

Merge branch 'zj-plain-diffs-by-workhorse' into 'master'

Diffs served by workhorse

First iteration; mainly pushed the branch to trigger a build.

Based on `archive.go` and `blob.go`. 

TODO:
- [x] Add the correct route
- [x] Test locally
- [x] Write tests

See merge request !45
parents dc9b6c39 6f92a907
package git
import (
"fmt"
"io"
"log"
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
)
type diff struct{ senddata.Prefix }
type diffParams struct {
RepoPath string
ShaFrom string
ShaTo string
}
var SendDiff = &diff{"git-diff:"}
func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params diffParams
if err := d.Unpack(&params, sendData); err != nil {
helper.Fail500(w, fmt.Errorf("SendDiff: unpack sendData: %v", err))
return
}
log.Printf("SendDiff: sending diff between %q and %q for %q", params.ShaFrom, params.ShaTo, r.URL.Path)
gitDiffCmd := gitCommand("", "git", "--git-dir="+params.RepoPath, "diff", params.ShaFrom, params.ShaTo)
stdout, err := gitDiffCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, fmt.Errorf("SendDiff: create stdout pipe: %v", err))
return
}
if err := gitDiffCmd.Start(); err != nil {
helper.Fail500(w, fmt.Errorf("SendDiff: start %v: %v", gitDiffCmd, err))
return
}
defer helper.CleanUpProcessGroup(gitDiffCmd)
w.Header().Del("Content-Length")
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(fmt.Errorf("SendDiff: copy %v stdout: %v", gitDiffCmd, err))
return
}
if err := gitDiffCmd.Wait(); err != nil {
helper.LogError(fmt.Errorf("SendDiff: wait for %v: %v", gitDiffCmd, err))
return
}
}
...@@ -48,6 +48,7 @@ func (u *Upstream) configureRoutes() { ...@@ -48,6 +48,7 @@ func (u *Upstream) configureRoutes() {
)), )),
git.SendArchive, git.SendArchive,
git.SendBlob, git.SendBlob,
git.SendDiff,
) )
u.Routes = []route{ u.Routes = []route{
......
...@@ -621,6 +621,50 @@ func TestGetGitBlob(t *testing.T) { ...@@ -621,6 +621,50 @@ func TestGetGitBlob(t *testing.T) {
} }
} }
func TestGetGitDiff(t *testing.T) {
fromSha := "be93687618e4b132087f430a4d8fc3a609c9b77c"
toSha := "54fcc214b94e78d7a41a9a8fe6d87a5e59500e51"
headerKey := http.CanonicalHeaderKey("Gitlab-Workhorse-Send-Data")
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
responseJSON := fmt.Sprintf(`{"RepoPath":"%s","ShaFrom":"%s","ShaTo":"%s"}`, path.Join(testRepoRoot, testRepo), fromSha, toSha)
encodedJSON := base64.StdEncoding.EncodeToString([]byte(responseJSON))
w.Header().Set(headerKey, "git-diff:"+encodedJSON)
return
})
defer ts.Close()
ws := startWorkhorseServer(ts.URL)
defer ws.Close()
resourcePath := "/something"
resp, err := http.Get(ws.URL + resourcePath)
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("GET %q: expected 200, got %d", resourcePath, resp.StatusCode)
}
if len(resp.Header[headerKey]) != 0 {
t.Fatalf("Unexpected response header: %s: %q", headerKey, resp.Header.Get(headerKey))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(string(body), "diff --git a/README b/README") {
t.Fatalf("diff --git a/README b/README, got %q", body)
}
bodyLengthBytes := len(body)
if bodyLengthBytes != 155 {
t.Fatal("Expected the body to consist of 155 bytes, got %v", bodyLengthBytes)
}
}
func setupStaticFile(fpath, content string) error { func setupStaticFile(fpath, content string) error {
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment