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

gittuf / gittuf / 13442438676

20 Feb 2025 06:45PM UTC coverage: 60.303% (+0.007%) from 60.296%
13442438676

push

github

web-flow
Merge pull request #844 from gittuf/gitinterface-fetch-single-object

gitinterface: Support fetching single object

5 of 7 new or added lines in 1 file covered. (71.43%)

6043 of 10021 relevant lines covered (60.3%)

35.09 hits per line

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

78.08
/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
func (r *Repository) PushRefSpec(remoteName string, refSpecs []string) error {
8✔
17
        args := []string{"push", remoteName}
8✔
18
        args = append(args, refSpecs...)
8✔
19

8✔
20
        _, err := r.executor(args...).executeString()
8✔
21
        if err != nil {
8✔
22
                return fmt.Errorf("unable to push: %w", err)
×
23
        }
×
24

25
        return nil
8✔
26
}
27

28
func (r *Repository) Push(remoteName string, refs []string) error {
4✔
29
        refSpecs := make([]string, 0, len(refs))
4✔
30
        for _, ref := range refs {
8✔
31
                refSpec, err := r.RefSpec(ref, "", true)
4✔
32
                if err != nil {
4✔
33
                        return err
×
34
                }
×
35
                refSpecs = append(refSpecs, refSpec)
4✔
36
        }
37

38
        return r.PushRefSpec(remoteName, refSpecs)
4✔
39
}
40

41
func (r *Repository) FetchRefSpec(remoteName string, refSpecs []string) error {
14✔
42
        args := []string{"fetch", remoteName}
14✔
43
        args = append(args, refSpecs...)
14✔
44

14✔
45
        _, err := r.executor(args...).executeString()
14✔
46
        if err != nil {
14✔
47
                return fmt.Errorf("unable to fetch: %w", err)
×
48
        }
×
49

50
        return nil
14✔
51
}
52

53
func (r *Repository) Fetch(remoteName string, refs []string, fastForwardOnly bool) error {
10✔
54
        refSpecs := make([]string, 0, len(refs))
10✔
55
        for _, ref := range refs {
18✔
56
                refSpec, err := r.RefSpec(ref, "", fastForwardOnly)
8✔
57
                if err != nil {
8✔
58
                        return err
×
59
                }
×
60
                refSpecs = append(refSpecs, refSpec)
8✔
61
        }
62

63
        return r.FetchRefSpec(remoteName, refSpecs)
10✔
64
}
65

66
func (r *Repository) FetchObject(remoteName string, objectID Hash) error {
1✔
67
        args := []string{"fetch", "--depth=1", remoteName, objectID.String()}
1✔
68
        _, err := r.executor(args...).executeString()
1✔
69
        if err != nil {
1✔
NEW
70
                return fmt.Errorf("unable to fetch object: %w", err)
×
NEW
71
        }
×
72

73
        return nil
1✔
74
}
75

76
func CloneAndFetchRepository(remoteURL, dir, initialBranch string, refs []string, bare bool) (*Repository, error) {
6✔
77
        if dir == "" {
6✔
78
                return nil, fmt.Errorf("target directory must be specified")
×
79
        }
×
80

81
        repo := &Repository{clock: clockwork.NewRealClock()}
6✔
82

6✔
83
        args := []string{"clone", remoteURL}
6✔
84
        if initialBranch != "" {
8✔
85
                initialBranch = strings.TrimPrefix(initialBranch, BranchRefPrefix)
2✔
86
                args = append(args, "--branch", initialBranch)
2✔
87
        }
2✔
88
        args = append(args, dir)
6✔
89

6✔
90
        if bare {
9✔
91
                args = append(args, "--bare")
3✔
92
                repo.gitDirPath = dir
3✔
93
        } else {
6✔
94
                repo.gitDirPath = path.Join(dir, ".git")
3✔
95
        }
3✔
96

97
        _, stdErr, err := repo.executor(args...).execute()
6✔
98
        if err != nil {
6✔
99
                return nil, fmt.Errorf("unable to clone repository: %s", stdErr)
×
100
        }
×
101

102
        return repo, repo.Fetch(DefaultRemoteName, refs, true)
6✔
103
}
104

105
func (r *Repository) CreateRemote(remoteName, remoteURL string) error {
12✔
106
        _, err := r.executor("remote", "add", remoteName, remoteURL).executeString()
12✔
107
        if err != nil {
12✔
108
                return fmt.Errorf("unable to add remote: %w", err)
×
109
        }
×
110

111
        return nil
12✔
112
}
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