Files
skillshare/internal/hooks/git_render.go
T
Rusty Shackleford 1a2389dea2 feat(hooks): manage Git config hooks (#310)
Git 2.54 added named config hooks, but keeping their registrations and
helper paths consistent across machines still required manual config
edits. Bring them into the same preview, sync, and recovery workflow as
Agent hooks.

- Global and project scope, with linked worktrees sharing the common dir
- Portable helper files, collision detection, and ownership tracking of
  generated files and include.path entries
- Backups and restore preserve unrelated settings and conditional includes
- Management never executes hooks; Git below 2.54 is reported as inactive
  while outputs are still generated
- CLI/API inventory, raw YAML editing in the dashboard, localized docs
2026-10-02 02:56:54 +08:00

99 lines
3.3 KiB
Go

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
}