1 // MsgSyncReq: run triggers for changed files
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024 Simon Ruderich
12 "ruderich.org/simon/safcm"
15 // queueTriggers queues all triggers applying to file.
16 func (s *Sync) queueTriggers(file *safcm.File) {
17 for _, path := range triggerPaths(file.Path) {
18 if s.req.Files[path].TriggerCommands == nil {
21 // Queue each trigger only once
22 if s.triggersActive[path] {
24 "files: %q: skipping trigger on %q, already active",
29 s.log.Verbosef("files: %q: queuing trigger on %q",
31 s.triggers = append(s.triggers, path)
32 s.triggersActive[path] = true
36 // triggerPaths returns all possible trigger paths for path, that is the path
37 // itself and all parent paths. The paths are returned in reverse order so
38 // more specific triggers can override effects of less specific ones (first
39 // "/" or ".", then the parents and finally path itself).
40 func triggerPaths(path string) []string {
41 // Slash separated paths are used for the configuration
43 if path == sep || path == "." {
46 parts := strings.Split(path, sep)
47 if strings.HasPrefix(path, sep) {
52 parts = append([]string{"."}, parts...)
56 for i := 0; i < len(parts); i++ {
57 res = append(res, slashpath.Join(parts[:i+1]...))