2016-11-03 23:16:01 +01:00
// Copyright 2015 The Gogs Authors. All rights reserved.
2019-04-19 14:17:27 +02:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2016-11-03 23:16:01 +01:00
package git
import (
2021-07-20 15:16:20 +02:00
"bytes"
2016-11-03 23:16:01 +01:00
"strings"
)
2019-04-19 14:17:27 +02:00
// NewTree create a new tree according the repository and tree id
2023-12-13 21:02:00 +00:00
func NewTree ( repo * Repository , id ObjectID ) * Tree {
2016-11-03 23:16:01 +01:00
return & Tree {
ID : id ,
repo : repo ,
}
}
2016-12-22 17:30:52 +08:00
// SubTree get a sub tree by the sub dir path
2016-11-03 23:16:01 +01:00
func ( t * Tree ) SubTree ( rpath string ) ( * Tree , error ) {
if len ( rpath ) == 0 {
return t , nil
}
paths := strings . Split ( rpath , "/" )
var (
err error
g = t
p = t
te * TreeEntry
)
for _ , name := range paths {
te , err = p . GetTreeEntryByPath ( name )
if err != nil {
return nil , err
}
g , err = t . repo . getTree ( te . ID )
if err != nil {
return nil , err
}
g . ptree = p
p = g
}
return g , nil
}
2021-07-20 15:16:20 +02:00
// LsTree checks if the given filenames are in the tree
func ( repo * Repository ) LsTree ( ref string , filenames ... string ) ( [ ] string , error ) {
2022-10-23 22:44:45 +08:00
cmd := NewCommand ( repo . Ctx , "ls-tree" , "-z" , "--name-only" ) .
AddDashesAndList ( append ( [ ] string { ref } , filenames ... ) ... )
2022-04-01 10:55:30 +08:00
res , _ , err := cmd . RunStdBytes ( & RunOpts { Dir : repo . Path } )
2021-07-20 15:16:20 +02:00
if err != nil {
return nil , err
}
filelist := make ( [ ] string , 0 , len ( filenames ) )
for _ , line := range bytes . Split ( res , [ ] byte { '\000' } ) {
filelist = append ( filelist , string ( line ) )
}
return filelist , err
}