From ed9b7d2db9d797105a62a0c2078c614ab90ff956 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 26 Mar 2014 07:24:20 -0400 Subject: [PATCH 1/3] More on diff page --- gogs.go | 2 +- models/git.go | 45 ++++++++++++++++++++++++++-------------- modules/base/template.go | 1 + modules/base/tool.go | 13 ++++++++++++ templates/repo/diff.tmpl | 2 +- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/gogs.go b/gogs.go index f5a328add..d88a2bc6f 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.1.8.0325" +const APP_VER = "0.1.8.0326" func init() { base.AppVer = APP_VER diff --git a/models/git.go b/models/git.go index ce4434ca5..00621776d 100644 --- a/models/git.go +++ b/models/git.go @@ -274,7 +274,6 @@ const DIFF_HEAD = "diff --git " func ParsePatch(reader io.Reader) (*Diff, error) { scanner := bufio.NewScanner(reader) - var totalAdd, totalDel int var curFile *DiffFile curSection := &DiffSection{ Lines: make([]*DiffLine, 0, 10), @@ -285,6 +284,10 @@ func ParsePatch(reader io.Reader) (*Diff, error) { for scanner.Scan() { line := scanner.Text() fmt.Println(i, line) + if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") { + continue + } + i = i + 1 if line == "" { continue @@ -300,40 +303,51 @@ func ParsePatch(reader io.Reader) (*Diff, error) { diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: "@@" + ss[len(ss)-2] + "@@"} curSection.Lines = append(curSection.Lines, diffLine) - diffLine = &DiffLine{Type: DIFF_LINE_PLAIN, Content: ss[len(ss)-1]} - curSection.Lines = append(curSection.Lines, diffLine) + if len(ss[len(ss)-1]) > 0 { + diffLine = &DiffLine{Type: DIFF_LINE_PLAIN, Content: ss[len(ss)-1]} + curSection.Lines = append(curSection.Lines, diffLine) + } continue } else if line[0] == '+' { + curFile.Addition++ + diff.TotalAddition++ diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line} curSection.Lines = append(curSection.Lines, diffLine) continue } else if line[0] == '-' { + curFile.Deletion++ + diff.TotalDeletion++ diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line} curSection.Lines = append(curSection.Lines, diffLine) continue } + // Get new file. if strings.HasPrefix(line, DIFF_HEAD) { - if curFile != nil { - curFile.Addition, totalAdd = totalAdd, 0 - curFile.Deletion, totalDel = totalDel, 0 - curFile = nil - } fs := strings.Split(line[len(DIFF_HEAD):], " ") a := fs[0] curFile = &DiffFile{ Name: a[strings.Index(a, "/")+1:], Type: DIFF_FILE_CHANGE, - Sections: make([]*DiffSection, 0), + Sections: make([]*DiffSection, 0, 10), } diff.Files = append(diff.Files, curFile) - scanner.Scan() - scanner.Scan() - if scanner.Text() == "--- /dev/null" { - curFile.Type = DIFF_FILE_ADD + + // Check file diff type. + for scanner.Scan() { + switch { + case strings.HasPrefix(scanner.Text(), "new file"): + curFile.Type = DIFF_FILE_ADD + case strings.HasPrefix(scanner.Text(), "deleted"): + curFile.Type = DIFF_FILE_DEL + case strings.HasPrefix(scanner.Text(), "index"): + curFile.Type = DIFF_FILE_CHANGE + } + if curFile.Type > 0 { + break + } } - scanner.Scan() } } @@ -351,14 +365,13 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { return nil, err } - // ???? if commit.ParentCount() == 0 { return &Diff{}, err } rd, wr := io.Pipe() go func() { - cmd := exec.Command("git", "diff", commitid, commit.Parent(0).Oid.String()) + cmd := exec.Command("git", "diff", commit.Parent(0).Oid.String(), commitid) cmd.Dir = repoPath cmd.Stdout = wr cmd.Stdin = os.Stdin diff --git a/modules/base/template.go b/modules/base/template.go index 8d95dbeab..e577bd7c3 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -70,4 +70,5 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "SubStr": func(str string, start, length int) string { return str[start : start+length] }, + "DiffTypeToStr": DiffTypeToStr, } diff --git a/modules/base/tool.go b/modules/base/tool.go index 8f38d492a..15d42fa75 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -539,3 +539,16 @@ func ActionDesc(act Actioner, avatarLink string) string { return "invalid type" } } + +func DiffTypeToStr(diffType int) string { + switch diffType { + case 1: + return "add" + case 2: + return "modify" + case 3: + return "del" + default: + return "unknown" + } +} diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index c05f4d2ff..ab13c4017 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -39,7 +39,7 @@ {{.Deletion}} -   +   {{.Name}} {{end}} From 3cc860a46fe696065618ed0800021336c1994671 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 26 Mar 2014 07:40:50 -0400 Subject: [PATCH 2/3] auth fix --- routers/repo/issue.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 242593ff2..339d5a4da 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -55,11 +55,6 @@ func Issues(ctx *middleware.Context, params martini.Params) { } func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - if !ctx.Repo.IsOwner { - ctx.Handle(404, "issue.CreateIssue", nil) - return - } - ctx.Data["Title"] = "Create issue" ctx.Data["IsRepoToolbarIssues"] = true From 409e4cde7a379bbdbe53367b3726f64b80aed0eb Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 26 Mar 2014 07:42:08 -0400 Subject: [PATCH 3/3] auth fix --- routers/repo/issue.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 339d5a4da..67d3059f5 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -117,11 +117,6 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { } func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - if !ctx.Repo.IsOwner { - ctx.Handle(404, "issue.UpdateIssue", nil) - return - } - index, err := base.StrTo(params["index"]).Int() if err != nil { ctx.Handle(404, "issue.UpdateIssue", err) @@ -138,6 +133,11 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat return } + if ctx.User.Id != issue.PosterId { + ctx.Handle(404, "issue.UpdateIssue", nil) + return + } + issue.Name = form.IssueName issue.MilestoneId = form.MilestoneId issue.AssigneeId = form.AssigneeId