package hooks import ( "fmt" "path/filepath" "strings" ) type gitWantCommand struct { entry, name string command GitCommand } type gitWant struct { destination gitDestination commands []gitWantCommand content []byte } func gitQuote(value string) string { r := strings.NewReplacer("\\", "\\\\", "\"", "\\\"", "\t", "\\t") return "\"" + r.Replace(value) + "\"" } func shellQuote(path string) string { // Backslashes are valid POSIX filename bytes; normalize only Windows paths. if len(path) > 2 && path[1] == ':' || strings.HasPrefix(path, `\\`) { path = strings.ReplaceAll(path, "\\", "/") } return "'" + strings.ReplaceAll(path, "'", "'\\''") + "'" } func (s *Service) renderGit(d *desired, root string, entries map[string]Entry) error { var commands []gitWantCommand for _, name := range sortedKeys(entries) { e := entries[name] if !e.IsEnabled() { continue } for _, friendly := range sortedKeys(e.Bindings["git"].Commands) { commands = append(commands, gitWantCommand{entry: name, name: friendly, command: e.Bindings["git"].Commands[friendly]}) } } if len(commands) == 0 { return nil } if _, err := s.gitRunner().Run("", nil, "version"); err != nil { d.skipGitRoot(root) for _, c := range commands { d.notes = append(d.notes, Change{Target: "git", Root: root, Name: c.entry, Action: "inactive", Message: "git not found; outputs skipped"}) } return nil } dest, err := s.gitDestination(root) if err != nil { d.skipGitRoot(root) for _, c := range commands { d.notes = append(d.notes, Change{Target: "git", Root: root, Name: c.entry, Action: "inactive", Message: err.Error()}) } return nil } if d.git == nil { d.git = map[string]gitWant{} } if old, exists := d.git[dest.identity]; exists { return fmt.Errorf("project roots %s and %s share Git destination %s; declare it once", old.destination.root, root, dest.hooksFile) } seen := map[string]bool{} var out strings.Builder fmt.Fprintf(&out, "# Generated by Skillshare from %s.\n# Changes made here are reported as conflicts; edit hooks with `skillshare hooks`.\n", strings.ReplaceAll(s.ConfigPath, "\n", " ")) for _, c := range commands { if seen[c.name] { return fmt.Errorf("two hooks define hook.%s in %s", c.name, dest.hooksFile) } seen[c.name] = true command := strings.ReplaceAll(c.command.Command, "{files}", shellQuote(filepath.Join(dest.base, "skillshare", "files", c.entry))) fmt.Fprintf(&out, "[hook %s]\n\tcommand = %s\n", gitQuote(c.name), gitQuote(command)) for _, event := range c.command.Events { fmt.Fprintf(&out, "\tevent = %s\n", gitQuote(event)) } if c.command.Parallel != nil { fmt.Fprintf(&out, "\tparallel = %s\n", gitQuote(fmt.Sprint(*c.command.Parallel))) } } content := []byte(out.String()) d.git[dest.identity] = gitWant{destination: dest, commands: commands, content: content} // Helpers use the existing whole-file ownership protocol. for _, name := range sortedKeys(entries) { e := entries[name] if !e.IsEnabled() { continue } for _, file := range sortedKeys(e.Bindings["git"].Files) { path := filepath.Join(dest.base, "skillshare", "files", name, file) d.files[path] = wantFile{target: "git", path: path, root: root, entry: name, content: []byte(strings.ReplaceAll(e.Bindings["git"].Files[file], "\r\n", "\n")), mode: 0755} } } return nil }