Leavitt/pkg/core/core.go

76 lines
1.6 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 (
"fmt"
"io"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func FindAllFiles(state *State, files chan string) {
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)
filesiter := tree.Files()
for {
f, err := filesiter.Next()
if err == io.EOF {
break
}
if err != nil {
state.ErrChannel <- err
continue
}
if f == nil {
state.ErrChannel <- fmt.Errorf("'f' is nil. File a BUG report please")
continue
}
isbin, err := f.IsBinary()
if err != nil {
state.ErrChannel <- err
continue
}
if !isbin && f.Mode.IsFile() {
files <- f.Name
}
}
cobra.CheckErr(err)
logrus.Debug("closing files channel")
state.WaitGroup.Done()
close(files)
}