From af19bf6c5b78be088997f5573e8f1841d877845e Mon Sep 17 00:00:00 2001 From: davrot Date: Sun, 9 Feb 2025 16:53:37 +0000 Subject: [PATCH] modules/git/repo_index.go aktualisiert --- modules/git/repo_index.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/modules/git/repo_index.go b/modules/git/repo_index.go index f45b6e6191..d7a3790698 100644 --- a/modules/git/repo_index.go +++ b/modules/git/repo_index.go @@ -6,6 +6,7 @@ package git import ( "bytes" "context" + "errors" "os" "path/filepath" "strings" @@ -102,6 +103,30 @@ func (repo *Repository) LsFiles(filenames ...string) ([]string, error) { return filelist, err } +// Gives a list of all files in a directory and below +func (repo *Repository) LsFilesFromDirectory(directory, branch string) ([]string, error) { + if branch == "" { + return nil, errors.New("branch not found in context URL") + } + + cmd := NewCommand(repo.Ctx, "ls-files").AddDynamicArguments("--with-tree="+branch) + if len(directory) > 0 { + cmd = NewCommand(repo.Ctx, "ls-files").AddDynamicArguments("--with-tree="+branch).AddDynamicArguments("--directory").AddDynamicArguments(directory) + } + res, stderror, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path}) + if err != nil { + return nil, err + } + + if len(stderror) > 0 { + return nil, errors.New(string(stderror)) + } + + lines := strings.Split(string(res), "\n") + + return lines, nil +} + // RemoveFilesFromIndex removes given filenames from the index - it does not check whether they are present. func (repo *Repository) RemoveFilesFromIndex(filenames ...string) error { objectFormat, err := repo.GetObjectFormat()