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

gittuf / gittuf / 13148437301

05 Feb 2025 01:40AM UTC coverage: 63.172% (+0.05%) from 63.124%
13148437301

push

github

web-flow
Merge pull request #782 from gittuf/gitinterface-propagate

gitinterface: Add subtree workflow

73 of 116 new or added lines in 3 files covered. (62.93%)

5779 of 9148 relevant lines covered (63.17%)

53.93 hits per line

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

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

4
package gitinterface
5

6
import (
7
        "fmt"
8
        "os"
9
        "os/exec"
10
        "path"
11
        "strings"
12
        "testing"
13
)
14

15
// ResetDueToError reverses a change applied to a ref to the specified target
16
// ID. It is used to ensure a gittuf operation is atomic: if a gittuf operation
17
// fails, any changes made to the repository in refs/gittuf can be rolled back.
18
// Worktrees are not updated.
19
func (r *Repository) ResetDueToError(cause error, refName string, commitID Hash) error {
×
20
        if err := r.SetReference(refName, commitID); err != nil {
×
21
                return fmt.Errorf("unable to reset %s to %s, caused by following error: %w", refName, commitID.String(), cause)
×
22
        }
×
23
        return cause
×
24
}
25

26
func RemoteRef(refName, remoteName string) string {
10✔
27
        var remotePath string
10✔
28
        switch {
10✔
29
        case strings.HasPrefix(refName, BranchRefPrefix):
6✔
30
                // refs/heads/<path> -> refs/remotes/<remote>/<path>
6✔
31
                rest := strings.TrimPrefix(refName, BranchRefPrefix)
6✔
32
                remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
6✔
33
        case strings.HasPrefix(refName, TagRefPrefix):
2✔
34
                // refs/tags/<path> -> refs/tags/<path>
2✔
35
                remotePath = refName
2✔
36
        default:
2✔
37
                // refs/<path> -> refs/remotes/<remote>/<path>
2✔
38
                rest := strings.TrimPrefix(refName, RefPrefix)
2✔
39
                remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
2✔
40
        }
41

42
        return remotePath
10✔
43
}
44

45
// RestoreWorktree is a test helper to fix the worktree in tests where we need
46
// to operate in a checked out copy of the repository. This is primarily needed
47
// for support with older Git versions.
48
func (r *Repository) RestoreWorktree(t *testing.T) {
1✔
49
        t.Helper()
1✔
50

1✔
51
        worktree := r.gitDirPath
1✔
52
        if !r.IsBare() {
2✔
53
                worktree = strings.TrimSuffix(worktree, ".git") // TODO: this doesn't support detached git dir
1✔
54
        }
1✔
55
        cwd, err := os.Getwd()
1✔
56
        if err != nil {
1✔
NEW
57
                t.Fatal(err)
×
NEW
58
        }
×
59
        if err := os.Chdir(worktree); err != nil {
1✔
NEW
60
                t.Fatal(err)
×
NEW
61
        }
×
62
        defer os.Chdir(cwd) //nolint:errcheck
1✔
63

1✔
64
        if _, err := r.executor("restore", "--staged", ".").executeString(); err != nil {
1✔
65
                t.Fatal(err)
×
66
        }
×
67

68
        if _, err := r.executor("restore", ".").executeString(); err != nil {
1✔
69
                t.Fatal(err)
×
70
        }
×
71
}
72

73
// IsNiceGitVersion determines whether the version of git is "nice". Certain Git
74
// subcommands that gittuf uses were added in newer versions than some common
75
// client versions. Instead of using a workaround for all clients, we determine
76
// if we can use the newer features or instead need to use workarounds.
77
func isNiceGitVersion() (bool, error) {
2✔
78
        cmd := exec.Command("git", "--version")
2✔
79
        output, err := cmd.Output()
2✔
80
        if err != nil {
2✔
81
                return false, err
×
82
        }
×
83

84
        versionString := strings.TrimPrefix(strings.TrimSpace(string(output)), "git version ")
2✔
85

2✔
86
        var major, minor, patch int
2✔
87
        _, err = fmt.Sscanf(versionString, "%d.%d.%d", &major, &minor, &patch)
2✔
88
        if err != nil {
2✔
89
                return false, err
×
90
        }
×
91

92
        if major >= 2 && minor >= 38 {
4✔
93
                return true, nil
2✔
94
        }
2✔
95
        return false, nil
×
96
}
97

98
func testNameToRefName(testName string) string {
42✔
99
        return BranchReferenceName(strings.ReplaceAll(testName, " ", "__"))
42✔
100
}
42✔
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