Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ls_tree.go « gitpipe « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70c9374fc413fcef083c36e8ebd71b562cf3c95a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gitpipe

import (
	"context"
	"errors"
	"fmt"
	"io"
	"strings"

	"gitlab.com/gitlab-org/gitaly/v15/internal/git"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/lstree"
)

// lsTreeConfig is configuration for the LsTree pipeline step.
type lsTreeConfig struct {
	recursive  bool
	typeFilter func(*lstree.Entry) bool
	skipResult func(*RevisionResult) bool
}

// LsTreeOption is an option for the LsTree pipeline step.
type LsTreeOption func(cfg *lsTreeConfig)

// LsTreeWithRecursive will make LsTree recursive into subtrees.
func LsTreeWithRecursive() LsTreeOption {
	return func(cfg *lsTreeConfig) {
		cfg.recursive = true
	}
}

// LsTreeWithBlobFilter configures LsTree to only pass through blob objects.
func LsTreeWithBlobFilter() LsTreeOption {
	return func(cfg *lsTreeConfig) {
		cfg.typeFilter = func(e *lstree.Entry) bool { return e.Type == lstree.Blob }
	}
}

// LsTree runs git-ls-tree(1) for the given revisions. The returned channel will
// contain all object IDs listed by this command. This might include:
//   - Blobs
//   - Trees, unless you're calling it with LsTreeWithRecursive()
//   - Submodules, referring to the commit of the submodule
func LsTree(
	ctx context.Context,
	repo *localrepo.Repo,
	revision string,
	options ...LsTreeOption,
) RevisionIterator {
	var cfg lsTreeConfig
	for _, option := range options {
		option(&cfg)
	}

	resultChan := make(chan RevisionResult)
	go func() {
		defer close(resultChan)

		objectHash, err := repo.ObjectHash(ctx)
		if err != nil {
			sendRevisionResult(ctx, resultChan, RevisionResult{
				err: fmt.Errorf("detecting object hash: %w", err),
			})
			return
		}

		flags := []git.Option{
			git.Flag{Name: "-z"},
		}

		if cfg.recursive {
			flags = append(flags, git.Flag{Name: "-r"})
		}

		var stderr strings.Builder
		cmd, err := repo.Exec(ctx,
			git.SubCmd{
				Name:  "ls-tree",
				Flags: flags,
				Args:  []string{revision},
			},
			git.WithStderr(&stderr),
		)
		if err != nil {
			sendRevisionResult(ctx, resultChan, RevisionResult{
				err: fmt.Errorf("spawning ls-tree: %w", err),
			})
			return
		}

		parser := lstree.NewParser(cmd, objectHash)
		for {
			entry, err := parser.NextEntry()
			if err != nil {
				if errors.Is(err, io.EOF) {
					break
				}
				sendRevisionResult(ctx, resultChan, RevisionResult{
					err: fmt.Errorf("scanning ls-tree output: %w", err),
				})
				return
			}

			if cfg.typeFilter != nil && !cfg.typeFilter(entry) {
				continue
			}

			result := RevisionResult{
				OID:        entry.ObjectID,
				ObjectName: []byte(entry.Path),
			}

			if cfg.skipResult != nil && cfg.skipResult(&result) {
				continue
			}

			if isDone := sendRevisionResult(ctx, resultChan, result); isDone {
				return
			}
		}

		if err := cmd.Wait(); err != nil {
			sendRevisionResult(ctx, resultChan, RevisionResult{
				err: fmt.Errorf("ls-tree pipeline command: %w, stderr: %q", err, stderr.String()),
			})
			return
		}
	}()

	return &revisionIterator{
		ctx: ctx,
		ch:  resultChan,
	}
}