Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compare page to look up base, head, own-fork, forkbase-of-head #11327

Merged
merged 17 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow compare page to look up base, head, own-fork, forkbase-of-head
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed May 7, 2020
commit a1a63dc186450a9e7c55b7a170a769ca5ef65701
113 changes: 88 additions & 25 deletions routers/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,47 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
ctx.Data["BaseIsBranch"] = baseIsBranch
ctx.Data["BaseIsTag"] = baseIsTag

var forkedRepo *models.Repository
if baseRepo.IsFork {
err = baseRepo.GetBaseRepo()
lafriks marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if !models.IsErrRepoNotExist(err) {
ctx.ServerError("Unable to find root repo", err)
return nil, nil, nil, nil, "", ""
}
} else {
forkedRepo = baseRepo.BaseRepo
ctx.Data["ForkedRepo"] = forkedRepo
}
}

var ownForkedRepo *models.Repository
if baseRepo.OwnerID != ctx.User.ID {
repo, has := models.HasForkedRepo(ctx.User.ID, baseRepo.ID)
if has {
ownForkedRepo = repo
ctx.Data["OwnForkedRepo"] = ownForkedRepo
}
}

headRepo := baseRepo
has := false
// Check if current user has fork of repository or in the same repository.
headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
if !has && !isSameRepo {
if !isSameRepo && forkedRepo != nil && forkedRepo.OwnerID == headUser.ID {
headRepo = forkedRepo
has = true
}
if !isSameRepo && ownForkedRepo != nil && ownForkedRepo.OwnerID == headUser.ID {
headRepo = ownForkedRepo
has = true
}
if !isSameRepo && !has {
headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID)
}
if !isSameRepo && !has && baseRepo.IsFork {
headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ForkID)
}
if !isSameRepo && !has {
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
ctx.Data["PageIsComparePull"] = false
}

Expand All @@ -150,8 +188,8 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
headRepo = ctx.Repo.Repository
headGitRepo = ctx.Repo.GitRepo
ctx.Data["BaseName"] = headUser.Name
} else {
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
} else if has {
headGitRepo, err = git.OpenRepository(headRepo.RepoPath())
ctx.Data["BaseName"] = baseRepo.OwnerName
if err != nil {
ctx.ServerError("OpenRepository", err)
Expand Down Expand Up @@ -196,6 +234,40 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
}
}

if forkedRepo != nil {
if forkedRepo.ID == headRepo.ID || forkedRepo.ID == baseRepo.ID {
delete(ctx.Data, "ForkedRepo")
} else {
perm, branches, err := getBranchesForRepo(ctx.User, forkedRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil, nil, nil, nil, "", ""
}
if !perm {
delete(ctx.Data, "ForkedRepo")
}
ctx.Data["ForkedRepoBranches"] = branches
}
}

if ownForkedRepo != nil {
if ownForkedRepo.ID == headRepo.ID ||
ownForkedRepo.ID == baseRepo.ID ||
(forkedRepo != nil && ownForkedRepo.ID == forkedRepo.ID) {
delete(ctx.Data, "OwnForkedRepo")
zeripath marked this conversation as resolved.
Show resolved Hide resolved
} else {
perm, branches, err := getBranchesForRepo(ctx.User, ownForkedRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil, nil, nil, nil, "", ""
}
if !perm {
delete(ctx.Data, "OwnForkedRepo")
}
ctx.Data["OwnForkedRepoBranches"] = branches
}
}

// Check if head branch is valid.
headIsCommit := headGitRepo.IsCommitExist(headBranch)
headIsBranch := headGitRepo.IsBranchExist(headBranch)
Expand Down Expand Up @@ -343,28 +415,25 @@ func PrepareCompareDiff(
return false
}

// parseBaseRepoInfo parse base repository if current repo is forked.
// The "base" here means the repository where current repo forks from,
// not the repository fetch from current URL.
func parseBaseRepoInfo(ctx *context.Context, repo *models.Repository) error {
if !repo.IsFork {
return nil
func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []string, error) {
perm, err := models.GetUserRepoPermission(repo, user)
if err != nil {
return false, nil, err
}
if err := repo.GetBaseRepo(); err != nil {
return err
if !perm.CanRead(models.UnitTypeCode) {
return false, nil, nil
}

baseGitRepo, err := git.OpenRepository(repo.BaseRepo.RepoPath())
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
return err
return false, nil, err
}
defer baseGitRepo.Close()
defer gitRepo.Close()

ctx.Data["BaseRepoBranches"], err = baseGitRepo.GetBranches()
branches, err := gitRepo.GetBranches()
if err != nil {
return err
return false, nil, err
}
return nil
return true, branches, nil
}

// CompareDiff show different from one commit to another commit
Expand All @@ -375,12 +444,6 @@ func CompareDiff(ctx *context.Context) {
}
defer headGitRepo.Close()

var err error
if err = parseBaseRepoInfo(ctx, headRepo); err != nil {
ctx.ServerError("parseBaseRepoInfo", err)
return
}

nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch)
if ctx.Written() {
return
Expand Down
21 changes: 18 additions & 3 deletions templates/repo/diff/compare.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
{{range .Branches}}
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{EscapePound .}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}:{{end}}{{EscapePound $.HeadBranch}}">{{$.BaseName}}:{{.}}</div>
{{end}}
{{if .Repository.IsFork}}
{{range .BaseRepoBranches}}
<div class="item" data-url="{{$.PullRequestCtx.BaseRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}:{{EscapePound $.HeadBranch}}">{{$.PullRequestCtx.BaseRepo.OwnerName}}:{{.}}</div>
{{if .OwnForkedRepo}}
{{range .ForkedRepoBranches}}
<div class="item" data-url="{{$.OwnForkedRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}:{{EscapePound $.HeadBranch}}">{{$.OwnForkedRepo.OwnerName}}:{{.}}</div>
{{end}}
{{end}}
{{if .ForkedRepo}}
{{range .ForkedRepoBranches}}
<div class="item" data-url="{{$.ForkedRepo.Link}}/compare/{{EscapePound .}}...{{$.HeadUser.Name}}:{{EscapePound $.HeadBranch}}">{{$.ForkedRepo.OwnerName}}:{{.}}</div>
{{end}}
{{end}}
</div>
Expand All @@ -51,6 +56,16 @@
{{range .HeadBranches}}
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}:{{end}}{{EscapePound .}}">{{$.HeadUser.Name}}:{{.}}</div>
{{end}}
{{if .OwnForkedRepo}}
{{range .OwnForkedRepoBranches}}
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{$.OwnForkedRepo.OwnerName}}:{{EscapePound .}}">{{$.OwnForkedRepo.OwnerName}}:{{.}}</div>
{{end}}
{{end}}
{{if .ForkedRepo}}
{{range .ForkedRepoBranches}}
<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{EscapePound $.BaseBranch}}...{{$.ForkedRepo.OwnerName}}:{{EscapePound .}}">{{$.ForkedRepo.OwnerName}}:{{.}}</div>
{{end}}
{{end}}
</div>
</div>
</div>
Expand Down