Leavitt/pkg/core/core.go

63 lines
1.5 KiB
Go

/*
Copyright © 2022 Sameer Rahmani <lxsameer@gnu.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package core
import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/spf13/cobra"
)
func FilterTargetFiles(state *State, storage *[]string) func(f *object.File) error {
return func(f *object.File) error {
isbin, err := f.IsBinary()
if err != nil {
return err
}
if !isbin && f.Mode.IsFile() {
*storage = append(*storage, f.Name)
}
return nil
}
}
func FindAllFiles(state *State) (*[]string, error) {
if state == nil {
panic("state is nil")
}
repo := state.Repo
head, err := repo.Head()
cobra.CheckErr(err)
commit, err := repo.CommitObject(head.Hash())
cobra.CheckErr(err)
tree, err := commit.Tree()
cobra.CheckErr(err)
var files []string
filter := FilterTargetFiles(state, &files)
err = tree.Files().ForEach(filter)
cobra.CheckErr(err)
return &files, nil
}