// MsgSyncReq: run triggers for changed files // SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2021-2024 Simon Ruderich package sync import ( slashpath "path" "strings" "ruderich.org/simon/safcm" ) // queueTriggers queues all triggers applying to file. func (s *Sync) queueTriggers(file *safcm.File) { for _, path := range triggerPaths(file.Path) { if s.req.Files[path].TriggerCommands == nil { continue } // Queue each trigger only once if s.triggersActive[path] { s.log.Debugf( "files: %q: skipping trigger on %q, already active", file.Path, path) continue } s.log.Verbosef("files: %q: queuing trigger on %q", file.Path, path) s.triggers = append(s.triggers, path) s.triggersActive[path] = true } } // triggerPaths returns all possible trigger paths for path, that is the path // itself and all parent paths. The paths are returned in reverse order so // more specific triggers can override effects of less specific ones (first // "/" or ".", then the parents and finally path itself). func triggerPaths(path string) []string { // Slash separated paths are used for the configuration const sep = "/" if path == sep || path == "." { return []string{path} } parts := strings.Split(path, sep) if strings.HasPrefix(path, sep) { // Absolute path parts[0] = sep } else { // Relative path parts = append([]string{"."}, parts...) } var res []string for i := 0; i < len(parts); i++ { res = append(res, slashpath.Join(parts[:i+1]...)) } return res }