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

heathcliff26 / gh-utility / 29606645449

17 Jul 2026 07:11PM UTC coverage: 77.54% (-5.2%) from 82.787%
29606645449

Pull #24

github

web-flow
Merge 229d7f78b into 2343e1e53
Pull Request #24: Add new commit command

254 of 338 new or added lines in 8 files covered. (75.15%)

2 existing lines in 2 files now uncovered.

435 of 561 relevant lines covered (77.54%)

0.86 hits per line

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

90.59
/pkg/git/git.go
1
package git
2

3
import (
4
        "fmt"
5
        "strings"
6

7
        "github.com/go-git/go-git/v5"
8
        "github.com/go-git/go-git/v5/plumbing/transport/http"
9
        "github.com/heathcliff26/gh-utility/pkg/utils"
10
)
11

12
// Clone the given repository into the specified directory.
13
// If the directory is empty, the repository name will be used as the directory name.
14
func CloneRepository(repoURL string, directory string, fetchDepth int) error {
1✔
15
        opt := newCloneOptions(repoURL, fetchDepth)
1✔
16

1✔
17
        if directory == "" {
2✔
18
                directory = extractRepositoryName(repoURL)
1✔
19
        }
1✔
20

21
        _, err := git.PlainClone(directory, false, opt)
1✔
22
        return err
1✔
23
}
24

25
func newCloneOptions(repoURL string, fetchDepth int) *git.CloneOptions {
1✔
26
        opt := &git.CloneOptions{
1✔
27
                URL:   repoURL,
1✔
28
                Depth: fetchDepth,
1✔
29
        }
1✔
30

1✔
31
        token := utils.GetToken()
1✔
32
        if token != "" {
2✔
33
                opt.Auth = &http.BasicAuth{
1✔
34
                        Username: "x-access-token",
1✔
35
                        Password: token,
1✔
36
                }
1✔
37
        }
1✔
38

39
        return opt
1✔
40
}
41

42
func extractRepositoryName(url string) string {
1✔
43
        url = extractRepositoryOwnerAndName(url)
1✔
44
        s := strings.Split(url, "/")
1✔
45
        return s[len(s)-1]
1✔
46
}
1✔
47

48
func extractRepositoryOwnerAndName(url string) string {
1✔
49
        s := strings.Split(url, ":")
1✔
50
        s = strings.Split(s[len(s)-1], "/")
1✔
51
        if len(s) == 1 {
2✔
52
                url = s[0]
1✔
53
        } else {
2✔
54
                url = s[len(s)-2] + "/" + s[len(s)-1]
1✔
55
        }
1✔
56
        return strings.TrimSuffix(url, ".git")
1✔
57
}
58

59
type Repository struct {
60
        repo *git.Repository
61
}
62

63
func OpenRepository(path string) (*Repository, error) {
1✔
64
        repo, err := git.PlainOpen(path)
1✔
65
        if err != nil {
2✔
66
                return nil, fmt.Errorf("failed to open repository: %w", err)
1✔
67
        }
1✔
68
        return &Repository{
1✔
69
                repo: repo,
1✔
70
        }, nil
1✔
71
}
72

73
// Scan the current worktree and return all changed files.
74
func (r *Repository) GetChangedFiles() ([]string, error) {
1✔
75
        tree, err := r.repo.Worktree()
1✔
76
        if err != nil {
1✔
NEW
77
                return nil, fmt.Errorf("failed to read repository worktree: %w", err)
×
NEW
78
        }
×
79
        status, err := tree.Status()
1✔
80
        if err != nil {
1✔
NEW
81
                return nil, fmt.Errorf("failed to read worktree status: %w", err)
×
NEW
82
        }
×
83
        result := make([]string, 0, 10)
1✔
84
        for file, fileStatus := range status {
2✔
85
                if fileStatus.Worktree != git.Unmodified || fileStatus.Staging != git.Unmodified {
2✔
86
                        result = append(result, file)
1✔
87
                }
1✔
88
        }
89
        return result, nil
1✔
90
}
91

92
// Return the Tree Hash of the current commit in the repository.
93
func (r *Repository) GetTreeHash() (string, error) {
1✔
94
        headRef, err := r.repo.Head()
1✔
95
        if err != nil {
2✔
96
                return "", fmt.Errorf("failed to get HEAD reference: %w", err)
1✔
97
        }
1✔
98
        commit, err := r.repo.CommitObject(headRef.Hash())
1✔
99
        if err != nil {
1✔
NEW
100
                return "", fmt.Errorf("failed to get current commit object: %w", err)
×
NEW
101
        }
×
102
        return commit.TreeHash.String(), nil
1✔
103
}
104

105
// Return the Hash of the current commit in the repository.
106
func (r *Repository) GetCommitHash() (string, error) {
1✔
107
        headRef, err := r.repo.Head()
1✔
108
        if err != nil {
2✔
109
                return "", fmt.Errorf("failed to get HEAD reference: %w", err)
1✔
110
        }
1✔
111
        return headRef.Hash().String(), nil
1✔
112
}
113

114
// Return the remote name.
115
func (r *Repository) GetRemote() (string, error) {
1✔
116
        remotes, err := r.repo.Remotes()
1✔
117
        if err != nil {
1✔
NEW
118
                return "", fmt.Errorf("failed to get remotes: %w", err)
×
NEW
119
        }
×
120
        if len(remotes) == 0 {
2✔
121
                return "", fmt.Errorf("no remotes found")
1✔
122
        }
1✔
123

124
        url := remotes[0].Config().URLs[0]
1✔
125
        return extractRepositoryOwnerAndName(url), nil
1✔
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc