code.gitea.io/gitea@v1.19.3/modules/options/repo.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package options
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  
    12  	"code.gitea.io/gitea/modules/log"
    13  	"code.gitea.io/gitea/modules/setting"
    14  	"code.gitea.io/gitea/modules/util"
    15  )
    16  
    17  // GetRepoInitFile returns repository init files
    18  func GetRepoInitFile(tp, name string) ([]byte, error) {
    19  	cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
    20  	relPath := path.Join("options", tp, cleanedName)
    21  
    22  	// Use custom file when available.
    23  	customPath := path.Join(setting.CustomPath, relPath)
    24  	isFile, err := util.IsFile(customPath)
    25  	if err != nil {
    26  		log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
    27  	}
    28  	if isFile {
    29  		return os.ReadFile(customPath)
    30  	}
    31  
    32  	switch tp {
    33  	case "readme":
    34  		return Readme(cleanedName)
    35  	case "gitignore":
    36  		return Gitignore(cleanedName)
    37  	case "license":
    38  		return License(cleanedName)
    39  	case "label":
    40  		return Labels(cleanedName)
    41  	default:
    42  		return []byte{}, fmt.Errorf("Invalid init file type")
    43  	}
    44  }