Browse Source

Fix API raw file content access for default branch (#2849)

Lauris BH 1 year ago
parent
commit
57de1ff991
2 changed files with 8 additions and 1 deletions
  1. 3 0
      integrations/api_repo_raw_test.go
  2. 5 1
      modules/context/repo.go

+ 3 - 0
integrations/api_repo_raw_test.go

@@ -25,4 +25,7 @@ func TestAPIReposRaw(t *testing.T) {
25 25
 		req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/%s/README.md", user.Name, ref)
26 26
 		session.MakeRequest(t, req, http.StatusOK)
27 27
 	}
28
+	// Test default branch
29
+	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/README.md", user.Name)
30
+	session.MakeRequest(t, req, http.StatusOK)
28 31
 }

+ 5 - 1
modules/context/repo.go

@@ -507,7 +507,11 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
507 507
 		if refName := getRefName(ctx, RepoRefTag); len(refName) > 0 {
508 508
 			return refName
509 509
 		}
510
-		return getRefName(ctx, RepoRefCommit)
510
+		if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
511
+			return refName
512
+		}
513
+		ctx.Repo.TreePath = path
514
+		return ctx.Repo.Repository.DefaultBranch
511 515
 	case RepoRefBranch:
512 516
 		return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
513 517
 	case RepoRefTag: