code.gitea.io/gitea@v1.21.7/services/repository/commit.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	gitea_ctx "code.gitea.io/gitea/modules/context"
    11  	"code.gitea.io/gitea/modules/util"
    12  )
    13  
    14  type ContainedLinks struct { // TODO: better name?
    15  	Branches      []*namedLink `json:"branches"`
    16  	Tags          []*namedLink `json:"tags"`
    17  	DefaultBranch string       `json:"default_branch"`
    18  }
    19  
    20  type namedLink struct { // TODO: better name?
    21  	Name    string `json:"name"`
    22  	WebLink string `json:"web_link"`
    23  }
    24  
    25  // LoadBranchesAndTags creates a new repository branch
    26  func LoadBranchesAndTags(ctx context.Context, baseRepo *gitea_ctx.Repository, commitSHA string) (*ContainedLinks, error) {
    27  	containedTags, err := baseRepo.GitRepo.ListOccurrences(ctx, "tag", commitSHA)
    28  	if err != nil {
    29  		return nil, fmt.Errorf("encountered a problem while querying %s: %w", "tags", err)
    30  	}
    31  	containedBranches, err := baseRepo.GitRepo.ListOccurrences(ctx, "branch", commitSHA)
    32  	if err != nil {
    33  		return nil, fmt.Errorf("encountered a problem while querying %s: %w", "branches", err)
    34  	}
    35  
    36  	result := &ContainedLinks{
    37  		DefaultBranch: baseRepo.Repository.DefaultBranch,
    38  		Branches:      make([]*namedLink, 0, len(containedBranches)),
    39  		Tags:          make([]*namedLink, 0, len(containedTags)),
    40  	}
    41  	for _, tag := range containedTags {
    42  		// TODO: Use a common method to get the link to a branch/tag instead of hard-coding it here
    43  		result.Tags = append(result.Tags, &namedLink{
    44  			Name:    tag,
    45  			WebLink: fmt.Sprintf("%s/src/tag/%s", baseRepo.RepoLink, util.PathEscapeSegments(tag)),
    46  		})
    47  	}
    48  	for _, branch := range containedBranches {
    49  		result.Branches = append(result.Branches, &namedLink{
    50  			Name:    branch,
    51  			WebLink: fmt.Sprintf("%s/src/branch/%s", baseRepo.RepoLink, util.PathEscapeSegments(branch)),
    52  		})
    53  	}
    54  	return result, nil
    55  }