git.go

 1package resolvers
 2
 3import (
 4	"context"
 5
 6	"github.com/git-bug/git-bug/api/graphql/connections"
 7	"github.com/git-bug/git-bug/api/graphql/graph"
 8	"github.com/git-bug/git-bug/api/graphql/models"
 9	"github.com/git-bug/git-bug/cache"
10	"github.com/git-bug/git-bug/repository"
11)
12
13const blobTruncateSize = 1 << 20 // 1 MiB
14
15var _ graph.GitCommitResolver = &gitCommitResolver{}
16
17type gitCommitResolver struct {
18	cache *cache.MultiRepoCache
19}
20
21func (r gitCommitResolver) ShortHash(_ context.Context, obj *models.GitCommitMeta) (string, error) {
22	s := string(obj.Hash)
23	if len(s) > 8 {
24		s = s[:8]
25	}
26	return s, nil
27}
28
29func (r gitCommitResolver) FullMessage(_ context.Context, obj *models.GitCommitMeta) (string, error) {
30	repo := obj.Repo.BrowseRepo()
31	detail, err := repo.CommitDetail(obj.Hash)
32	if err != nil {
33		return "", err
34	}
35	return detail.FullMessage, nil
36}
37
38func (r gitCommitResolver) Parents(_ context.Context, obj *models.GitCommitMeta) ([]string, error) {
39	out := make([]string, len(obj.Parents))
40	for i, h := range obj.Parents {
41		out[i] = string(h)
42	}
43	return out, nil
44}
45
46func (r gitCommitResolver) Files(_ context.Context, obj *models.GitCommitMeta, after *string, before *string, first *int, last *int) (*models.GitChangedFileConnection, error) {
47	repo := obj.Repo.BrowseRepo()
48	detail, err := repo.CommitDetail(obj.Hash)
49	if err != nil {
50		return nil, err
51	}
52
53	input := models.ConnectionInput{After: after, Before: before, First: first, Last: last}
54	edger := func(f repository.ChangedFile, offset int) connections.Edge {
55		return connections.CursorEdge{Cursor: connections.OffsetToCursor(offset)}
56	}
57	conMaker := func(_ []*connections.CursorEdge, nodes []repository.ChangedFile, info *models.PageInfo, total int) (*models.GitChangedFileConnection, error) {
58		ptrs := make([]*repository.ChangedFile, len(nodes))
59		for i := range nodes {
60			ptrs[i] = &nodes[i]
61		}
62		return &models.GitChangedFileConnection{Nodes: ptrs, PageInfo: info, TotalCount: total}, nil
63	}
64	return connections.Connection(detail.Files, edger, conMaker, input)
65}
66
67func (r gitCommitResolver) Diff(_ context.Context, obj *models.GitCommitMeta, path string) (*repository.FileDiff, error) {
68	repo := obj.Repo.BrowseRepo()
69	fd, err := repo.CommitFileDiff(obj.Hash, path)
70	if err != nil {
71		return nil, err
72	}
73	return &fd, nil
74}