diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/commit.go | 61 | ||||
-rw-r--r-- | routers/repo/issue.go | 67 | ||||
-rw-r--r-- | routers/repo/repo.go | 72 | ||||
-rw-r--r-- | routers/user/user.go | 79 |
4 files changed, 231 insertions, 48 deletions
diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 3d00f8d747..afc1ffda29 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -5,13 +5,22 @@ package repo import ( + "container/list" + "path" + "github.com/codegangsta/martini" + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) func Commits(ctx *middleware.Context, params martini.Params) { - brs, err := models.GetBranches(params["username"], params["reponame"]) + userName := params["username"] + repoName := params["reponame"] + branchName := params["branchname"] + + brs, err := models.GetBranches(userName, repoName) if err != nil { ctx.Handle(200, "repo.Commits", err) return @@ -20,38 +29,70 @@ func Commits(ctx *middleware.Context, params martini.Params) { return } - ctx.Data["IsRepoToolbarCommits"] = true - commits, err := models.GetCommits(params["username"], - params["reponame"], params["branchname"]) + var commits *list.List + if models.IsBranchExist(userName, repoName, branchName) { + commits, err = models.GetCommitsByBranch(userName, repoName, branchName) + } else { + commits, err = models.GetCommitsByCommitId(userName, repoName, branchName) + } + if err != nil { - ctx.Handle(404, "repo.Commits", nil) + ctx.Handle(404, "repo.Commits", err) return } - ctx.Data["Username"] = params["username"] - ctx.Data["Reponame"] = params["reponame"] + + ctx.Data["Username"] = userName + ctx.Data["Reponame"] = repoName ctx.Data["CommitCount"] = commits.Len() ctx.Data["Commits"] = commits + ctx.Data["IsRepoToolbarCommits"] = true ctx.HTML(200, "repo/commits") } func Diff(ctx *middleware.Context, params martini.Params) { - commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"]) + userName := params["username"] + repoName := params["reponame"] + branchName := params["branchname"] + commitId := params["commitid"] + + commit, err := models.GetCommit(userName, repoName, branchName, commitId) if err != nil { ctx.Handle(404, "repo.Diff", err) return } - diff, err := models.GetDiff(models.RepoPath(params["username"], params["reponame"]), params["commitid"]) + diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) if err != nil { ctx.Handle(404, "repo.Diff", err) return } - shortSha := params["commitid"][:7] + isImageFile := func(name string) bool { + repoFile, err := models.GetTargetFile(userName, repoName, + branchName, commitId, name) + + if err != nil { + return false + } + + blob, err := repoFile.LookupBlob() + if err != nil { + return false + } + + data := blob.Contents() + _, isImage := base.IsImageFile(data) + return isImage + } + + shortSha := params["commitid"][:10] + ctx.Data["IsImageFile"] = isImageFile ctx.Data["Title"] = commit.Message() + " ยท " + shortSha ctx.Data["Commit"] = commit ctx.Data["ShortSha"] = shortSha ctx.Data["Diff"] = diff ctx.Data["IsRepoToolbarCommits"] = true + ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId) + ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId) ctx.HTML(200, "repo/diff") } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index e53aebf636..77e35bbae6 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -6,6 +6,7 @@ package repo import ( "fmt" + "net/url" "github.com/codegangsta/martini" @@ -17,23 +18,41 @@ import ( "github.com/gogits/gogs/modules/middleware" ) -func Issues(ctx *middleware.Context, params martini.Params) { +func Issues(ctx *middleware.Context) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "issue.Issues(invalid repo):", nil) + } + ctx.Data["Title"] = "Issues" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true + ctx.Data["ViewType"] = "all" + + milestoneId, _ := base.StrTo(ctx.Query("milestone")).Int() + page, _ := base.StrTo(ctx.Query("page")).Int() - milestoneId, _ := base.StrTo(params["milestone"]).Int() - page, _ := base.StrTo(params["page"]).Int() + ctx.Data["IssueCreatedCount"] = 0 + + var posterId int64 = 0 + if ctx.Query("type") == "created_by" { + if !ctx.IsSigned { + ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI)) + ctx.Redirect("/user/login/", 302) + return + } + posterId = ctx.User.Id + ctx.Data["ViewType"] = "created_by" + ctx.Data["IssueCreatedCount"] = models.GetUserIssueCount(posterId, ctx.Repo.Repository.Id) + } // Get issues. - issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, 0, - int64(milestoneId), page, params["state"] == "closed", false, params["labels"], params["sortType"]) + issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, posterId, int64(milestoneId), page, + ctx.Query("state") == "closed", false, ctx.Query("labels"), ctx.Query("sortType")) if err != nil { ctx.Handle(200, "issue.Issues: %v", err) return } - var closedCount int // Get posters. for i := range issues { u, err := models.GetUserById(issues[i].PosterId) @@ -41,21 +60,22 @@ func Issues(ctx *middleware.Context, params martini.Params) { ctx.Handle(200, "issue.Issues(get poster): %v", err) return } - - if issues[i].IsClosed { - closedCount++ - } issues[i].Poster = u } ctx.Data["Issues"] = issues - ctx.Data["IssueCount"] = len(issues) - ctx.Data["OpenCount"] = len(issues) - closedCount - ctx.Data["ClosedCount"] = closedCount + ctx.Data["IssueCount"] = ctx.Repo.Repository.NumIssues + ctx.Data["OpenCount"] = ctx.Repo.Repository.NumIssues - ctx.Repo.Repository.NumClosedIssues + ctx.Data["ClosedCount"] = ctx.Repo.Repository.NumClosedIssues + ctx.Data["IsShowClosed"] = ctx.Query("state") == "closed" ctx.HTML(200, "issue/list") } func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "issue.CreateIssue(invalid repo):", nil) + } + ctx.Data["Title"] = "Create issue" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false @@ -71,15 +91,16 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat } issue, err := models.CreateIssue(ctx.User.Id, ctx.Repo.Repository.Id, form.MilestoneId, form.AssigneeId, - form.IssueName, form.Labels, form.Content, false) + ctx.Repo.Repository.NumIssues, form.IssueName, form.Labels, form.Content, false) if err != nil { ctx.Handle(200, "issue.CreateIssue", err) return } // Notify watchers. - if err = models.NotifyWatchers(ctx.User.Id, ctx.Repo.Repository.Id, models.OP_CREATE_ISSUE, - ctx.User.Name, ctx.Repo.Repository.Name, "", fmt.Sprintf("%d|%s", issue.Index, issue.Name)); err != nil { + if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, + OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), + RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil { ctx.Handle(200, "issue.CreateIssue", err) return } @@ -97,6 +118,10 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat } func ViewIssue(ctx *middleware.Context, params martini.Params) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "issue.ViewIssue(invalid repo):", nil) + } + index, err := base.StrTo(params["index"]).Int() if err != nil { ctx.Handle(404, "issue.ViewIssue", err) @@ -120,6 +145,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { return } issue.Poster = u + issue.Content = string(base.RenderMarkdown([]byte(issue.Content), "")) // Get comments. comments, err := models.GetIssueComments(issue.Id) @@ -136,6 +162,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { return } comments[i].Poster = u + comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), "")) } ctx.Data["Title"] = issue.Name @@ -147,6 +174,10 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { } func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "issue.UpdateIssue(invalid repo):", nil) + } + index, err := base.StrTo(params["index"]).Int() if err != nil { ctx.Handle(404, "issue.UpdateIssue", err) @@ -183,6 +214,10 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat } func Comment(ctx *middleware.Context, params martini.Params) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "issue.Comment(invalid repo):", nil) + } + index, err := base.StrTo(ctx.Query("issueIndex")).Int() if err != nil { ctx.Handle(404, "issue.Comment", err) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 435587472a..e7107ad1cd 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -57,19 +57,23 @@ func Single(ctx *middleware.Context, params martini.Params) { return } + branchName := params["branchname"] + userName := params["username"] + repoName := params["reponame"] + // Get tree path treename := params["_1"] if len(treename) > 0 && treename[len(treename)-1] == '/' { ctx.Redirect("/" + ctx.Repo.Owner.LowerName + "/" + - ctx.Repo.Repository.Name + "/src/" + params["branchname"] + "/" + treename[:len(treename)-1]) + ctx.Repo.Repository.Name + "/src/" + branchName + "/" + treename[:len(treename)-1]) return } ctx.Data["IsRepoToolbarSource"] = true // Branches. - brs, err := models.GetBranches(params["username"], params["reponame"]) + brs, err := models.GetBranches(userName, repoName) if err != nil { ctx.Handle(404, "repo.Single(GetBranches)", err) return @@ -80,15 +84,22 @@ func Single(ctx *middleware.Context, params martini.Params) { } ctx.Data["Branches"] = brs - repoFile, err := models.GetTargetFile(params["username"], params["reponame"], - params["branchname"], params["commitid"], treename) + var commitId string + isViewBranch := models.IsBranchExist(userName, repoName, branchName) + if !isViewBranch { + commitId = branchName + } + ctx.Data["IsViewBranch"] = isViewBranch + + repoFile, err := models.GetTargetFile(userName, repoName, + branchName, commitId, treename) if err != nil && err != models.ErrRepoFileNotExist { ctx.Handle(404, "repo.Single(GetTargetFile)", err) return } - branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + params["branchname"] - rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + params["branchname"] + branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + branchName + rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + branchName if len(treename) != 0 && repoFile == nil { ctx.Handle(404, "repo.Single", nil) @@ -111,23 +122,28 @@ func Single(ctx *middleware.Context, params martini.Params) { data := blob.Contents() _, isTextFile := base.IsTextFile(data) + _, isImageFile := base.IsImageFile(data) ctx.Data["FileIsText"] = isTextFile - readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name) - ctx.Data["ReadmeExist"] = readmeExist - if readmeExist { - ctx.Data["FileContent"] = string(base.RenderMarkdown(data, "")) + if isImageFile { + ctx.Data["IsImageFile"] = true } else { - if isTextFile { - ctx.Data["FileContent"] = string(data) + readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name) + ctx.Data["ReadmeExist"] = readmeExist + if readmeExist { + ctx.Data["FileContent"] = string(base.RenderMarkdown(data, "")) + } else { + if isTextFile { + ctx.Data["FileContent"] = string(data) + } } } } } else { // Directory and file list. - files, err := models.GetReposFiles(params["username"], params["reponame"], - params["branchname"], params["commitid"], treename) + files, err := models.GetReposFiles(userName, repoName, + branchName, commitId, treename) if err != nil { ctx.Handle(404, "repo.Single(GetReposFiles)", err) return @@ -166,8 +182,8 @@ func Single(ctx *middleware.Context, params martini.Params) { } } - ctx.Data["Username"] = params["username"] - ctx.Data["Reponame"] = params["reponame"] + ctx.Data["Username"] = userName + ctx.Data["Reponame"] = repoName var treenames []string Paths := make([]string, 0) @@ -185,8 +201,8 @@ func Single(ctx *middleware.Context, params martini.Params) { } // Get latest commit according username and repo name. - commit, err := models.GetCommit(params["username"], params["reponame"], - params["branchname"], params["commitid"]) + commit, err := models.GetCommit(userName, repoName, + branchName, commitId) if err != nil { log.Error("repo.Single(GetCommit): %v", err) ctx.Handle(404, "repo.Single(GetCommit)", err) @@ -194,6 +210,8 @@ func Single(ctx *middleware.Context, params martini.Params) { } ctx.Data["LastCommit"] = commit + ctx.Data["CommitId"] = commitId + ctx.Data["Paths"] = Paths ctx.Data["Treenames"] = treenames ctx.Data["BranchLink"] = branchLink @@ -209,8 +227,18 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) { // Get tree path treename := params["_1"] - repoFile, err := models.GetTargetFile(params["username"], params["reponame"], - params["branchname"], params["commitid"], treename) + branchName := params["branchname"] + userName := params["username"] + repoName := params["reponame"] + + var commitId string + if !models.IsBranchExist(userName, repoName, branchName) { + commitId = branchName + branchName = "" + } + + repoFile, err := models.GetTargetFile(userName, repoName, + branchName, commitId, treename) if err != nil { ctx.Handle(404, "repo.SingleDownload(GetTargetFile)", err) @@ -225,9 +253,9 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) { data := blob.Contents() contentType, isTextFile := base.IsTextFile(data) + _, isImageFile := base.IsImageFile(data) ctx.Res.Header().Set("Content-Type", contentType) - if !isTextFile { - ctx.Res.Header().Set("Content-Type", contentType) + if !isTextFile && !isImageFile { ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename)) ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") } diff --git a/routers/user/user.go b/routers/user/user.go index d3ef96211e..b0fc583978 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -286,6 +286,85 @@ func Feeds(ctx *middleware.Context, form auth.FeedsForm) { func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Your Issues" + ctx.Data["ViewType"] = "all" + + page, _ := base.StrTo(ctx.Query("page")).Int() + repoId, _ := base.StrTo(ctx.Query("repoid")).Int64() + + ctx.Data["RepoId"] = repoId + + var posterId int64 = 0 + if ctx.Query("type") == "created_by" { + posterId = ctx.User.Id + ctx.Data["ViewType"] = "created_by" + } + + // Get all repositories. + repos, err := models.GetRepositories(ctx.User) + if err != nil { + ctx.Handle(200, "user.Issues(get repositories)", err) + return + } + + showRepos := make([]models.Repository, 0, len(repos)) + + var closedIssueCount, createdByCount int + + // Get all issues. + allIssues := make([]models.Issue, 0, 5*len(repos)) + for i, repo := range repos { + issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, false, false, "", "") + if err != nil { + ctx.Handle(200, "user.Issues(get issues)", err) + return + } + + closedIssueCount += repo.NumClosedIssues + + // Set repository information to issues. + for j := range issues { + issues[j].Repo = &repos[i] + } + allIssues = append(allIssues, issues...) + + repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues + if repos[i].NumOpenIssues > 0 { + showRepos = append(showRepos, repos[i]) + + } + } + + showIssues := make([]models.Issue, 0, len(allIssues)) + isShowClosed := ctx.Query("state") == "closed" + ctx.Data["IsShowClosed"] = isShowClosed + + // Get posters and filter issues. + for i := range allIssues { + u, err := models.GetUserById(allIssues[i].PosterId) + if err != nil { + ctx.Handle(200, "user.Issues(get poster): %v", err) + return + } + allIssues[i].Poster = u + if u.Id == ctx.User.Id { + createdByCount++ + } + + if repoId > 0 && repoId != allIssues[i].Repo.Id { + continue + } + + if isShowClosed == allIssues[i].IsClosed { + showIssues = append(showIssues, allIssues[i]) + } + } + + ctx.Data["Repos"] = showRepos + ctx.Data["Issues"] = showIssues + ctx.Data["AllIssueCount"] = len(allIssues) + ctx.Data["ClosedIssueCount"] = closedIssueCount + ctx.Data["OpenIssueCount"] = len(allIssues) - closedIssueCount + ctx.Data["CreatedByCount"] = createdByCount ctx.HTML(200, "issue/user") } |