Skip to content

Commit

Permalink
Prevent server 500 on compare branches with no common history (go-git…
Browse files Browse the repository at this point in the history
…ea#6555)

* Prevent 500 if there is no common mergebase
* Prevent creation of PR with no history
  • Loading branch information
zeripath authored Apr 9, 2019
1 parent 7350e43 commit 89cc7c6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
27 changes: 16 additions & 11 deletions modules/git/repo_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,22 @@ func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch stri

prInfo := new(PullRequestInfo)
prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch)
if err != nil {
return nil, fmt.Errorf("GetMergeBase: %v", err)
}

logs, err := NewCommand("log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
if err != nil {
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
if err == nil {
// We have a common base
logs, err := NewCommand("log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
if err != nil {
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
}
} else {
prInfo.Commits = list.New()
prInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
if err != nil {
prInfo.MergeBase = remoteBranch
}
}

// Count number of changed files.
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ editor.cannot_commit_to_protected_branch = Cannot commit to protected branch '%s
commits.desc = Browse source code change history.
commits.commits = Commits
commits.no_commits = No commits in common. '%s' and '%s' have entirely different histories.
commits.search = Search commits…
commits.find = Search
commits.search_all = All Branches
Expand Down
12 changes: 8 additions & 4 deletions templates/repo/commits_table.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<h4 class="ui top attached header">
<div class="ui stackable grid">
<div class="six wide column">
{{.CommitCount}} {{.i18n.Tr "repo.commits.commits"}} {{if .Branch}}({{.Branch}}){{end}}
<div class="ten wide column">
{{if or .PageIsCommits (gt .CommitCount 0)}}
{{.CommitCount}} {{.i18n.Tr "repo.commits.commits"}} {{if .Branch}}({{.Branch}}){{end}}
{{else}}
{{.i18n.Tr "repo.commits.no_commits" $.BaseBranch $.HeadBranch }} {{if .Branch}}({{.Branch}}){{end}}
{{end}}
</div>
<div class="ten wide right aligned column">
<div class="six wide right aligned column">
{{if .PageIsCommits}}
<form class="ignore-dirty" action="{{.RepoLink}}/commits/{{.BranchNameSubURL | EscapePound}}/search">
<div class="ui tiny search input">
Expand All @@ -23,7 +27,7 @@
</div>
</h4>

{{if .Commits}}
{{if and .Commits (gt .CommitCount 0)}}
<div class="ui attached table segment">
<table class="ui very basic striped fixed table single line" id="commits-table">
<thead>
Expand Down
3 changes: 3 additions & 0 deletions templates/repo/pulls/compare.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<div class="ui segment">
{{.i18n.Tr "repo.pulls.has_pull_request" $.RepoLink $.RepoRelPath .PullRequest.Index | Safe}}
</div>
{{else if eq .CommitCount 0 }}
{{template "repo/commits_table" .}}
{{template "repo/diff/box" .}}
{{else}}
{{template "repo/issue/new_form" .}}
{{template "repo/commits_table" .}}
Expand Down

0 comments on commit 89cc7c6

Please sign in to comment.