| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "unicode/utf8" |
| 9 | ) |
| 10 | |
| 11 | // NotText is the line that replaces the hunks of a file that is not text in |
| 12 | // TextDiff, giving the file's size in bytes; a negative size is unknown. |
| 13 | func NotText(size int64) string { |
| 14 | if size < 0 { |
| 15 | return "[the content of this file is not text and was left out]" |
| 16 | } |
| 17 | return fmt.Sprintf("[the content of this file is not text and was left out: %d bytes]", size) |
| 18 | } |
| 19 | |
| 20 | // TextDiff returns diff, a unified diff of the repository at dir, as text |
| 21 | // for a model's prompt: valid UTF-8 without NUL bytes, as the CLIs refuse |
| 22 | // other input. A file whose hunks aren't valid UTF-8 or hold a NUL byte is |
| 23 | // not text, whatever git took it for: it keeps its header, so the reader |
| 24 | // still sees that it changed, and has its hunks replaced with the line |
| 25 | // NotText giving its size after the change, or before it when the change |
| 26 | // deletes it. With dir "", the sizes are unknown. |
| 27 | func TextDiff(ctx context.Context, dir, diff string) string { |
| 28 | return textDiff(diff, func(blob string) int64 { |
| 29 | if dir == "" { |
| 30 | return -1 |
| 31 | } |
| 32 | out, err := runGit(ctx, dir, "cat-file", "-s", blob) |
| 33 | if err != nil { |
| 34 | return -1 |
| 35 | } |
| 36 | n, err := strconv.ParseInt(out, 10, 64) |
| 37 | if err != nil { |
| 38 | return -1 |
| 39 | } |
| 40 | return n |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | // textDiff is TextDiff with size giving a blob's size in bytes, or -1. |
| 45 | func textDiff(diff string, size func(blob string) int64) string { |
| 46 | if isText(diff) { |
| 47 | return diff |
| 48 | } |
| 49 | var b strings.Builder |
| 50 | for _, file := range splitFiles(diff) { |
| 51 | if isText(file) { |
| 52 | b.WriteString(file) |
| 53 | continue |
| 54 | } |
| 55 | header, hunks := file, "" |
| 56 | if strings.HasPrefix(file, "@@ ") { |
| 57 | header, hunks = "", file |
| 58 | } else if i := strings.Index(file, "\n@@ "); i >= 0 { |
| 59 | header, hunks = file[:i+1], file[i+1:] |
| 60 | } |
| 61 | // Only a path can make a header invalid; keep it, marked. |
| 62 | b.WriteString(strings.ToValidUTF8(header, "�")) |
| 63 | if isText(hunks) { |
| 64 | b.WriteString(hunks) |
| 65 | continue |
| 66 | } |
| 67 | n := int64(-1) |
| 68 | if blob := changedBlob(header); blob != "" { |
| 69 | n = size(blob) |
| 70 | } |
| 71 | b.WriteString(NotText(n) + "\n") |
| 72 | } |
| 73 | return b.String() |
| 74 | } |
| 75 | |
| 76 | func isText(s string) bool { |
| 77 | return utf8.ValidString(s) && strings.IndexByte(s, 0) < 0 |
| 78 | } |
| 79 | |
| 80 | // changedBlob returns the object name a file's diff header gives for its |
| 81 | // content after the change, or before it when the change deletes the file; |
| 82 | // "" when the header has no index line. |
| 83 | func changedBlob(header string) string { |
| 84 | for _, l := range strings.Split(header, "\n") { |
| 85 | rest, ok := strings.CutPrefix(l, "index ") |
| 86 | if !ok { |
| 87 | continue |
| 88 | } |
| 89 | ids, _, _ := strings.Cut(rest, " ") |
| 90 | before, after, ok := strings.Cut(ids, "..") |
| 91 | if !ok { |
| 92 | return "" |
| 93 | } |
| 94 | if strings.Trim(after, "0") == "" { |
| 95 | return before |
| 96 | } |
| 97 | return after |
| 98 | } |
| 99 | return "" |
| 100 | } |
| 101 | |
| 102 | // splitFiles splits a diff before each "diff --git" line. |
| 103 | func splitFiles(diff string) []string { |
| 104 | var files []string |
| 105 | for diff != "" { |
| 106 | end := strings.Index(diff, "\ndiff --git ") |
| 107 | if end < 0 { |
| 108 | return append(files, diff) |
| 109 | } |
| 110 | files = append(files, diff[:end+1]) |
| 111 | diff = diff[end+1:] |
| 112 | } |
| 113 | return files |
| 114 | } |
| 115 | |
| 116 | // TruncateText cuts s to at most n bytes at a rune boundary, so valid UTF-8 |
| 117 | // stays valid. |
| 118 | func TruncateText(s string, n int) string { |
| 119 | if len(s) <= n { |
| 120 | return s |
| 121 | } |
| 122 | for n > 0 && !utf8.RuneStart(s[n]) { |
| 123 | n-- |
| 124 | } |
| 125 | return s[:n] |
| 126 | } |