// Copyright 2025 BackendServerTemplate Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package gitutils import ( "fmt" "github.com/SongZihuan/BackendServerTemplate/tool/utils/cleanstringutils" "github.com/SongZihuan/BackendServerTemplate/tool/utils/executils" "github.com/SongZihuan/BackendServerTemplate/tool/utils/filesystemutils" "strings" "sync" ) var hasGitOnce sync.Once var hasGit = false func HasGit() bool { hasGitOnce.Do(func() { hasGit = filesystemutils.IsDir("./.git") }) return hasGit } func GetLastCommit() (string, error) { return executils.RunOnline("git", "rev-parse", "HEAD") } func GetTagListWithFilter(filter func(string) bool) ([]string, error) { ret, err := executils.Run("git", "for-each-ref", "--sort=-creatordate", "--format", "%(refname:short)", "refs/tags/") if err != nil { return nil, err } fmt.Printf("\n=1=\n%s\n=1=\n", ret) ret = cleanstringutils.GetString(ret) fmt.Printf("\n=2=\n%s\n=2=\n", ret) tagListSrc := strings.Split(ret, "\n") fmt.Printf("tagListSrc length=%d\n", len(tagListSrc)) tagList := make([]string, 0, len(tagListSrc)) for _, tag := range tagListSrc { tag = strings.TrimSpace(tag) if tag == "" || (filter != nil && !filter(tag)) { fmt.Printf("generate: skip tag %s\n", tag) continue } tagList = append(tagList, tag) } return tagList, nil } func GetTagList() ([]string, error) { return GetTagListWithFilter(nil) } func GetTagCommit(tag string) (string, error) { return executils.RunOnline("git", "rev-list", "-n", "1", tag) } func GetFirstCommit() (string, error) { return executils.RunOnline("git", "rev-list", "--max-parents=0", "HEAD") }