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

refs.go « ref « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fe12e34e41e7cbb1ffd47f7d8e229ebc2d4391a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ref

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"strings"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	pb "gitlab.com/gitlab-org/gitaly-proto/go"
	"gitlab.com/gitlab-org/gitaly/internal/helper"
	"golang.org/x/net/context"
)

var (
	master = []byte("refs/heads/master")
	// We declare the following functions in variables so that we can override them in our tests
	findBranchNames = _findBranchNames
	headReference   = _headReference
)

func handleGitCommand(w refsWriter, r io.Reader) error {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		if err := w.AddRef(scanner.Bytes()); err != nil {
			return err
		}
	}
	if err := scanner.Err(); err != nil {
		return err
	}
	return w.Flush()
}

func findRefs(writer refsWriter, repo *pb.Repository, pattern string, args ...string) error {
	repoPath, err := helper.GetRepoPath(repo)
	if err != nil {
		return err
	}

	helper.Debugf("FindRefs: RepoPath=%q Pattern=%q", repoPath, pattern)

	baseArgs := []string{"--git-dir", repoPath, "for-each-ref", pattern}

	if len(args) == 0 {
		args = append(baseArgs, "--format=%(refname)") // Default format
	} else {
		args = append(baseArgs, args...)
	}

	cmd, err := helper.GitCommandReader(args...)
	if err != nil {
		return err
	}
	defer cmd.Kill()

	if err := handleGitCommand(writer, cmd); err != nil {
		return err
	}

	return cmd.Wait()
}

// FindAllBranchNames creates a stream of ref names for all branches in the given repository
func (s *server) FindAllBranchNames(in *pb.FindAllBranchNamesRequest, stream pb.Ref_FindAllBranchNamesServer) error {
	return findRefs(newFindAllBranchNamesWriter(stream, s.MaxMsgSize), in.Repository, "refs/heads")
}

// FindAllTagNames creates a stream of ref names for all tags in the given repository
func (s *server) FindAllTagNames(in *pb.FindAllTagNamesRequest, stream pb.Ref_FindAllTagNamesServer) error {
	return findRefs(newFindAllTagNamesWriter(stream, s.MaxMsgSize), in.Repository, "refs/tags")
}

func _findBranchNames(repoPath string) ([][]byte, error) {
	var names [][]byte

	cmd, err := helper.GitCommandReader("--git-dir", repoPath, "for-each-ref", "refs/heads", "--format=%(refname)")
	if err != nil {
		return nil, err
	}
	defer cmd.Kill()

	scanner := bufio.NewScanner(cmd)
	for scanner.Scan() {
		names, _ = appendRef(names, scanner.Bytes())
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("reading standard input: %v", err)
	}

	if err := cmd.Wait(); err != nil {
		return nil, err
	}

	return names, nil
}

func _headReference(repoPath string) ([]byte, error) {
	var headRef []byte

	cmd, err := helper.GitCommandReader("--git-dir", repoPath, "rev-parse", "--symbolic-full-name", "HEAD")
	if err != nil {
		return nil, err
	}
	defer cmd.Kill()

	scanner := bufio.NewScanner(cmd)
	scanner.Scan()
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	headRef = scanner.Bytes()

	if err := cmd.Wait(); err != nil {
		return nil, err
	}

	return headRef, nil
}

func defaultBranchName(repoPath string) ([]byte, error) {
	branches, err := findBranchNames(repoPath)

	if err != nil {
		return nil, err
	}

	// Return empty ref name if there are no branches
	if len(branches) == 0 {
		return nil, nil
	}

	// Return first branch name if there's only one
	if len(branches) == 1 {
		return branches[0], nil
	}

	hasMaster := false
	headRef, err := headReference(repoPath)
	if err != nil {
		return nil, err
	}
	for _, branch := range branches {
		// Return HEAD if it corresponds to a branch
		if bytes.Equal(headRef, branch) {
			return headRef, nil
		}
		if bytes.Equal(branch, master) {
			hasMaster = true
		}
	}
	// Return `ref/names/master` if it exists
	if hasMaster {
		return master, nil
	}
	// If all else fails, return the first branch name
	return branches[0], nil
}

// FindDefaultBranchName returns the default branch name for the given repository
func (s *server) FindDefaultBranchName(ctx context.Context, in *pb.FindDefaultBranchNameRequest) (*pb.FindDefaultBranchNameResponse, error) {
	repoPath, err := helper.GetRepoPath(in.GetRepository())
	if err != nil {
		return nil, err
	}

	helper.Debugf("FindDefaultBranchName: RepoPath=%q", repoPath)

	defaultBranchName, err := defaultBranchName(repoPath)
	if err != nil {
		return nil, grpc.Errorf(codes.Internal, err.Error())
	}

	return &pb.FindDefaultBranchNameResponse{Name: defaultBranchName}, nil
}

func parseSortKey(sortKey pb.FindLocalBranchesRequest_SortBy) string {
	switch sortKey {
	case pb.FindLocalBranchesRequest_NAME:
		return "refname"
	case pb.FindLocalBranchesRequest_UPDATED_ASC:
		return "committerdate"
	case pb.FindLocalBranchesRequest_UPDATED_DESC:
		return "-committerdate"
	}

	panic("never reached") // famous last words
}

// FindLocalBranches creates a stream of branches for all local branches in the given repository
func (s *server) FindLocalBranches(in *pb.FindLocalBranchesRequest, stream pb.Ref_FindLocalBranchesServer) error {
	// %00 inserts the null character into the output (see for-each-ref docs)
	formatFlag := "--format=" + strings.Join(localBranchFormatFields, "%00")
	sortFlag := "--sort=" + parseSortKey(in.GetSortBy())
	writer := newFindLocalBranchesWriter(stream, s.MaxMsgSize)

	return findRefs(writer, in.Repository, "refs/heads", formatFlag, sortFlag)
}