mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/services/context"
|
|
)
|
|
|
|
type languageResponse []*repo_model.LanguageStat
|
|
|
|
func (l languageResponse) MarshalJSON() ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
if _, err := buf.WriteString("{"); err != nil {
|
|
return nil, err
|
|
}
|
|
for i, lang := range l {
|
|
if i > 0 {
|
|
if _, err := buf.WriteString(","); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if _, err := buf.WriteString(strconv.Quote(lang.Language)); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := buf.WriteString(":"); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := buf.WriteString(strconv.FormatInt(lang.Size, 10)); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if _, err := buf.WriteString("}"); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// GetLanguages returns languages and number of bytes of code written
|
|
func GetLanguages(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/languages repository repoGetLanguages
|
|
// ---
|
|
// summary: Get languages and number of bytes of code written
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "200":
|
|
// "$ref": "#/responses/LanguageStatistics"
|
|
|
|
langs, err := repo_model.GetLanguageStats(ctx, ctx.Repo.Repository)
|
|
if err != nil {
|
|
log.Error("GetLanguageStats failed: %v", err)
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
resp := make(languageResponse, len(langs))
|
|
copy(resp, langs)
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
}
|