1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-13 21:54:24 +00:00
gitea/vendor/code.gitea.io/git/tree_entry.go

291 lines
7.1 KiB
Go
Raw Normal View History

2016-11-03 22:16:01 +00:00
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"path/filepath"
"sort"
"strconv"
"strings"
)
2016-12-22 09:30:52 +00:00
// EntryMode the type of the object in the git tree
2016-11-03 22:16:01 +00:00
type EntryMode int
// There are only a few file modes in Git. They look like unix file modes, but they can only be
// one of these.
const (
2016-12-22 09:30:52 +00:00
// EntryModeBlob
EntryModeBlob EntryMode = 0100644
// EntryModeExec
EntryModeExec EntryMode = 0100755
// EntryModeSymlink
EntryModeSymlink EntryMode = 0120000
// EntryModeCommit
EntryModeCommit EntryMode = 0160000
// EntryModeTree
EntryModeTree EntryMode = 0040000
2016-11-03 22:16:01 +00:00
)
2016-12-22 09:30:52 +00:00
// TreeEntry the leaf in the git tree
2016-11-03 22:16:01 +00:00
type TreeEntry struct {
2016-12-22 09:30:52 +00:00
ID SHA1
2016-11-03 22:16:01 +00:00
Type ObjectType
mode EntryMode
name string
ptree *Tree
commited bool
size int64
sized bool
}
2016-12-22 09:30:52 +00:00
// Name returns the name of the entry
2016-11-03 22:16:01 +00:00
func (te *TreeEntry) Name() string {
return te.name
}
2016-12-22 09:30:52 +00:00
// Size returns the size of the entry
2016-11-03 22:16:01 +00:00
func (te *TreeEntry) Size() int64 {
if te.IsDir() {
return 0
} else if te.sized {
return te.size
}
stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
if err != nil {
return 0
}
te.sized = true
te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
return te.size
}
2016-12-22 09:30:52 +00:00
// IsSubModule if the entry is a sub module
2016-11-03 22:16:01 +00:00
func (te *TreeEntry) IsSubModule() bool {
2016-12-22 09:30:52 +00:00
return te.mode == EntryModeCommit
2016-11-03 22:16:01 +00:00
}
2016-12-22 09:30:52 +00:00
// IsDir if the entry is a sub dir
2016-11-03 22:16:01 +00:00
func (te *TreeEntry) IsDir() bool {
2016-12-22 09:30:52 +00:00
return te.mode == EntryModeTree
2016-11-03 22:16:01 +00:00
}
2016-12-22 09:30:52 +00:00
// IsLink if the entry is a symlink
func (te *TreeEntry) IsLink() bool {
return te.mode == EntryModeSymlink
}
// Blob retrun the blob object the entry
2016-11-03 22:16:01 +00:00
func (te *TreeEntry) Blob() *Blob {
return &Blob{
repo: te.ptree.repo,
TreeEntry: te,
}
}
2016-12-28 16:35:52 +00:00
// GetSubJumpablePathName return the full path of subdirectory jumpable ( contains only one directory )
func (te *TreeEntry) GetSubJumpablePathName() string {
if te.IsSubModule() || !te.IsDir() {
return ""
}
tree, err := te.ptree.SubTree(te.name)
if err != nil {
return te.name
}
entries, _ := tree.ListEntries()
if len(entries) == 1 && entries[0].IsDir() {
name := entries[0].GetSubJumpablePathName()
if name != "" {
return te.name + "/" + name
}
}
return te.name
}
2016-12-22 09:30:52 +00:00
// Entries a list of entry
2016-11-03 22:16:01 +00:00
type Entries []*TreeEntry
var sorter = []func(t1, t2 *TreeEntry) bool{
func(t1, t2 *TreeEntry) bool {
return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
},
func(t1, t2 *TreeEntry) bool {
return t1.name < t2.name
},
}
func (tes Entries) Len() int { return len(tes) }
func (tes Entries) Swap(i, j int) { tes[i], tes[j] = tes[j], tes[i] }
func (tes Entries) Less(i, j int) bool {
t1, t2 := tes[i], tes[j]
var k int
for k = 0; k < len(sorter)-1; k++ {
2016-11-12 11:09:25 +00:00
s := sorter[k]
2016-11-03 22:16:01 +00:00
switch {
2016-11-12 11:09:25 +00:00
case s(t1, t2):
2016-11-03 22:16:01 +00:00
return true
2016-11-12 11:09:25 +00:00
case s(t2, t1):
2016-11-03 22:16:01 +00:00
return false
}
}
return sorter[k](t1, t2)
}
2016-12-22 09:30:52 +00:00
// Sort sort the list of entry
2016-11-03 22:16:01 +00:00
func (tes Entries) Sort() {
sort.Sort(tes)
}
// getCommitInfoState transient state for getting commit info for entries
type getCommitInfoState struct {
entries map[string]*TreeEntry // map from filepath to entry
commits map[string]*Commit // map from entry name to commit
lastCommitHash string
lastCommit *Commit
treePath string
headCommit *Commit
nextSearchSize int // next number of commits to search for
2016-11-03 22:16:01 +00:00
}
func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string) *getCommitInfoState {
entriesByPath := make(map[string]*TreeEntry, len(entries))
for _, entry := range entries {
entriesByPath[filepath.Join(treePath, entry.Name())] = entry
}
return &getCommitInfoState{
entries: entriesByPath,
commits: make(map[string]*Commit, len(entriesByPath)),
treePath: treePath,
headCommit: headCommit,
nextSearchSize: 16,
}
}
// GetCommitsInfo gets information of all commits that are corresponding to these entries
2016-11-03 22:16:01 +00:00
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
state := initGetCommitInfoState(tes, commit, treePath)
if err := getCommitsInfo(state); err != nil {
return nil, err
}
commitsInfo := make([][]interface{}, len(tes))
for i, entry := range tes {
commit = state.commits[filepath.Join(treePath, entry.Name())]
switch entry.Type {
case ObjectCommit:
subModuleURL := ""
if subModule, err := state.headCommit.GetSubModule(entry.Name()); err != nil {
return nil, err
} else if subModule != nil {
subModuleURL = subModule.URL
}
subModuleFile := NewSubModuleFile(commit, subModuleURL, entry.ID.String())
commitsInfo[i] = []interface{}{entry, subModuleFile}
default:
commitsInfo[i] = []interface{}{entry, commit}
}
}
return commitsInfo, nil
2016-11-15 15:24:08 +00:00
}
func (state *getCommitInfoState) nextCommit(hash string) {
state.lastCommitHash = hash
state.lastCommit = nil
}
func (state *getCommitInfoState) commit() (*Commit, error) {
var err error
if state.lastCommit == nil {
state.lastCommit, err = state.headCommit.repo.GetCommit(state.lastCommitHash)
2016-11-03 22:16:01 +00:00
}
return state.lastCommit, err
}
2016-11-03 22:16:01 +00:00
func (state *getCommitInfoState) update(path string) error {
relPath, err := filepath.Rel(state.treePath, path)
if err != nil {
return nil
}
var entryPath string
if index := strings.IndexRune(relPath, '/'); index >= 0 {
entryPath = filepath.Join(state.treePath, relPath[:index])
} else {
entryPath = path
2016-11-15 15:24:08 +00:00
}
if _, ok := state.entries[entryPath]; !ok {
return nil
} else if _, ok := state.commits[entryPath]; ok {
return nil
}
state.commits[entryPath], err = state.commit()
return err
}
2016-11-15 15:24:08 +00:00
func getCommitsInfo(state *getCommitInfoState) error {
for len(state.entries) > len(state.commits) {
if err := getNextCommitInfos(state); err != nil {
return err
}
}
return nil
}
2016-11-03 22:16:01 +00:00
func getNextCommitInfos(state *getCommitInfoState) error {
logOutput, err := logCommand(state.lastCommitHash, state).RunInDir(state.headCommit.repo.Path)
if err != nil {
return err
}
lines := strings.Split(logOutput, "\n")
i := 0
for i < len(lines) {
state.nextCommit(lines[i])
i++
for ; i < len(lines); i++ {
path := lines[i]
if path == "" {
2016-11-03 22:16:01 +00:00
break
}
state.update(path)
2016-11-03 22:16:01 +00:00
}
i++ // skip blank line
if len(state.entries) == len(state.commits) {
break
2016-11-03 22:16:01 +00:00
}
}
return nil
}
2016-11-03 22:16:01 +00:00
func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command {
var commitHash string
if len(exclusiveStartHash) == 0 {
2017-06-01 01:43:11 +00:00
commitHash = state.headCommit.ID.String()
} else {
commitHash = exclusiveStartHash + "^"
2016-11-03 22:16:01 +00:00
}
var command *Command
numRemainingEntries := len(state.entries) - len(state.commits)
if numRemainingEntries < 32 {
searchSize := (numRemainingEntries + 1) / 2
command = NewCommand("log", prettyLogFormat, "--name-only",
"-"+strconv.Itoa(searchSize), commitHash, "--")
for path, entry := range state.entries {
if _, ok := state.commits[entry.Name()]; !ok {
command.AddArguments(path)
}
}
} else {
command = NewCommand("log", prettyLogFormat, "--name-only",
"-"+strconv.Itoa(state.nextSearchSize), commitHash, "--", state.treePath)
2016-11-03 22:16:01 +00:00
}
state.nextSearchSize += state.nextSearchSize
return command
2016-11-03 22:16:01 +00:00
}