Commit 34b2309b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use "git show" instead of "git cat-file"

parent 86aaa133
...@@ -2,16 +2,12 @@ package git ...@@ -2,16 +2,12 @@ package git
import ( import (
"../helper" "../helper"
"bufio"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
) )
const blobLine = `blob
`
func SendGitBlob(w http.ResponseWriter, r *http.Request, repoPath string, blobId string) { func SendGitBlob(w http.ResponseWriter, r *http.Request, repoPath string, blobId string) {
blobSpec, err := base64.URLEncoding.DecodeString(blobId) blobSpec, err := base64.URLEncoding.DecodeString(blobId)
if err != nil { if err != nil {
...@@ -19,44 +15,24 @@ func SendGitBlob(w http.ResponseWriter, r *http.Request, repoPath string, blobId ...@@ -19,44 +15,24 @@ func SendGitBlob(w http.ResponseWriter, r *http.Request, repoPath string, blobId
return return
} }
catFileCmd := gitCommand("", "git", "--git-dir="+repoPath, "cat-file", "--batch=%(objecttype)") gitShowCmd := gitCommand("", "git", "--git-dir="+repoPath, "show", string(blobSpec))
catFileStdin, err := catFileCmd.StdinPipe() stdout, err := gitShowCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, fmt.Errorf("SendGitBlob: git cat-file stdin: %v", err))
return
}
catFileStdout, err := catFileCmd.StdoutPipe()
if err != nil { if err != nil {
helper.Fail500(w, fmt.Errorf("SendGitBlob: git cat-file stdout: %v", err)) helper.Fail500(w, fmt.Errorf("SendGitBlob: git show stdout: %v", err))
return
}
if err := catFileCmd.Start(); err != nil {
helper.Fail500(w, fmt.Errorf("SendGitBlob: start %v: %v", catFileCmd, err))
return
}
defer cleanUpProcessGroup(catFileCmd)
if _, err := fmt.Fprintf(catFileStdin, "%s\n", blobSpec); err != nil {
helper.Fail500(w, fmt.Errorf("SendGitBlob: send command to git cat-file: %v", err))
return
}
if err := catFileStdin.Close(); err != nil {
helper.Fail500(w, fmt.Errorf("SendGitBlob: close git cat-file stdin: %v", err))
return return
} }
out := bufio.NewReader(catFileStdout) if err := gitShowCmd.Start(); err != nil {
if response, err := out.ReadString('\n'); err != nil || response != blobLine { helper.Fail500(w, fmt.Errorf("SendGitBlob: start %v: %v", gitShowCmd, err))
helper.Fail500(w, fmt.Errorf("SendGitBlob: git cat-file returned %q, error: %v", response, err))
return return
} }
defer cleanUpProcessGroup(gitShowCmd)
if _, err := io.Copy(w, catFileStdout); err != nil { if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(fmt.Errorf("SendGitBlob: copy git cat-file stdout: %v", err)) helper.LogError(fmt.Errorf("SendGitBlob: copy git show stdout: %v", err))
return return
} }
if err := catFileCmd.Wait(); err != nil { if err := gitShowCmd.Wait(); err != nil {
helper.LogError(fmt.Errorf("SendGitBlob: wait for git cat-file: %v", err)) helper.LogError(fmt.Errorf("SendGitBlob: wait for git show: %v", err))
return return
} }
} }
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