38 lines
635 B
Go
38 lines
635 B
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
type CommitInfo struct {
|
|
ShortSHA string
|
|
Message string
|
|
}
|
|
|
|
func GetCommitInfo(rootDir string) *CommitInfo {
|
|
cmd := exec.Command("git", "-C", rootDir, "log", "-1", "--format=%h %s")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
parts := strings.SplitN(strings.TrimSpace(string(output)), " ", 2)
|
|
if len(parts) < 2 {
|
|
return nil
|
|
}
|
|
|
|
return &CommitInfo{
|
|
ShortSHA: parts[0],
|
|
Message: parts[1],
|
|
}
|
|
}
|
|
|
|
func FormatCommitLine(info *CommitInfo) string {
|
|
if info == nil {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("> Commit: %s (%s)\n", info.ShortSHA, info.Message)
|
|
}
|