mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-28 10:04:49 +01:00 
			
		
		
		
	Refactor Git command functions to use WithXXX methods instead of exposing RunOpts. This change simplifies reuse across gitrepo and improves consistency, encapsulation, and maintainability of command options. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			79 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2015 The Gogs Authors. All rights reserved.
 | |
| // Copyright 2019 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package git
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"strings"
 | |
| 
 | |
| 	"code.gitea.io/gitea/modules/git/gitcmd"
 | |
| )
 | |
| 
 | |
| // NewTree create a new tree according the repository and tree id
 | |
| func NewTree(repo *Repository, id ObjectID) *Tree {
 | |
| 	return &Tree{
 | |
| 		ID:   id,
 | |
| 		repo: repo,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // SubTree get a subtree by the sub dir path
 | |
| 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
 | |
| }
 | |
| 
 | |
| // LsTree checks if the given filenames are in the tree
 | |
| func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
 | |
| 	cmd := gitcmd.NewCommand("ls-tree", "-z", "--name-only").
 | |
| 		AddDashesAndList(append([]string{ref}, filenames...)...)
 | |
| 
 | |
| 	res, _, err := cmd.WithDir(repo.Path).RunStdBytes(repo.Ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	filelist := make([]string, 0, len(filenames))
 | |
| 	for line := range bytes.SplitSeq(res, []byte{'\000'}) {
 | |
| 		filelist = append(filelist, string(line))
 | |
| 	}
 | |
| 
 | |
| 	return filelist, err
 | |
| }
 | |
| 
 | |
| // GetTreePathLatestCommit returns the latest commit of a tree path
 | |
| func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) {
 | |
| 	stdout, _, err := gitcmd.NewCommand("rev-list", "-1").
 | |
| 		AddDynamicArguments(refName).AddDashesAndList(treePath).
 | |
| 		WithDir(repo.Path).
 | |
| 		RunStdString(repo.Ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return repo.GetCommit(strings.TrimSpace(stdout))
 | |
| }
 |