Skip to content

Commit

Permalink
Code fix for PR gogs#3748
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Dec 24, 2016
1 parent cdedc2d commit 2994272
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra

![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)

##### Current tip version: 0.9.112 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~)
##### Current tip version: 0.9.113 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~)

| Web | UI | Preview |
|:-------------:|:-------:|:-------:|
Expand Down
2 changes: 1 addition & 1 deletion gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)

const APP_VER = "0.9.112.1223"
const APP_VER = "0.9.113.1223"

func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
Expand Down
67 changes: 52 additions & 15 deletions routers/repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,29 @@ func HTTP(ctx *context.Context) {
}
}

callback := func(rpc string, input []byte) {
callback := func(rpc string, input *os.File) {
if rpc != "receive-pack" || isWiki {
return
}

var lastLine int64 = 0
var (
head = make([]byte, 4) // 00+size
n int
err error
)
for {
head := input[lastLine : lastLine+2]
n, err = input.Read(head)
if err != nil && err != io.EOF {
log.Error(4, "read head: %v", err)
return
} else if n < 4 {
break
}

if head[0] == '0' && head[1] == '0' {
size, err := strconv.ParseInt(string(input[lastLine+2:lastLine+4]), 16, 32)
size, err := strconv.ParseInt(string(head[2:4]), 16, 32)
if err != nil {
log.Error(4, "%v", err)
log.Error(4, "parse size: %v", err)
return
}

Expand All @@ -185,15 +196,24 @@ func HTTP(ctx *context.Context) {
break
}

line := input[lastLine : lastLine+size]
line := make([]byte, size)
n, err = input.Read(line)
if err != nil {
log.Error(4, "read line: %v", err)
return
} else if n < int(size) {
log.Error(4, "didn't read enough bytes: expect %d got %d", size, n)
break
}

idx := bytes.IndexRune(line, '\000')
if idx > -1 {
line = line[:idx]
}

fields := strings.Fields(string(line))
if len(fields) >= 3 {
oldCommitId := fields[0][4:]
oldCommitId := fields[0]
newCommitId := fields[1]
refFullName := fields[2]

Expand All @@ -211,7 +231,6 @@ func HTTP(ctx *context.Context) {
}

}
lastLine = lastLine + size
} else {
break
}
Expand All @@ -230,7 +249,7 @@ func HTTP(ctx *context.Context) {
type serviceConfig struct {
UploadPack bool
ReceivePack bool
OnSucceed func(rpc string, input []byte)
OnSucceed func(rpc string, input *os.File)
}

type serviceHandler struct {
Expand Down Expand Up @@ -347,10 +366,10 @@ func serviceRPC(h serviceHandler, service string) {
h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", service))

var (
reqBody = h.r.Body
input []byte
br io.Reader
err error
reqBody = h.r.Body
tmpFilename string
br io.Reader
err error
)

// Handle GZIP.
Expand All @@ -371,31 +390,49 @@ func serviceRPC(h serviceHandler, service string) {
return
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

_, err = io.Copy(tmpfile, reqBody)
if err != nil {
log.GitLogger.Error(2, "fail to save request body: %v", err)
h.w.WriteHeader(http.StatusInternalServerError)
return
}
tmpfile.Close()

tmpFilename = tmpfile.Name()
tmpfile, err = os.Open(tmpFilename)
if err != nil {
log.GitLogger.Error(2, "fail to open temporary file: %v", err)
h.w.WriteHeader(http.StatusInternalServerError)
return
}
defer tmpfile.Close()

br = tmpfile
} else {
br = reqBody
}

var stderr bytes.Buffer
cmd := exec.Command("git", service, "--stateless-rpc", h.dir)
cmd.Dir = h.dir
cmd.Stdout = h.w
cmd.Stderr = &stderr
cmd.Stdin = br
if err := cmd.Run(); err != nil {
log.GitLogger.Error(2, "fail to serve RPC(%s): %v", service, err)
log.GitLogger.Error(2, "fail to serve RPC(%s): %v - %s", service, err, stderr)
h.w.WriteHeader(http.StatusInternalServerError)
return
}

if h.cfg.OnSucceed != nil {
input, err := os.Open(tmpFilename)
if err != nil {
log.GitLogger.Error(2, "fail to open temporary file: %v", err)
h.w.WriteHeader(http.StatusInternalServerError)
return
}
defer input.Close()
h.cfg.OnSucceed(service, input)
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/.VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.112.1223
0.9.113.1223

0 comments on commit 2994272

Please sign in to comment.