repolist/scan.go

99 lines
1.9 KiB
Go

package repolist
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"go.wit.com/log"
"go.wit.com/lib/gui/repostatus"
)
func (r *RepoList) RegisterHideFunction(f func(*RepoRow)) {
me.hideFunction = f
}
func (r *RepoList) ScanRepositories() (int, string) {
var i int
var shown int
var total int
t := TimeFunction(func() {
for _, repo := range me.allrepos {
i += 1
changed := repo.NewScan()
total += changed
}
var hidden int
for _, repo := range me.allrepos {
if repo.Hidden() {
hidden += 1
} else {
shown += 1
}
}
})
s := fmt.Sprint(t)
tmp := strconv.Itoa(shown) + " repos shown"
me.shownCount.SetText(tmp)
me.duration.SetText(s)
log.Log(REPOWARN, "Scanned", i, "repositories.", total, "changes in", s)
return i, s
}
func (r *RepoRow) NewScan() int {
var changed int = 0
if r.Status == nil {
log.Log(REPOWARN, "repo.Status = nil. not initialized for some reason")
return changed
}
// run the repostatus update
r.Status.Update()
if me.hideFunction == nil {
// application didn't register a hide function
// always show everything in that case
r.Show()
} else {
me.hideFunction(r)
}
// print out whatever changes have happened
if c, ok := r.Status.Changed(); ok {
log.Log(REPOWARN, "something finally changed")
c := strings.TrimSpace(c)
for _, line := range strings.Split(c, "\n") {
log.Log(REPOWARN, r.Status.Path(), line)
changed += 1
}
}
return changed
}
func ScanGitDirectories(srcDir string) []string {
var all []string
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Log(REPOWARN, "Error accessing path:", path, err)
return nil
}
// Check if the path is a directory and has a .git subdirectory
if info.IsDir() && repostatus.IsGitDir(path) {
all = append(all, path)
}
return nil
})
if err != nil {
log.Log(REPOWARN, "Error walking the path:", srcDir, err)
}
return all
}