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

     1  // Copyright 2018 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  // GetTree get the tree of a repository.
    14  func GetTree(ctx *context.APIContext) {
    15  	// swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree
    16  	// ---
    17  	// summary: Gets the tree 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  	// - name: recursive
    37  	//   in: query
    38  	//   description: show all directories and files
    39  	//   required: false
    40  	//   type: boolean
    41  	// - name: page
    42  	//   in: query
    43  	//   description: page number; the 'truncated' field in the response will be true if there are still more items after this page, false if the last page
    44  	//   required: false
    45  	//   type: integer
    46  	// - name: per_page
    47  	//   in: query
    48  	//   description: number of items per page
    49  	//   required: false
    50  	//   type: integer
    51  	// responses:
    52  	//   "200":
    53  	//     "$ref": "#/responses/GitTreeResponse"
    54  	//   "400":
    55  	//     "$ref": "#/responses/error"
    56  	//   "404":
    57  	//     "$ref": "#/responses/notFound"
    58  
    59  	sha := ctx.Params(":sha")
    60  	if len(sha) == 0 {
    61  		ctx.Error(http.StatusBadRequest, "", "sha not provided")
    62  		return
    63  	}
    64  	if tree, err := files_service.GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
    65  		ctx.Error(http.StatusBadRequest, "", err.Error())
    66  	} else {
    67  		ctx.SetTotalCountHeader(int64(tree.TotalCount))
    68  		ctx.JSON(http.StatusOK, tree)
    69  	}
    70  }