Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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_test.go +66−4

@@ -108,6 +108,68 @@ Binary files a/logo.png and b/logo.png differ
108 108 if len(files) != 1 || !files[0].Binary {
109 109 t.Fatalf("binary file not recognised: %+v", files)
110 110 }
111 // A binary file has no ---/+++ pair, so the diff --git header is the only
112 // place it names itself. Without those paths no raw URL can be built and the
113 // picture cannot be shown.
114 if files[0].OldPath != "logo.png" || files[0].NewPath != "logo.png" {
115 t.Fatalf("binary paths = %+v", files[0])
116 }
117 }
118
119 // A binary file that one side does not have says which side that is in a mode
120 // line, not in a /dev/null pair: showing a before image for a file that was added
121 // would fetch a blob that does not exist.
122 func TestParseDiffOfAnAddedOrDeletedBinary(t *testing.T) {
123 t.Parallel()
124 added := parseDiff(`diff --git a/new.png b/new.png
125 new file mode 100644
126 index 0000000..5555555
127 Binary files /dev/null and b/new.png differ
128 `)
129 if len(added) != 1 || added[0].OldPath != "" || added[0].NewPath != "new.png" {
130 t.Fatalf("added binary = %+v", added)
131 }
132 deleted := parseDiff(`diff --git a/gone.png b/gone.png
133 deleted file mode 100644
134 index 5555555..0000000
135 Binary files a/gone.png and /dev/null differ
136 `)
137 if len(deleted) != 1 || deleted[0].OldPath != "gone.png" || deleted[0].NewPath != "" {
138 t.Fatalf("deleted binary = %+v", deleted)
139 }
140 }
141
142 // Git quotes a name it cannot print byte for byte, and the quoting is per name, so
143 // one side of a rename may be quoted while the other is not.
144 func TestParseDiffOfQuotedPaths(t *testing.T) {
145 t.Parallel()
146 cases := []struct {
147 name, header, old, new string
148 }{
149 {
150 name: "both quoted",
151 header: `"a/caf\303\251.png" "b/caf\303\251.png"`,
152 old: "café.png", new: "café.png",
153 },
154 {
155 name: "only the new side quoted",
156 header: `a/plain.png "b/caf\303\251.png"`,
157 old: "plain.png", new: "café.png",
158 },
159 {
160 name: "only the old side quoted",
161 header: `"a/caf\303\251.png" b/plain.png`,
162 old: "café.png", new: "plain.png",
163 },
164 }
165 for _, tc := range cases {
166 t.Run(tc.name, func(t *testing.T) {
167 old, new := diffGitPaths(tc.header)
168 if old != tc.old || new != tc.new {
169 t.Fatalf("diffGitPaths(%q) = %q, %q; want %q, %q", tc.header, old, new, tc.old, tc.new)
170 }
171 })
172 }
111 173 }
112 174
113 175 func TestParseDiffOfANewFile(t *testing.T) {
@@ -140,10 +202,10 @@ rename to new.md
140 202 if len(files) != 1 {
141 203 t.Fatalf("files = %+v", files)
142 204 }
143 // A pure rename has no ---/+++ lines, so both paths are empty and the file is
144 // shown under the name git gave it in the diff header.
145 if files[0].OldPath != "" || files[0].NewPath != "" {
146 t.Fatalf("rename paths = %+v", files[0])
205 // A pure rename has no ---/+++ lines, so the diff --git header is what names
206 // it: both paths are read, and the language follows the new name.
207 if files[0].OldPath != "old.md" || files[0].NewPath != "new.md" || files[0].Language != "markdown" {
208 t.Fatalf("rename = %+v", files[0])
147 209 }
148 210 }
149 211