2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2023-03-14 09:53:33 -04:00
|
|
|
"fmt"
|
2022-07-14 09:45:04 -04:00
|
|
|
"regexp"
|
2022-08-03 10:01:36 -04:00
|
|
|
"strings"
|
2022-07-14 09:45:04 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
git "github.com/go-git/go-git/v5"
|
2022-07-14 11:16:38 -04:00
|
|
|
)
|
2022-07-14 09:45:04 -04:00
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// Git represents a git repository.
|
2022-07-14 09:45:04 -04:00
|
|
|
type Git struct {
|
|
|
|
repo *git.Repository
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// New opens the git repository in current directory.
|
2022-07-14 09:45:04 -04:00
|
|
|
func New() (*Git, error) {
|
|
|
|
repo, err := git.PlainOpenWithOptions("", &git.PlainOpenOptions{DetectDotGit: true})
|
|
|
|
return &Git{repo: repo}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revision returns the current revision (HEAD) of the repository in the format used by go pseudo versions.
|
|
|
|
func (g *Git) Revision() (string, time.Time, error) {
|
|
|
|
commitRef, err := g.repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, err
|
|
|
|
}
|
|
|
|
commit, err := g.repo.CommitObject(commitRef.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, err
|
|
|
|
}
|
2022-12-02 10:34:11 -05:00
|
|
|
return commitRef.Hash().String()[:12], commit.Committer.When, nil
|
2022-07-14 09:45:04 -04:00
|
|
|
}
|
|
|
|
|
2022-08-03 10:01:36 -04:00
|
|
|
// ParsedBranchName returns the name of the current branch.
|
|
|
|
// Special characters are replaced with "-", and the name is lowercased and trimmed to 49 characters.
|
|
|
|
// This makes sure that the branch name is usable as a GCP image name.
|
|
|
|
func (g *Git) ParsedBranchName() (string, error) {
|
|
|
|
commitRef, err := g.repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
rxp, err := regexp.Compile("[^a-zA-Z0-9-]+")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := strings.ToLower(rxp.ReplaceAllString(commitRef.Name().Short(), "-"))
|
|
|
|
if len(branch) > 49 {
|
|
|
|
branch = branch[:49]
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(branch, "-"), nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// BranchName of current HEAD.
|
2022-08-19 09:55:44 -04:00
|
|
|
func (g *Git) BranchName() (string, error) {
|
|
|
|
commitRef, err := g.repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return commitRef.Name().Short(), nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 09:53:33 -04:00
|
|
|
// Path returns the path of the git repository.
|
|
|
|
func (g *Git) Path() (string, error) {
|
|
|
|
worktree, err := g.repo.Worktree()
|
2022-07-14 09:45:04 -04:00
|
|
|
if err != nil {
|
2023-03-14 09:53:33 -04:00
|
|
|
return "", fmt.Errorf("failed to get worktree: %w", err)
|
2022-07-14 11:16:38 -04:00
|
|
|
}
|
2023-03-14 09:53:33 -04:00
|
|
|
return worktree.Filesystem.Root(), nil
|
2022-07-14 11:16:38 -04:00
|
|
|
}
|