vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Name a binary file in the diff parser
ffbcb4cca7f6f7ac70ee3357ee519ef82d26173a · Keenan Nemetz · 11d ago · parent bd9ebad9
Name a binary file in the diff parser A file's old and new name came only from the ---/+++ pair, which git does not print for a binary file: it prints "Binary files ... differ" instead. A pure rename has neither. So every real PNG, STL or woff2 reached the page with no path at all. The commit page and a change request's Files tab hid that by falling back to the changed-file inventory row, which has the paths. The compare view renders what the parser produced, so there a picture had no name, no raw URL to fetch and no preview: an unnamed "Binary file: no line diff" section. Read both names from the diff --git header, which every file has, and take the absent side of an addition or deletion from its mode line so an added image does not ask for a "before" blob that never existed. Quoting is per name, so either side may be quoted while the other is not. The media fixture was "PNG" bytes with no NUL, so git called it text and the existing compare and request image tests never touched the binary path. It is binary now, which is the case a real PNG hits.
server/web/diff.go +59−3
| @@ -146,7 +146,8 @@ var ( | |||
| 146 | 146 | ||
| 147 | 147 | // parseDiff turns `git diff`/`git show` output into files, hunks and numbered | |
| 148 | 148 | // lines. Unrecognised metadata is dropped: a viewer that shows "index 3f2a.." is | |
| 149 | // showing the plumbing, not the change. | ||
| 149 | // showing the plumbing, not the change. Every file is named, whether git printed | ||
| 150 | // its names in the diff --git header, in a ---/+++ pair, or in both. | ||
| 150 | 151 | func parseDiff(patch string) []diffFile { | |
| 151 | 152 | var files []diffFile | |
| 152 | 153 | var cur *diffFile | |
| @@ -177,12 +178,24 @@ func parseDiff(patch string) []diffFile { | |||
| 177 | 178 | case strings.HasPrefix(line, "diff --git "): | |
| 178 | 179 | flushFile() | |
| 179 | 180 | cur = &diffFile{} | |
| 181 | // The header names both sides, and for a binary file it is the only | ||
| 182 | // place that does: git prints "Binary files ... differ" instead of a | ||
| 183 | // ---/+++ pair, and a pure rename has neither. Without this a picture | ||
| 184 | // has no path to build a raw URL from, and a renamed file has no name. | ||
| 185 | cur.OldPath, cur.NewPath = diffGitPaths(strings.TrimPrefix(line, "diff --git ")) | ||
| 180 | 186 | case cur == nil: | |
| 181 | 187 | continue | |
| 182 | 188 | case strings.HasPrefix(line, "--- "): | |
| 183 | 189 | cur.OldPath = diffPath(strings.TrimPrefix(line, "--- ")) | |
| 184 | 190 | case strings.HasPrefix(line, "+++ "): | |
| 185 | 191 | cur.NewPath = diffPath(strings.TrimPrefix(line, "+++ ")) | |
| 192 | // The header cannot say which side is absent; these two mode lines can, | ||
| 193 | // and they come before the binary marker rather than after a /dev/null | ||
| 194 | // pair that a binary file never gets. | ||
| 195 | case strings.HasPrefix(line, "new file mode"): | ||
| 196 | cur.OldPath = "" | ||
| 197 | case strings.HasPrefix(line, "deleted file mode"): | ||
| 198 | cur.NewPath = "" | ||
| 186 | 199 | case strings.HasPrefix(line, "Binary files ") || strings.HasPrefix(line, "GIT binary patch"): | |
| 187 | 200 | cur.Binary = true | |
| 188 | 201 | case metaRE.MatchString(line): | |
| @@ -213,8 +226,51 @@ func parseDiff(patch string) []diffFile { | |||
| 213 | 226 | return files | |
| 214 | 227 | } | |
| 215 | 228 | ||
| 216 | // diffPath normalises a ---/+++ path: strips the a/ or b/ prefix and the tab | ||
| 217 | // git appends when a path contains spaces. | ||
| 229 | // diffGitPaths reads the two names out of a `diff --git` line. Quoting is per | ||
| 230 | // name, so either side may be a quoted C string while the other is not; an | ||
| 231 | // unquoted pair is split at the b/ prefix of the second name. | ||
| 232 | func diffGitPaths(rest string) (oldPath, newPath string) { | ||
| 233 | if old, remainder, ok := cutQuotedPath(rest); ok { | ||
| 234 | remainder = strings.TrimSpace(remainder) | ||
| 235 | if newName, _, ok := cutQuotedPath(remainder); ok { | ||
| 236 | return diffPath(old), diffPath(newName) | ||
| 237 | } | ||
| 238 | return diffPath(old), diffPath(remainder) | ||
| 239 | } | ||
| 240 | // An unquoted old name ends where the new one starts. A quoted new name is | ||
| 241 | // preceded by a space and a quote, not by " b/". | ||
| 242 | if i := strings.LastIndex(rest, " b/"); i >= 0 { | ||
| 243 | return diffPath(rest[:i]), diffPath(rest[i+1:]) | ||
| 244 | } | ||
| 245 | if i := strings.LastIndex(rest, ` "`); i >= 0 { | ||
| 246 | if newName, _, ok := cutQuotedPath(rest[i+1:]); ok { | ||
| 247 | return diffPath(rest[:i]), diffPath(newName) | ||
| 248 | } | ||
| 249 | } | ||
| 250 | return "", "" | ||
| 251 | } | ||
| 252 | |||
| 253 | // cutQuotedPath reads one quoted path from the front of s, returning the token, | ||
| 254 | // the rest of the line, and whether there was one. Git escapes a byte it cannot | ||
| 255 | // print, so a backslash consumes the byte after it. | ||
| 256 | func cutQuotedPath(s string) (quoted, rest string, ok bool) { | ||
| 257 | if !strings.HasPrefix(s, `"`) { | ||
| 258 | return "", s, false | ||
| 259 | } | ||
| 260 | for i := 1; i < len(s); i++ { | ||
| 261 | switch s[i] { | ||
| 262 | case '\\': | ||
| 263 | i++ | ||
| 264 | case '"': | ||
| 265 | return s[:i+1], s[i+1:], true | ||
| 266 | } | ||
| 267 | } | ||
| 268 | return "", s, false | ||
| 269 | } | ||
| 270 | |||
| 271 | // diffPath normalises a path from a diff header: strips the a/ or b/ prefix, the | ||
| 272 | // tab git appends when a path contains spaces, and the quoting it uses for a name | ||
| 273 | // with an unprintable byte. /dev/null is the absent side of the file. | ||
| 218 | 274 | func diffPath(p string) string { | |
| 219 | 275 | if i := strings.IndexByte(p, '\t'); i >= 0 { | |
| 220 | 276 | p = p[:i] | |