| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/access" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | ) |
| 13 | |
| 14 | // SuggestionEdit vervangt de regels Start t/m End (1-based, inclusief) |
| 15 | // als die nog gelijk zijn aan Base. |
| 16 | type SuggestionEdit struct { |
| 17 | Path, Base, Replacement string |
| 18 | Start, End int |
| 19 | } |
| 20 | |
| 21 | // ParseSuggestion haalt de eerste ```suggestion-blok uit een reactie. |
| 22 | // De tekst is zonder de afsluitende newline van het blok. |
| 23 | func ParseSuggestion(body string) (string, bool) { |
| 24 | body = strings.ReplaceAll(body, "\r\n", "\n") |
| 25 | const open = "```suggestion" |
| 26 | i := strings.Index(body, open) |
| 27 | if i < 0 { |
| 28 | return "", false |
| 29 | } |
| 30 | rest := body[i+len(open):] |
| 31 | nl := strings.IndexByte(rest, '\n') |
| 32 | if nl < 0 { |
| 33 | return "", false |
| 34 | } |
| 35 | rest = rest[nl+1:] |
| 36 | end := strings.Index(rest, "```") |
| 37 | if end < 0 { |
| 38 | return "", false |
| 39 | } |
| 40 | return strings.TrimSuffix(rest[:end], "\n"), true |
| 41 | } |
| 42 | |
| 43 | // SplitLines splitst een bestand op newlines. Een afsluitende newline telt niet als lege regel. |
| 44 | func SplitLines(s string) []string { |
| 45 | if s == "" { |
| 46 | return nil |
| 47 | } |
| 48 | s = strings.TrimSuffix(s, "\n") |
| 49 | return strings.Split(s, "\n") |
| 50 | } |
| 51 | |
| 52 | // ApplySuggestions past alle edits toe. Bij een conflict of overlap verandert er niets. |
| 53 | func ApplySuggestions(files map[string]string, edits []SuggestionEdit) (map[string]string, error) { |
| 54 | byPath := map[string][]SuggestionEdit{} |
| 55 | for _, e := range edits { |
| 56 | byPath[e.Path] = append(byPath[e.Path], e) |
| 57 | } |
| 58 | out := map[string]string{} |
| 59 | changed := false |
| 60 | for path, group := range byPath { |
| 61 | raw, ok := files[path] |
| 62 | if !ok { |
| 63 | return nil, i18n.E("err.sugFile") |
| 64 | } |
| 65 | sort.Slice(group, func(i, j int) bool { return group[i].Start < group[j].Start }) |
| 66 | for i := 1; i < len(group); i++ { |
| 67 | if group[i].Start <= group[i-1].End { |
| 68 | return nil, i18n.E("err.sugOverlap", path) |
| 69 | } |
| 70 | } |
| 71 | lines := SplitLines(raw) |
| 72 | nl := strings.HasSuffix(raw, "\n") || raw == "" |
| 73 | sort.Slice(group, func(i, j int) bool { return group[i].Start > group[j].Start }) |
| 74 | for _, e := range group { |
| 75 | if e.Start < 1 || e.End < e.Start || e.End > len(lines) { |
| 76 | return nil, i18n.E("err.sugConflict", path, e.Start, e.End) |
| 77 | } |
| 78 | base := strings.Join(lines[e.Start-1:e.End], "\n") |
| 79 | if base != e.Base { |
| 80 | return nil, i18n.E("err.sugConflict", path, e.Start, e.End) |
| 81 | } |
| 82 | var mid []string |
| 83 | if e.Replacement != "" { |
| 84 | mid = strings.Split(e.Replacement, "\n") |
| 85 | } |
| 86 | next := append([]string{}, lines[:e.Start-1]...) |
| 87 | next = append(next, mid...) |
| 88 | next = append(next, lines[e.End:]...) |
| 89 | lines = next |
| 90 | } |
| 91 | next := "" |
| 92 | if len(lines) > 0 { |
| 93 | next = strings.Join(lines, "\n") |
| 94 | if nl { |
| 95 | next += "\n" |
| 96 | } |
| 97 | } |
| 98 | if next != raw { |
| 99 | changed = true |
| 100 | out[path] = next |
| 101 | } |
| 102 | } |
| 103 | if !changed { |
| 104 | return nil, i18n.E("err.sugSame") |
| 105 | } |
| 106 | return out, nil |
| 107 | } |
| 108 | |
| 109 | // CommitOnBranch schrijft files op branch en pusht één commit. |
| 110 | // De push loopt door de hook, met role en de beschermde patronen. |
| 111 | func CommitOnBranch(ctx context.Context, gitBin, bare, work, branch, author, email, title, description string, files map[string]string, role access.Role, patterns []string) error { |
| 112 | if err := os.RemoveAll(work); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | if err := os.MkdirAll(filepath.Dir(work), 0o755); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if _, err := run(ctx, gitBin, nil, "clone", bare, work); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | if _, err := run(ctx, gitBin, nil, "-C", work, "checkout", branch); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | var paths []string |
| 125 | for path, body := range files { |
| 126 | if !safeWorkPath(path) { |
| 127 | return i18n.E("err.sugFile") |
| 128 | } |
| 129 | dst := filepath.Join(work, filepath.FromSlash(path)) |
| 130 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 131 | return err |
| 132 | } |
| 133 | if err := os.WriteFile(dst, []byte(body), 0o644); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | paths = append(paths, path) |
| 137 | } |
| 138 | sort.Strings(paths) |
| 139 | args := append([]string{"-C", work, "add", "--"}, paths...) |
| 140 | if _, err := run(ctx, gitBin, nil, args...); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | env := identityEnv(author, email) |
| 144 | commit := []string{"-C", work, "-c", "commit.gpgsign=false", "commit", "-m", title} |
| 145 | if strings.TrimSpace(description) != "" { |
| 146 | commit = append(commit, "-m", description) |
| 147 | } |
| 148 | if _, err := run(ctx, gitBin, env, commit...); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | pushEnv := append(env, PushEnv(role, patterns, nil)...) |
| 152 | if _, err := run(ctx, gitBin, pushEnv, "-C", work, "push", "origin", "HEAD:"+branch); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | return nil |
| 156 | } |
| 157 | |
| 158 | func identityEnv(author, email string) []string { |
| 159 | env := make([]string, 0, len(os.Environ())+4) |
| 160 | for _, e := range os.Environ() { |
| 161 | if strings.HasPrefix(e, "GIT_AUTHOR_") || strings.HasPrefix(e, "GIT_COMMITTER_") { |
| 162 | continue |
| 163 | } |
| 164 | env = append(env, e) |
| 165 | } |
| 166 | return append(env, |
| 167 | "GIT_AUTHOR_NAME="+author, |
| 168 | "GIT_AUTHOR_EMAIL="+email, |
| 169 | "GIT_COMMITTER_NAME="+author, |
| 170 | "GIT_COMMITTER_EMAIL="+email, |
| 171 | ) |
| 172 | } |
| 173 | |
| 174 | func safeWorkPath(p string) bool { |
| 175 | if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") { |
| 176 | return false |
| 177 | } |
| 178 | return true |
| 179 | } |