File: tree_entry.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (39 lines) | stat: -rw-r--r-- 996 bytes parent folder | download | duplicates (4)
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
package git

import (
	"fmt"
	"path/filepath"
	"strconv"

	"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)

// NewTreeEntry is a helper to construct a gitalypb.TreeEntry from the provided parameters.
func NewTreeEntry(commitOid, rootPath string, filename, oidBytes, modeBytes []byte) (*gitalypb.TreeEntry, error) {
	var objectType gitalypb.TreeEntry_EntryType

	mode, err := strconv.ParseInt(string(modeBytes), 8, 32)
	if err != nil {
		return nil, fmt.Errorf("parse mode: %w", err)
	}

	oid := fmt.Sprintf("%02x", oidBytes)

	// Based on https://github.com/git/git/blob/v2.13.1/builtin/ls-tree.c#L67-L87
	switch mode & 0xf000 {
	case 0o160000:
		objectType = gitalypb.TreeEntry_COMMIT
	case 0o40000:
		objectType = gitalypb.TreeEntry_TREE
	default:
		objectType = gitalypb.TreeEntry_BLOB
	}

	return &gitalypb.TreeEntry{
		CommitOid: commitOid,
		Oid:       oid,
		Path:      []byte(filepath.Join(rootPath, string(filename))),
		Type:      objectType,
		Mode:      int32(mode),
	}, nil
}