MCPcopy Index your code
hub / github.com/sinclairtarget/git-who / Get

Method Get

internal/cache/backends/json.go:37–95  ·  view source on GitHub ↗

Reads all commits into memory!

(revs []string)

Source from the content-addressed store, hash-verified

35
36// Reads all commits into memory!
37func (b JSONBackend) Get(revs []string) (iter.Seq[git.Commit], func() error) {
38 lookingFor := map[string]bool{}
39 for _, rev := range revs {
40 lookingFor[rev] = true
41 }
42
43 var iterErr error
44 empty := slices.Values([]git.Commit{})
45 finish := func() error {
46 return iterErr
47 }
48
49 f, err := os.Open(b.Path)
50 if errors.Is(err, fs.ErrNotExist) {
51 // If file doesn't exist, don't treat as an error
52 return empty, finish
53 } else if err != nil {
54 iterErr = err
55 return empty, finish
56 }
57 defer f.Close() // Don't care about error closing when reading
58
59 dec := json.NewDecoder(f)
60
61 var commits []git.Commit
62
63 // In theory we shouldn't get any duplicates into the cache if we're
64 // careful about what we write to it. But let's make sure by detecting dups
65 // and throwing an error if we see one.
66 seen := map[string]bool{}
67
68 for {
69 var c git.Commit
70
71 err = dec.Decode(&c)
72 if err == io.EOF {
73 break
74 } else if err != nil {
75 iterErr = err
76 return slices.Values(commits), finish
77 }
78
79 hit, _ := lookingFor[c.Hash]
80 if hit {
81 if isDup, _ := seen[c.Hash]; isDup {
82 iterErr = fmt.Errorf(
83 "duplicate commit in cache: %s",
84 c.Hash,
85 )
86 return slices.Values(commits), finish
87 }
88
89 seen[c.Hash] = true
90 commits = append(commits, c)
91 }
92 }
93
94 return slices.Values(commits), finish

Callers 2

TestAddGetClearFunction · 0.95
TestAddGetAddGetFunction · 0.95

Calls 2

OpenMethod · 0.65
CloseMethod · 0.65

Tested by 2

TestAddGetClearFunction · 0.76
TestAddGetAddGetFunction · 0.76