| | | 1 | | package gogit |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "cmp" |
| | | 5 | | "context" |
| | | 6 | | "errors" |
| | | 7 | | "fmt" |
| | | 8 | | "os" |
| | | 9 | | "slices" |
| | | 10 | | "strings" |
| | | 11 | | "time" |
| | | 12 | | |
| | | 13 | | "github.com/go-git/go-git/v5" |
| | | 14 | | "github.com/go-git/go-git/v5/config" |
| | | 15 | | "github.com/go-git/go-git/v5/plumbing" |
| | | 16 | | "github.com/go-git/go-git/v5/plumbing/object" |
| | | 17 | | "github.com/go-git/go-git/v5/plumbing/storer" |
| | | 18 | | "github.com/go-git/go-git/v5/plumbing/transport" |
| | | 19 | | githttp "github.com/go-git/go-git/v5/plumbing/transport/http" |
| | | 20 | | |
| | | 21 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 22 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 23 | | ) |
| | | 24 | | |
| | | 25 | | // Compile-time interface compliance check. |
| | | 26 | | var _ ports.GitRepository = (*Repository)(nil) |
| | | 27 | | |
| | | 28 | | // Repository implements ports.GitRepository using go-git (pure Go, no CLI dependency). |
| | | 29 | | type Repository struct { |
| | | 30 | | repo *git.Repository |
| | | 31 | | workDir string |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // NewRepository opens an existing git repository at the given path. |
| | | 35 | | func NewRepository(workDir string) (*Repository, error) { |
| | | 36 | | repo, err := git.PlainOpen(workDir) |
| | | 37 | | if err != nil { |
| | | 38 | | return nil, fmt.Errorf("opening git repository at %s: %w", workDir, err) |
| | | 39 | | } |
| | | 40 | | return &Repository{repo: repo, workDir: workDir}, nil |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // CurrentBranch returns the name of the checked-out branch. |
| | 2 | 44 | | func (r *Repository) CurrentBranch(_ context.Context) (string, error) { |
| | 2 | 45 | | head, err := r.repo.Head() |
| | 0 | 46 | | if err != nil { |
| | 0 | 47 | | return "", fmt.Errorf("getting HEAD: %w", err) |
| | 0 | 48 | | } |
| | 2 | 49 | | return head.Name().Short(), nil |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // ListTags returns all tags in the repository. |
| | 4 | 53 | | func (r *Repository) ListTags(_ context.Context) ([]domain.Tag, error) { |
| | 4 | 54 | | tagRefs, err := r.repo.Tags() |
| | 0 | 55 | | if err != nil { |
| | 0 | 56 | | return nil, fmt.Errorf("listing tags: %w", err) |
| | 0 | 57 | | } |
| | | 58 | | |
| | 4 | 59 | | var tags []domain.Tag |
| | 3 | 60 | | err = tagRefs.ForEach(func(ref *plumbing.Reference) error { |
| | 3 | 61 | | hash := ref.Hash().String() |
| | 3 | 62 | | |
| | 3 | 63 | | // For annotated tags, resolve to the commit hash. |
| | 3 | 64 | | tagObj, tagErr := r.repo.TagObject(ref.Hash()) |
| | 3 | 65 | | isAnnotated := false |
| | 1 | 66 | | if tagErr == nil { |
| | 1 | 67 | | hash = tagObj.Target.String() |
| | 1 | 68 | | isAnnotated = true |
| | 1 | 69 | | } |
| | | 70 | | |
| | 3 | 71 | | tags = append(tags, domain.Tag{ |
| | 3 | 72 | | Name: ref.Name().Short(), |
| | 3 | 73 | | Hash: hash, |
| | 3 | 74 | | IsAnnotated: isAnnotated, |
| | 3 | 75 | | }) |
| | 3 | 76 | | return nil |
| | | 77 | | }) |
| | 0 | 78 | | if err != nil { |
| | 0 | 79 | | return nil, err |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | // Sort by name descending (version sort). |
| | 0 | 83 | | slices.SortFunc(tags, func(a, b domain.Tag) int { |
| | 0 | 84 | | return cmp.Compare(b.Name, a.Name) |
| | 0 | 85 | | }) |
| | | 86 | | |
| | 4 | 87 | | return tags, nil |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // CommitsSince returns commits since the given hash (exclusive). |
| | 3 | 91 | | func (r *Repository) CommitsSince(_ context.Context, sinceHash string) ([]domain.Commit, error) { |
| | 3 | 92 | | head, err := r.repo.Head() |
| | 0 | 93 | | if err != nil { |
| | 0 | 94 | | return nil, fmt.Errorf("getting HEAD: %w", err) |
| | 0 | 95 | | } |
| | | 96 | | |
| | 3 | 97 | | logOpts := &git.LogOptions{ |
| | 3 | 98 | | From: head.Hash(), |
| | 3 | 99 | | Order: git.LogOrderCommitterTime, |
| | 3 | 100 | | } |
| | 3 | 101 | | |
| | 3 | 102 | | iter, err := r.repo.Log(logOpts) |
| | 0 | 103 | | if err != nil { |
| | 0 | 104 | | return nil, fmt.Errorf("getting log: %w", err) |
| | 0 | 105 | | } |
| | | 106 | | |
| | 3 | 107 | | var commits []domain.Commit |
| | 3 | 108 | | err = iter.ForEach(func(c *object.Commit) error { |
| | 1 | 109 | | if sinceHash != "" && c.Hash.String() == sinceHash { |
| | 1 | 110 | | return storer.ErrStop |
| | 1 | 111 | | } |
| | | 112 | | |
| | 6 | 113 | | subject, body := splitMessage(c.Message) |
| | 6 | 114 | | |
| | 6 | 115 | | commits = append(commits, domain.Commit{ |
| | 6 | 116 | | Hash: c.Hash.String(), |
| | 6 | 117 | | Author: c.Author.Name, |
| | 6 | 118 | | AuthorEmail: c.Author.Email, |
| | 6 | 119 | | Date: c.Author.When, |
| | 6 | 120 | | Message: subject, |
| | 6 | 121 | | Body: body, |
| | 6 | 122 | | }) |
| | 6 | 123 | | return nil |
| | | 124 | | }) |
| | 0 | 125 | | if err != nil { |
| | 0 | 126 | | return nil, err |
| | 0 | 127 | | } |
| | | 128 | | |
| | 3 | 129 | | return commits, nil |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | // FilesChangedInCommit returns the list of files changed by a commit. |
| | 1 | 133 | | func (r *Repository) FilesChangedInCommit(_ context.Context, hash string) ([]string, error) { |
| | 1 | 134 | | commitObj, err := r.repo.CommitObject(plumbing.NewHash(hash)) |
| | 0 | 135 | | if err != nil { |
| | 0 | 136 | | return nil, fmt.Errorf("getting commit %s: %w", hash, err) |
| | 0 | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | stats, err := commitObj.Stats() |
| | 0 | 140 | | if err != nil { |
| | 0 | 141 | | return nil, fmt.Errorf("getting stats for %s: %w", hash, err) |
| | 0 | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | files := make([]string, 0, len(stats)) |
| | 1 | 145 | | for _, s := range stats { |
| | 1 | 146 | | files = append(files, s.Name) |
| | 1 | 147 | | } |
| | 1 | 148 | | return files, nil |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | // CreateTag creates a git tag at the given commit hash. |
| | 8 | 152 | | func (r *Repository) CreateTag(_ context.Context, name, hash, message string) error { |
| | 8 | 153 | | commitHash := plumbing.NewHash(hash) |
| | 8 | 154 | | |
| | 3 | 155 | | if message != "" { |
| | 3 | 156 | | // Create annotated tag. |
| | 3 | 157 | | _, err := r.repo.CreateTag(name, commitHash, &git.CreateTagOptions{ |
| | 3 | 158 | | Message: message, |
| | 3 | 159 | | Tagger: &object.Signature{ |
| | 3 | 160 | | Name: "semantic-release-bot", |
| | 3 | 161 | | Email: "semantic-release-bot@users.noreply.github.com", |
| | 3 | 162 | | When: time.Now(), |
| | 3 | 163 | | }, |
| | 3 | 164 | | }) |
| | 2 | 165 | | if err == nil { |
| | 2 | 166 | | return nil |
| | 2 | 167 | | } |
| | 1 | 168 | | if errors.Is(err, git.ErrTagExists) { |
| | 1 | 169 | | // Use the peel suffix "^{}" to dereference the annotated tag object |
| | 1 | 170 | | // to the underlying commit hash; without it ResolveRevision returns |
| | 1 | 171 | | // the tag object hash, which never matches the commit hash argument. |
| | 1 | 172 | | resolved, resolveErr := r.repo.ResolveRevision(plumbing.Revision("refs/tags/" + name + "^{}")) |
| | 1 | 173 | | if resolveErr == nil && resolved.String() == hash { |
| | 1 | 174 | | return domain.ErrTagAlreadyExists |
| | 1 | 175 | | } |
| | | 176 | | } |
| | 0 | 177 | | return fmt.Errorf("creating annotated tag %s: %w", name, err) |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | // Create lightweight tag — check for an existing ref first because |
| | | 181 | | // Storer.SetReference silently overwrites rather than erroring on duplicates. |
| | 5 | 182 | | existing, refErr := r.repo.Storer.Reference(plumbing.NewTagReferenceName(name)) |
| | 0 | 183 | | if refErr != nil && !errors.Is(refErr, plumbing.ErrReferenceNotFound) { |
| | 0 | 184 | | return fmt.Errorf("checking existing tag %s: %w", name, refErr) |
| | 0 | 185 | | } |
| | 1 | 186 | | if existing != nil { |
| | 1 | 187 | | if existing.Hash().String() == hash { |
| | 1 | 188 | | return domain.ErrTagAlreadyExists |
| | 1 | 189 | | } |
| | 0 | 190 | | return fmt.Errorf("tag %s already exists at a different commit: %w", name, git.ErrTagExists) |
| | | 191 | | } |
| | 4 | 192 | | ref := plumbing.NewReferenceFromStrings("refs/tags/"+name, hash) |
| | 4 | 193 | | return r.repo.Storer.SetReference(ref) |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | // PushTag pushes a tag to the remote. |
| | 2 | 197 | | func (r *Repository) PushTag(ctx context.Context, name string) error { |
| | 2 | 198 | | refSpec := config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", name, name)) |
| | 2 | 199 | | |
| | 2 | 200 | | auth := resolveAuth() |
| | 2 | 201 | | |
| | 2 | 202 | | err := r.repo.Push(&git.PushOptions{ |
| | 2 | 203 | | RemoteName: "origin", |
| | 2 | 204 | | RefSpecs: []config.RefSpec{refSpec}, |
| | 2 | 205 | | Auth: auth, |
| | 2 | 206 | | }) |
| | 2 | 207 | | // NoErrAlreadyUpToDate is a non-nil sentinel that go-git returns when the |
| | 2 | 208 | | // remote already has the ref at the same SHA. Treat it as success so that |
| | 2 | 209 | | // re-runs (where the tag was already pushed in a prior attempt) do not fail. |
| | 0 | 210 | | if err == nil || errors.Is(err, git.NoErrAlreadyUpToDate) { |
| | 0 | 211 | | return nil |
| | 0 | 212 | | } |
| | 2 | 213 | | return fmt.Errorf("pushing tag %s: %w", name, err) |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | // HeadHash returns the hash of HEAD. |
| | 1 | 217 | | func (r *Repository) HeadHash(_ context.Context) (string, error) { |
| | 1 | 218 | | head, err := r.repo.Head() |
| | 0 | 219 | | if err != nil { |
| | 0 | 220 | | return "", fmt.Errorf("getting HEAD: %w", err) |
| | 0 | 221 | | } |
| | 1 | 222 | | return head.Hash().String(), nil |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | // RemoteURL returns the remote origin URL. |
| | 2 | 226 | | func (r *Repository) RemoteURL(_ context.Context) (string, error) { |
| | 2 | 227 | | remote, err := r.repo.Remote("origin") |
| | 1 | 228 | | if err != nil { |
| | 1 | 229 | | return "", fmt.Errorf("getting remote: %w", err) |
| | 1 | 230 | | } |
| | 1 | 231 | | urls := remote.Config().URLs |
| | 0 | 232 | | if len(urls) == 0 { |
| | 0 | 233 | | return "", fmt.Errorf("no URLs configured for remote 'origin'") |
| | 0 | 234 | | } |
| | 1 | 235 | | return urls[0], nil |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | // Stage adds the given file paths to the worktree index. |
| | 3 | 239 | | func (r *Repository) Stage(_ context.Context, files []string) error { |
| | 1 | 240 | | if len(files) == 0 { |
| | 1 | 241 | | return nil |
| | 1 | 242 | | } |
| | 2 | 243 | | w, err := r.repo.Worktree() |
| | 0 | 244 | | if err != nil { |
| | 0 | 245 | | return fmt.Errorf("getting worktree: %w", err) |
| | 0 | 246 | | } |
| | 2 | 247 | | for _, file := range files { |
| | 0 | 248 | | if _, err := w.Add(file); err != nil { |
| | 0 | 249 | | return fmt.Errorf("staging %s: %w", file, err) |
| | 0 | 250 | | } |
| | | 251 | | } |
| | 2 | 252 | | return nil |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | // Commit creates a commit with the given message from the current index. |
| | 1 | 256 | | func (r *Repository) Commit(_ context.Context, message string) error { |
| | 1 | 257 | | w, err := r.repo.Worktree() |
| | 0 | 258 | | if err != nil { |
| | 0 | 259 | | return fmt.Errorf("getting worktree: %w", err) |
| | 0 | 260 | | } |
| | 1 | 261 | | _, err = w.Commit(message, &git.CommitOptions{ |
| | 1 | 262 | | Author: &object.Signature{ |
| | 1 | 263 | | Name: "semantic-release-bot", |
| | 1 | 264 | | Email: "semantic-release-bot@users.noreply.github.com", |
| | 1 | 265 | | When: time.Now(), |
| | 1 | 266 | | }, |
| | 1 | 267 | | }) |
| | 0 | 268 | | if err != nil { |
| | 0 | 269 | | return fmt.Errorf("committing: %w", err) |
| | 0 | 270 | | } |
| | 1 | 271 | | return nil |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | // Push pushes the current branch to origin. |
| | 2 | 275 | | func (r *Repository) Push(ctx context.Context) error { |
| | 2 | 276 | | auth := resolveAuth() |
| | 2 | 277 | | err := r.repo.Push(&git.PushOptions{ |
| | 2 | 278 | | RemoteName: "origin", |
| | 2 | 279 | | Auth: auth, |
| | 2 | 280 | | }) |
| | 1 | 281 | | if err == nil || errors.Is(err, git.NoErrAlreadyUpToDate) { |
| | 1 | 282 | | return nil |
| | 1 | 283 | | } |
| | 1 | 284 | | return fmt.Errorf("pushing branch: %w", err) |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | func splitMessage(msg string) (subject, body string) { |
| | | 288 | | parts := strings.SplitN(strings.TrimSpace(msg), "\n", 2) |
| | | 289 | | subject = strings.TrimSpace(parts[0]) |
| | | 290 | | if len(parts) > 1 { |
| | | 291 | | body = strings.TrimSpace(parts[1]) |
| | | 292 | | } |
| | | 293 | | return subject, body |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | func resolveAuth() transport.AuthMethod { |
| | | 297 | | // Try token-based auth for HTTPS remotes. |
| | | 298 | | for _, key := range []string{"GH_TOKEN", "GITHUB_TOKEN", "GL_TOKEN", "GITLAB_TOKEN", "BB_TOKEN", "BITBUCKET_TOKEN"} { |
| | | 299 | | if token := os.Getenv(key); token != "" { |
| | | 300 | | return &githttp.BasicAuth{ |
| | | 301 | | Username: "git", |
| | | 302 | | Password: token, |
| | | 303 | | } |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | return nil |
| | | 307 | | } |