0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 19:34:33 +01:00
gitea/templates/repo/diff/csv_diff.tmpl
Richard Mahn 98f7013756
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.

We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.

This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.

This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.

There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.

I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.

Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 20:10:03 +01:00

58 lines
2.2 KiB
Handlebars

<tr>
<td>
{{$result := call .root.CreateCsvDiff .file .root.BaseCommit .root.HeadCommit}}
{{if $result.Error}}
<div class="ui center">{{$result.Error}}</div>
{{else if $result.Sections}}
<table class="data-table">
{{range $i, $section := $result.Sections}}
<tbody {{if gt $i 0}}class="section"{{end}}>
{{range $j, $row := $section.Rows}}
<tr>
{{if and (eq $i 0) (eq $j 0)}}
<th class="line-num">{{.RowIdx}}</th>
{{range $j, $cell := $row.Cells}}
{{if not $cell}}
<th></th>
{{else if eq $cell.Type 2}}
<th class="modified"><span class="removed-code">{{.LeftCell}}</span> <span class="added-code">{{.RightCell}}</span></th>
{{else if eq $cell.Type 3}}
<th class="added"><span class="added-code">{{.RightCell}}</span></th>
{{else if eq $cell.Type 4}}
<th class="removed"><span class="removed-code">{{.LeftCell}}</span></th>
{{else if eq $cell.Type 5}}
<th class="moved">{{.RightCell}}</th>
{{else if eq $cell.Type 6}}
<th class="moved"><span class="removed-code">{{.LeftCell}}</span> <span class="added-code">{{.RightCell}}</span></th>
{{else}}
<th>{{.RightCell}}</th>
{{end}}
{{end}}
{{else}}
<td class="line-num">{{if .RowIdx}}{{.RowIdx}}{{end}}</td>
{{range $j, $cell := $row.Cells}}
{{if not $cell}}
<td></td>
{{else if eq $cell.Type 2}}
<td class="modified"><span class="removed-code">{{.LeftCell}}</span> <span class="added-code">{{.RightCell}}</span></td>
{{else if eq $cell.Type 3}}
<td class="added"><span class="added-code">{{.RightCell}}</span></td>
{{else if eq $cell.Type 4}}
<td class="removed"><span class="removed-code">{{.LeftCell}}</span></td>
{{else if eq $cell.Type 5}}
<td class="moved">{{.RightCell}}</td>
{{else if eq $cell.Type 6}}
<td class="moved"><span class="removed-code">{{.LeftCell}}</span> <span class="added-code">{{.RightCell}}</span></td>
{{else}}
<td>{{.RightCell}}</td>
{{end}}
{{end}}
{{end}}
</tr>
{{end}}
</tbody>
{{end}}
</table>
{{end}}
</td>
</tr>