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

gittuf / gittuf / 15332782443

29 May 2025 08:20PM UTC coverage: 61.783% (-0.03%) from 61.809%
15332782443

push

github

web-flow
Merge pull request #1012 from gittuf/shallow-fetches

gitinterface: Support shallow fetches

9 of 17 new or added lines in 1 file covered. (52.94%)

7393 of 11966 relevant lines covered (61.78%)

34.62 hits per line

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

72.09
/internal/gitinterface/sync.go
1
// Copyright The gittuf Authors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package gitinterface
5

6
import (
7
        "fmt"
8
        "path"
9
        "strings"
10

11
        "github.com/jonboulle/clockwork"
12
)
13

14
const DefaultRemoteName = "origin"
15

16
type FetchOptions struct {
17
        Depth int
18
}
19

20
type FetchOption func(*FetchOptions)
21

NEW
22
func WithFetchDepth(depth int) FetchOption {
×
NEW
23
        return func(o *FetchOptions) {
×
NEW
24
                o.Depth = depth
×
NEW
25
        }
×
26
}
27

28
func (r *Repository) PushRefSpec(remoteName string, refSpecs []string) error {
8✔
29
        args := []string{"push", remoteName}
8✔
30
        args = append(args, refSpecs...)
8✔
31

8✔
32
        _, err := r.executor(args...).executeString()
8✔
33
        if err != nil {
8✔
34
                return fmt.Errorf("unable to push: %w", err)
×
35
        }
×
36

37
        return nil
8✔
38
}
39

40
func (r *Repository) Push(remoteName string, refs []string) error {
4✔
41
        refSpecs := make([]string, 0, len(refs))
4✔
42
        for _, ref := range refs {
8✔
43
                refSpec, err := r.RefSpec(ref, "", true)
4✔
44
                if err != nil {
4✔
45
                        return err
×
46
                }
×
47
                refSpecs = append(refSpecs, refSpec)
4✔
48
        }
49

50
        return r.PushRefSpec(remoteName, refSpecs)
4✔
51
}
52

53
func (r *Repository) FetchRefSpec(remoteName string, refSpecs []string, opts ...FetchOption) error {
14✔
54
        options := &FetchOptions{}
14✔
55
        for _, fn := range opts {
14✔
NEW
56
                fn(options)
×
NEW
57
        }
×
58

59
        args := []string{"fetch"}
14✔
60

14✔
61
        if options.Depth != 0 {
14✔
NEW
62
                args = append(args, "--depth", fmt.Sprintf("%d", options.Depth))
×
NEW
63
        }
×
64

65
        args = append(args, remoteName)
14✔
66
        args = append(args, refSpecs...)
14✔
67

14✔
68
        _, err := r.executor(args...).executeString()
14✔
69
        if err != nil {
14✔
70
                return fmt.Errorf("unable to fetch: %w", err)
×
71
        }
×
72

73
        return nil
14✔
74
}
75

76
func (r *Repository) Fetch(remoteName string, refs []string, fastForwardOnly bool, opts ...FetchOption) error {
10✔
77
        refSpecs := make([]string, 0, len(refs))
10✔
78
        for _, ref := range refs {
18✔
79
                refSpec, err := r.RefSpec(ref, "", fastForwardOnly)
8✔
80
                if err != nil {
8✔
81
                        return err
×
82
                }
×
83
                refSpecs = append(refSpecs, refSpec)
8✔
84
        }
85

86
        return r.FetchRefSpec(remoteName, refSpecs, opts...)
10✔
87
}
88

89
func (r *Repository) FetchObject(remoteName string, objectID Hash) error {
1✔
90
        args := []string{"fetch", remoteName, objectID.String()}
1✔
91
        _, err := r.executor(args...).executeString()
1✔
92
        if err != nil {
1✔
93
                return fmt.Errorf("unable to fetch object: %w", err)
×
94
        }
×
95

96
        return nil
1✔
97
}
98

99
func CloneAndFetchRepository(remoteURL, dir, initialBranch string, refs []string, bare bool) (*Repository, error) {
6✔
100
        if dir == "" {
6✔
101
                return nil, fmt.Errorf("target directory must be specified")
×
102
        }
×
103

104
        repo := &Repository{clock: clockwork.NewRealClock()}
6✔
105

6✔
106
        args := []string{"clone", remoteURL}
6✔
107
        if initialBranch != "" {
8✔
108
                initialBranch = strings.TrimPrefix(initialBranch, BranchRefPrefix)
2✔
109
                args = append(args, "--branch", initialBranch)
2✔
110
        }
2✔
111
        args = append(args, dir)
6✔
112

6✔
113
        if bare {
9✔
114
                args = append(args, "--bare")
3✔
115
                repo.gitDirPath = dir
3✔
116
        } else {
6✔
117
                repo.gitDirPath = path.Join(dir, ".git")
3✔
118
        }
3✔
119

120
        _, stdErr, err := repo.executor(args...).execute()
6✔
121
        if err != nil {
6✔
122
                return nil, fmt.Errorf("unable to clone repository: %s", stdErr)
×
123
        }
×
124

125
        return repo, repo.Fetch(DefaultRemoteName, refs, true)
6✔
126
}
127

128
func (r *Repository) CreateRemote(remoteName, remoteURL string) error {
12✔
129
        _, err := r.executor("remote", "add", remoteName, remoteURL).executeString()
12✔
130
        if err != nil {
12✔
131
                return fmt.Errorf("unable to add remote: %w", err)
×
132
        }
×
133

134
        return nil
12✔
135
}
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