• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

happy-sdk / happy / 15980077914

30 Jun 2025 05:53PM UTC coverage: 46.685% (-5.3%) from 51.943%
15980077914

push

github

mkungla
wip: gohappy cmd

Signed-off-by: Marko Kungla <marko.kungla@gmail.com>

0 of 281 new or added lines in 9 files covered. (0.0%)

2059 existing lines in 30 files now uncovered.

7943 of 17014 relevant lines covered (46.69%)

97527.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cmd/gohappy/pkg/git/git.go
1
// SPDX-License-Identifier: Apache-2.0
2
//
3
// Copyright © 2025 The Happy Authors
4

5
package git
6

7
import (
8
        "bytes"
9
        "errors"
10
        "fmt"
11
        "os"
12
        "os/exec"
13
        "path/filepath"
14
        "strings"
15

16
        "github.com/happy-sdk/happy/pkg/options"
17
        "github.com/happy-sdk/happy/sdk/cli"
18
        "github.com/happy-sdk/happy/sdk/session"
19
)
20

21
var Error = errors.New("git")
22

23
// IsGitRepo checks if the given directory is a Git repository.
24
func IsGitRepo(path string) bool {
×
25
        gitDir := filepath.Join(path, ".git")
×
26
        _, err := os.Stat(gitDir)
×
27
        return err == nil || !os.IsNotExist(err)
×
28
}
×
29

NEW
30
func NewConfig() (*options.Spec, error) {
×
NEW
31
        return options.New("git",
×
NEW
32
                options.NewOption("repo.found", false),
×
NEW
33
                options.NewOption("repo.root", "").
×
NEW
34
                        Validator(func(opt options.Option) error {
×
NEW
35
                                repoRoot := opt.Value().String()
×
NEW
36
                                if repoRoot == "" {
×
NEW
37
                                        return nil
×
NEW
38
                                }
×
NEW
39
                                if !IsGitRepo(repoRoot) {
×
NEW
40
                                        return fmt.Errorf("not a valid Git repository: %s", repoRoot)
×
NEW
41
                                }
×
NEW
42
                                return nil
×
43
                        }),
44
                options.NewOption("repo.branch", ""),
45
                options.NewOption("repo.remote.name", ""),
46
                options.NewOption("repo.remote.url", ""),
47
                options.NewOption("repo.dirty", ""),
48
                options.NewOption("committer.name", ""),
49
                options.NewOption("committer.email", ""),
50
        )
51
}
52

NEW
53
func DetectGitRepo(sess *session.Context, config *options.Options) error {
×
NEW
54
        if !config.Get("local.wd").IsSet() {
×
NEW
55
                return fmt.Errorf("%w: local.wd is not set", Error)
×
NEW
56
        }
×
NEW
57
        dir := config.Get("local.wd").String()
×
58
        for {
×
59
                // Check if the current path is a Git repository
×
60
                if IsGitRepo(dir) {
×
NEW
61
                        if err := config.Set("git.repo.found", true); err != nil {
×
62
                                return err
×
63
                        }
×
NEW
64
                        if err := config.Set("git.repo.root", dir); err != nil {
×
65
                                return err
×
66
                        }
×
NEW
67
                        break
×
68
                }
69
                parent := filepath.Dir(dir)
×
70
                // Check if we've reached the root directory
×
71
                if parent == dir {
×
72
                        break
×
73
                }
74
                dir = parent
×
75
        }
NEW
76
        return nil
×
77
}
78

NEW
79
func LoadInfo(sess *session.Context, config *options.Options) error {
×
80

×
NEW
81
        if !config.Get("git.repo.found").Variable().Bool() {
×
82
                return nil
×
83
        }
×
84

NEW
85
        repoRoot := config.Get("git.repo.root").String()
×
86

×
87
        // Get current branch
×
88
        branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
×
89
        branchCmd.Dir = repoRoot
×
90
        branch, err := cli.ExecRaw(sess, branchCmd)
×
91
        if err != nil {
×
92
                return err
×
93
        }
×
NEW
94
        if err := config.Set("git.repo.branch", strings.TrimSpace(string(branch))); err != nil {
×
95
                return err
×
96
        }
×
97

98
        // Get remote name
99
        remoteCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "@{u}")
×
100
        remoteCmd.Dir = repoRoot
×
101
        remote, err := cli.ExecRaw(sess, remoteCmd)
×
102
        if err != nil {
×
103
                return err
×
104
        }
×
105
        remoteParts := strings.SplitN(strings.TrimSpace(string(remote)), "/", 2)
×
106
        if len(remoteParts) > 0 {
×
NEW
107
                if err := config.Set("git.repo.remote.name", strings.TrimSpace(remoteParts[0])); err != nil {
×
108
                        return err
×
109
                }
×
110
        }
111

112
        // Get origin URL
NEW
113
        remoteConfigKey := fmt.Sprintf("remote.%s.url", config.Get("git.repo.remote.name").String())
×
114
        remoteURLCmd := exec.Command("git", "config", "--get", remoteConfigKey)
×
115
        remoteURLCmd.Dir = repoRoot
×
116
        remoteURL, err := cli.ExecRaw(sess, remoteURLCmd)
×
117
        if err != nil {
×
118
                return err
×
119
        }
×
NEW
120
        if err := config.Set("git.repo.remote.url", strings.TrimSpace(string(remoteURL))); err != nil {
×
121
                return err
×
122
        }
×
123

124
        // Check for uncommitted changes
125
        statusCmd := exec.Command("git", "status", "--porcelain")
×
126
        statusCmd.Dir = repoRoot
×
127
        status, err := cli.ExecRaw(sess, statusCmd)
×
128
        if err != nil {
×
129
                return err
×
130
        }
×
131
        dirty := bytes.TrimSpace(status) != nil
×
NEW
132
        if err := config.Set("git.repo.dirty", dirty); err != nil {
×
133
                return err
×
134
        }
×
135

136
        // Get committer name
137
        committerCmd := exec.Command("git", "config", "user.name")
×
138
        committerCmd.Dir = repoRoot
×
139
        committer, err := cli.ExecRaw(sess, committerCmd)
×
140
        if err != nil {
×
141
                return err
×
142
        }
×
NEW
143
        if err := config.Set("git.committer.name", strings.TrimSpace(string(committer))); err != nil {
×
144
                return err
×
145
        }
×
146

147
        // Get committer email
148
        emailCmd := exec.Command("git", "config", "user.email")
×
149
        emailCmd.Dir = repoRoot
×
150
        email, err := cli.ExecRaw(sess, emailCmd)
×
151
        if err != nil {
×
152
                return err
×
153
        }
×
NEW
154
        if err := config.Set("git.committer.email", strings.TrimSpace(string(email))); err != nil {
×
155
                return err
×
156
        }
×
157

158
        return nil
×
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc