Skip to content

Commit 2d2d85b

Browse files
committed
#1597 support pull requests in same repository
1 parent 9df6ce4 commit 2d2d85b

File tree

13 files changed

+95
-134
lines changed

13 files changed

+95
-134
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
33

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

6-
##### Current version: 0.8.56
6+
##### Current version: 0.8.57
77

88
| Web | UI | Preview |
99
|:-------------:|:-------:|:-------:|

Diff for: cmd/web.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func checkVersion() {
7979

8080
// Check dependency version.
8181
checkers := []VerChecker{
82-
{"github.com/go-xorm/xorm", func() string { return xorm.Version }, "0.4.4.1029"},
82+
{"github.com/go-xorm/xorm", func() string { return xorm.Version }, "0.5.2.0304"},
8383
{"github.com/go-macaron/binding", binding.Version, "0.2.1"},
8484
{"github.com/go-macaron/cache", cache.Version, "0.1.2"},
8585
{"github.com/go-macaron/csrf", csrf.Version, "0.0.3"},
@@ -88,7 +88,7 @@ func checkVersion() {
8888
{"github.com/go-macaron/toolbox", toolbox.Version, "0.1.0"},
8989
{"gopkg.in/ini.v1", ini.Version, "1.8.4"},
9090
{"gopkg.in/macaron.v1", macaron.Version, "0.8.0"},
91-
{"github.com/gogits/git-module", git.Version, "0.2.8"},
91+
{"github.com/gogits/git-module", git.Version, "0.2.9"},
9292
{"github.com/gogits/go-gogs-client", gogs.Version, "0.7.3"},
9393
}
9494
for _, c := range checkers {

Diff for: gogs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/gogits/gogs/modules/setting"
1818
)
1919

20-
const APP_VER = "0.8.56.0304"
20+
const APP_VER = "0.8.57.0304"
2121

2222
func init() {
2323
runtime.GOMAXPROCS(runtime.NumCPU())

Diff for: models/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (pr *PullRequest) UpdatePatch() (err error) {
487487
// FIXME: could fail after user force push head repo, should we always force push here?
488488
// FIXME: Only push branches that are actually updates?
489489
func (pr *PullRequest) PushToBaseRepo() (err error) {
490-
log.Trace("PushToBaseRepo[%[1]d]: pushing commits to base repo 'refs/pull/%[1]d/head'", pr.ID)
490+
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo 'refs/pull/%d/head'", pr.BaseRepoID, pr.Index)
491491

492492
headRepoPath := pr.HeadRepo.RepoPath()
493493
headGitRepo, err := git.OpenRepository(headRepoPath)

Diff for: models/repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ type Repository struct {
184184

185185
func (repo *Repository) AfterSet(colName string, _ xorm.Cell) {
186186
switch colName {
187+
case "default_branch":
188+
// FIXME: use models migration to solve all at once.
189+
if len(repo.DefaultBranch) == 0 {
190+
repo.DefaultBranch = "master"
191+
}
187192
case "num_closed_issues":
188193
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
189194
case "num_closed_pulls":

Diff for: models/user.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,26 @@ func (u *User) UploadAvatar(data []byte) error {
348348

349349
// IsAdminOfRepo returns true if user has admin or higher access of repository.
350350
func (u *User) IsAdminOfRepo(repo *Repository) bool {
351-
if err := repo.GetOwner(); err != nil {
352-
log.Error(3, "GetOwner: %v", err)
353-
return false
354-
}
355-
356-
if repo.Owner.IsOrganization() {
351+
if repo.MustOwner().IsOrganization() {
357352
has, err := HasAccess(u, repo, ACCESS_MODE_ADMIN)
358353
if err != nil {
359354
log.Error(3, "HasAccess: %v", err)
360-
return false
361355
}
362356
return has
363357
}
364358

365359
return repo.IsOwnedBy(u.Id)
366360
}
367361

362+
// CanWriteTo returns true if user has write access to given repository.
363+
func (u *User) CanWriteTo(repo *Repository) bool {
364+
has, err := HasAccess(u, repo, ACCESS_MODE_WRITE)
365+
if err != nil {
366+
log.Error(3, "HasAccess: %v", err)
367+
}
368+
return has
369+
}
370+
368371
// IsOrganization returns true if user is actually a organization.
369372
func (u *User) IsOrganization() bool {
370373
return u.Type == ORGANIZATION

Diff for: modules/log/database.go

-68
This file was deleted.

Diff for: modules/middleware/repo.go

+25-26
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,6 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
3232
ctx.Handle(500, "BaseRepo.GetOwner", err)
3333
return
3434
}
35-
36-
bsaeRepo := repo.BaseRepo
37-
baseGitRepo, err := git.OpenRepository(models.RepoPath(bsaeRepo.Owner.Name, bsaeRepo.Name))
38-
if err != nil {
39-
ctx.Handle(500, "OpenRepository", err)
40-
return
41-
}
42-
if len(bsaeRepo.DefaultBranch) > 0 && baseGitRepo.IsBranchExist(bsaeRepo.DefaultBranch) {
43-
ctx.Data["BaseDefaultBranch"] = bsaeRepo.DefaultBranch
44-
} else {
45-
baseBranches, err := baseGitRepo.GetBranches()
46-
if err != nil {
47-
ctx.Handle(500, "GetBranches", err)
48-
return
49-
}
50-
if len(baseBranches) > 0 {
51-
ctx.Data["BaseDefaultBranch"] = baseBranches[0]
52-
}
53-
}
5435
}
5536

5637
func RepoAssignment(args ...bool) macaron.Handler {
@@ -154,20 +135,38 @@ func RepoAssignment(args ...bool) macaron.Handler {
154135
ctx.Data["Tags"] = tags
155136
ctx.Repo.Repository.NumTags = len(tags)
156137

138+
ctx.Data["Title"] = owner.Name + "/" + repo.Name
139+
ctx.Data["Repository"] = repo
140+
ctx.Data["Owner"] = ctx.Repo.Repository.Owner
141+
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
142+
ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
143+
ctx.Data["IsRepositoryPusher"] = ctx.Repo.IsPusher()
144+
157145
if repo.IsFork {
158146
RetrieveBaseRepo(ctx, repo)
159147
if ctx.Written() {
160148
return
161149
}
162150
}
163151

164-
ctx.Data["Title"] = owner.Name + "/" + repo.Name
165-
ctx.Data["Repository"] = repo
166-
ctx.Data["Owner"] = ctx.Repo.Repository.Owner
167-
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
168-
ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
169-
ctx.Data["IsRepositoryPusher"] = ctx.Repo.IsPusher()
170-
ctx.Data["CanPullRequest"] = ctx.Repo.IsAdmin() && repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls()
152+
// People who have push access and propose a new pull request.
153+
if ctx.Repo.IsPusher() {
154+
// Pull request is allowed if this is a fork repository
155+
// and base repository accepts pull requests.
156+
if repo.BaseRepo != nil {
157+
if repo.BaseRepo.AllowsPulls() {
158+
ctx.Data["CanPullRequest"] = true
159+
ctx.Data["BaseRepo"] = repo.BaseRepo
160+
}
161+
} else {
162+
// Or, this is repository accepts pull requests between branches.
163+
if repo.AllowsPulls() {
164+
ctx.Data["CanPullRequest"] = true
165+
ctx.Data["BaseRepo"] = repo
166+
ctx.Data["IsBetweenBranches"] = true
167+
}
168+
}
169+
}
171170

172171
ctx.Data["DisableSSH"] = setting.SSH.Disabled
173172
ctx.Data["CloneLink"] = repo.CloneLink()

Diff for: routers/repo/pull.go

+45-22
Original file line numberDiff line numberDiff line change
@@ -414,35 +414,53 @@ func MergePullRequest(ctx *middleware.Context) {
414414
}
415415

416416
func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
417-
// Get compare branch information.
417+
// Get compared branches information
418+
// format: <base branch>...[<head repo>:]<head branch>
419+
// base<-head: master...head:feature
420+
// same repo: master...feature
418421
infos := strings.Split(ctx.Params("*"), "...")
419422
if len(infos) != 2 {
423+
log.Trace("Not enough compared branches information: %s", infos)
420424
ctx.Handle(404, "CompareAndPullRequest", nil)
421425
return nil, nil, nil, nil, "", ""
422426
}
423427

424428
baseBranch := infos[0]
425429
ctx.Data["BaseBranch"] = baseBranch
426430

431+
var (
432+
headUser *models.User
433+
headBranch string
434+
isSameRepo bool
435+
err error
436+
)
437+
438+
// If there is no head repository, it means pull request between same repository.
427439
headInfos := strings.Split(infos[1], ":")
428-
if len(headInfos) != 2 {
429-
ctx.Handle(404, "CompareAndPullRequest", nil)
430-
return nil, nil, nil, nil, "", ""
431-
}
432-
headUsername := headInfos[0]
433-
headBranch := headInfos[1]
434-
ctx.Data["HeadBranch"] = headBranch
440+
if len(headInfos) == 1 {
441+
isSameRepo = true
442+
headUser = ctx.Repo.Owner
443+
headBranch = headInfos[0]
435444

436-
headUser, err := models.GetUserByName(headUsername)
437-
if err != nil {
438-
if models.IsErrUserNotExist(err) {
439-
ctx.Handle(404, "GetUserByName", nil)
440-
} else {
441-
ctx.Handle(500, "GetUserByName", err)
445+
} else if len(headInfos) == 2 {
446+
headUser, err = models.GetUserByName(headInfos[0])
447+
if err != nil {
448+
if models.IsErrUserNotExist(err) {
449+
ctx.Handle(404, "GetUserByName", nil)
450+
} else {
451+
ctx.Handle(500, "GetUserByName", err)
452+
}
453+
return nil, nil, nil, nil, "", ""
442454
}
455+
headBranch = headInfos[1]
456+
457+
} else {
458+
ctx.Handle(404, "CompareAndPullRequest", nil)
443459
return nil, nil, nil, nil, "", ""
444460
}
445461
ctx.Data["HeadUser"] = headUser
462+
ctx.Data["HeadBranch"] = headBranch
463+
ctx.Data["IsBetweenBranches"] = isSameRepo
446464

447465
repo := ctx.Repo.Repository
448466

@@ -452,17 +470,23 @@ func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository
452470
return nil, nil, nil, nil, "", ""
453471
}
454472

455-
// Check if current user has fork of repository.
473+
// Check if current user has fork of repository or in the same repository.
456474
headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
457-
if !has || (!ctx.User.IsAdminOfRepo(headRepo) && !ctx.User.IsAdmin) {
475+
if (!has && !isSameRepo) || (!ctx.User.CanWriteTo(headRepo) && !ctx.User.IsAdmin) {
458476
ctx.Handle(404, "HasForkedRepo", nil)
459477
return nil, nil, nil, nil, "", ""
460478
}
461479

462-
headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
463-
if err != nil {
464-
ctx.Handle(500, "OpenRepository", err)
465-
return nil, nil, nil, nil, "", ""
480+
var headGitRepo *git.Repository
481+
if isSameRepo {
482+
headRepo = ctx.Repo.Repository
483+
headGitRepo = ctx.Repo.GitRepo
484+
} else {
485+
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
486+
if err != nil {
487+
ctx.Handle(500, "OpenRepository", err)
488+
return nil, nil, nil, nil, "", ""
489+
}
466490
}
467491

468492
// Check if head branch is valid.
@@ -648,8 +672,7 @@ func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueFor
648672
if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
649673
ctx.Handle(500, "NewPullRequest", err)
650674
return
651-
}
652-
if err := pullRequest.PushToBaseRepo(); err != nil {
675+
} else if err := pullRequest.PushToBaseRepo(); err != nil {
653676
ctx.Handle(500, "PushToBaseRepo", err)
654677
return
655678
}

Diff for: templates/.VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.8.56.0304
1+
0.8.57.0304

Diff for: templates/repo/home.tmpl

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
<div class="ui secondary menu">
1010
{{if .CanPullRequest}}
1111
<div class="fitted item">
12-
{{ $baseRepo := .Repository.BaseRepo}}
13-
<a href="{{AppSubUrl}}/{{$baseRepo.Owner.Name}}/{{$baseRepo.Name}}/compare/{{$.BaseDefaultBranch}}...{{$.Owner.Name}}:{{$.BranchName}}">
12+
<a href="{{.BaseRepo.RepoLink}}/compare/{{.BaseRepo.DefaultBranch}}...{{if not .IsBetweenBranches}}{{$.Owner.Name}}:{{end}}{{$.BranchName}}">
1413
<button class="ui green small button"><i class="octicon octicon-git-compare"></i></button>
1514
</a>
1615
</div>

Diff for: templates/repo/issue/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{if .PageIsIssueList}}
99
<a class="ui green button" href="{{.RepoLink}}/issues/new">{{.i18n.Tr "repo.issues.new"}}</a>
1010
{{else}}
11-
<a class="ui green button {{if not .HasForkedRepo}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.SignedUserName}}:{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
11+
<a class="ui green button {{if not (or .CanPullRequest .HasForkedRepo)}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.Repository.DefaultBranch}}...{{if not (eq .Owner.Name .SignedUserName)}}{{.SignedUserName}}:{{end}}{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
1212
{{end}}
1313
</div>
1414
</div>

Diff for: templates/repo/pulls/compare.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</div>
2222
<div class="scrolling menu">
2323
{{range .Branches}}
24-
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{.}}...{{$.HeadUser.Name}}:{{$.HeadBranch}}">{{.}}</div>
24+
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{.}}...{{if not $.IsBetweenBranches}}{{$.HeadUser.Name}}:{{end}}{{$.HeadBranch}}">{{.}}</div>
2525
{{end}}
2626
</div>
2727
</div>
@@ -39,7 +39,7 @@
3939
</div>
4040
<div class="scrolling menu">
4141
{{range .HeadBranches}}
42-
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{$.BaseBranch}}...{{$.HeadUser.Name}}:{{.}}">{{.}}</div>
42+
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{$.BaseBranch}}...{{if not $.IsBetweenBranches}}{{$.HeadUser.Name}}:{{end}}{{.}}">{{.}}</div>
4343
{{end}}
4444
</div>
4545
</div>

0 commit comments

Comments
 (0)