2016-11-04 01:16:01 +03:00
// Copyright 2015 The Gogs Authors. All rights reserved.
2019-04-19 15:17:27 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2016-11-04 01:16:01 +03:00
package git
import (
2021-07-20 16:16:20 +03:00
"bytes"
2016-11-04 01:16:01 +03:00
"strings"
)
2019-04-19 15:17:27 +03:00
// NewTree create a new tree according the repository and tree id
2023-12-14 00:02:00 +03:00
func NewTree ( repo * Repository , id ObjectID ) * Tree {
2016-11-04 01:16:01 +03:00
return & Tree {
ID : id ,
repo : repo ,
}
}
2016-12-22 12:30:52 +03:00
// SubTree get a sub tree by the sub dir path
2016-11-04 01:16:01 +03: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 16:16:20 +03:00
// LsTree checks if the given filenames are in the tree
func ( repo * Repository ) LsTree ( ref string , filenames ... string ) ( [ ] string , error ) {
2022-10-23 17:44:45 +03:00
cmd := NewCommand ( repo . Ctx , "ls-tree" , "-z" , "--name-only" ) .
AddDashesAndList ( append ( [ ] string { ref } , filenames ... ) ... )
2022-04-01 05:55:30 +03:00
res , _ , err := cmd . RunStdBytes ( & RunOpts { Dir : repo . Path } )
2021-07-20 16:16:20 +03: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
}