github.com/devster/tarreleaser@v0.0.0-20221207180803-c608f4eb8918/pkg/pipe/gitinfo/gitinfo.go (about) 1 package gitinfo 2 3 import ( 4 "fmt" 5 "github.com/apex/log" 6 "github.com/devster/tarreleaser/pkg/context" 7 "github.com/devster/tarreleaser/pkg/git" 8 "github.com/devster/tarreleaser/pkg/pipe" 9 "github.com/pkg/errors" 10 "os" 11 ) 12 13 type Pipe struct{} 14 15 func (Pipe) String() string { 16 return "gathering git info" 17 } 18 19 func (Pipe) Default(ctx *context.Context) error { 20 return nil 21 } 22 23 func (Pipe) Run(ctx *context.Context) error { 24 if !git.HasGit() { 25 return pipe.Skip("git not found in PATH, skipping") 26 } 27 28 if !git.IsRepo() { 29 return pipe.Skip("current dir is not a git repository, skipping") 30 } 31 32 gitinfo, err := getInfo() 33 if err != nil { 34 return err 35 } 36 37 ctx.Git = gitinfo 38 log.WithFields(log.Fields{ 39 "tag": gitinfo.CurrentTag, 40 "branch": gitinfo.Branch, 41 "commit": fmt.Sprintf("%s %s - %s", gitinfo.ShortCommit, gitinfo.Commit.Author, gitinfo.Commit.Message), 42 }).Info("git info") 43 44 return nil 45 } 46 47 func getInfo() (context.GitInfo, error) { 48 short, err := getShortCommit() 49 if err != nil { 50 return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit") 51 } 52 53 full, err := getFullCommit() 54 if err != nil { 55 return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit") 56 } 57 58 branch, err := getBranch() 59 if err != nil { 60 return context.GitInfo{}, errors.Wrap(err, "couldn't get current branch") 61 } 62 63 author, err := getAuthor() 64 if err != nil { 65 return context.GitInfo{}, errors.Wrap(err, "couldn't get author name") 66 } 67 68 msg, err := getMessage() 69 if err != nil { 70 return context.GitInfo{}, errors.Wrap(err, "couldn't get commit message") 71 } 72 73 tag, err := getTag() 74 if err != nil { 75 log.WithError(err).Warn("No tags defined") 76 } 77 78 gitinfo := context.GitInfo{ 79 ShortCommit: short, 80 FullCommit: full, 81 CurrentTag: tag, 82 Branch: branch, 83 Commit: context.GitInfoCommit{ 84 Author: author, 85 Message: msg, 86 }, 87 } 88 89 if err = validateTag(gitinfo.CurrentTag); err != nil { 90 log.Warnf("git tag %v was not made against commit %v, skipping tag", gitinfo.CurrentTag, gitinfo.FullCommit) 91 gitinfo.CurrentTag = "" 92 } 93 94 return gitinfo, nil 95 } 96 97 func getShortCommit() (string, error) { 98 return git.Run("show", "--format=%h", "HEAD", "-q") 99 } 100 101 func getFullCommit() (string, error) { 102 return git.Run("show", "--format=%H", "HEAD", "-q") 103 } 104 105 func getAuthor() (string, error) { 106 return git.Run("show", "--format=%aN", "HEAD", "-q") 107 } 108 109 func getMessage() (string, error) { 110 return git.Run("show", "--format=%s", "HEAD", "-q") 111 } 112 113 func getTag() (string, error) { 114 return git.Run("describe", "--tags", "--abbrev=0") 115 } 116 117 func getBranch() (string, error) { 118 branch, err := git.Run("rev-parse", "--abbrev-ref", "HEAD") 119 if err != nil { 120 return "", err 121 } 122 123 // Provides consistent branch name on Travis 124 travisBranch := os.Getenv("TRAVIS_BRANCH") 125 if branch == "HEAD" && travisBranch != "" { 126 branch = travisBranch 127 } 128 129 return branch, nil 130 } 131 132 func validateTag(tag string) (err error) { 133 if tag == "" { 134 return 135 } 136 _, err = git.Run("describe", "--exact-match", "--tags", "--match", tag) 137 return 138 }