code.gitea.io/gitea@v1.21.7/routers/api/v1/repo/blob.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"code.gitea.io/gitea/modules/context"
    10  	files_service "code.gitea.io/gitea/services/repository/files"
    11  )
    12  
    13  // GetBlob get the blob of a repository file.
    14  func GetBlob(ctx *context.APIContext) {
    15  	// swagger:operation GET /repos/{owner}/{repo}/git/blobs/{sha} repository GetBlob
    16  	// ---
    17  	// summary: Gets the blob of a repository.
    18  	// produces:
    19  	// - application/json
    20  	// parameters:
    21  	// - name: owner
    22  	//   in: path
    23  	//   description: owner of the repo
    24  	//   type: string
    25  	//   required: true
    26  	// - name: repo
    27  	//   in: path
    28  	//   description: name of the repo
    29  	//   type: string
    30  	//   required: true
    31  	// - name: sha
    32  	//   in: path
    33  	//   description: sha of the commit
    34  	//   type: string
    35  	//   required: true
    36  	// responses:
    37  	//   "200":
    38  	//     "$ref": "#/responses/GitBlobResponse"
    39  	//   "400":
    40  	//     "$ref": "#/responses/error"
    41  	//   "404":
    42  	//     "$ref": "#/responses/notFound"
    43  
    44  	sha := ctx.Params("sha")
    45  	if len(sha) == 0 {
    46  		ctx.Error(http.StatusBadRequest, "", "sha not provided")
    47  		return
    48  	}
    49  
    50  	if blob, err := files_service.GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, sha); err != nil {
    51  		ctx.Error(http.StatusBadRequest, "", err)
    52  	} else {
    53  		ctx.JSON(http.StatusOK, blob)
    54  	}
    55  }