Prevent NPE on invalid diff #17197

Merged
lunny merged 2 commits from prevent-npe-17193 into main 2021-09-30 19:31:02 +00:00
3 changed files with 7 additions and 2 deletions

View File

@ -6,6 +6,7 @@ package integrations
import (
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -21,4 +22,8 @@ func TestCompareTag(t *testing.T) {
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
// A dropdown for both base and head.
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
req = NewRequest(t, "GET", "/user2/repo1/compare/invalid")
resp = session.MakeRequest(t, req, http.StatusNotFound)
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
}

View File

@ -635,7 +635,7 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
func CompareDiff(ctx *context.Context) {
ci := ParseCompareInfo(ctx)
defer func() {
if ci.HeadGitRepo != nil {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close()
}
}()

View File

@ -1043,7 +1043,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
ci := ParseCompareInfo(ctx)
defer func() {
if ci.HeadGitRepo != nil {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close()
}
}()