-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Tabular Diff for CSV files #14661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
8b5de2e
Moved CSV logic into base package.
KN4CK3R d8d9211
Added method to create a tabular diff.
KN4CK3R 6b06e9a
Added CSV compare context.
KN4CK3R eeb10bc
Prevent parser error for ' 1; "2"; 3'
KN4CK3R fdab153
Prevent nil-cells.
KN4CK3R 9fd31a2
Lint
KN4CK3R 16db3ba
Added csv diff template.
KN4CK3R d13a2cd
Use new table style in csv markup.
KN4CK3R 2b4eace
Lint & tests.
KN4CK3R 79bdeca
Fixed wrong value.
KN4CK3R db623ab
Added file size limit for csv rendering.
KN4CK3R f8b902b
Lazy read single file.
KN4CK3R 0753b92
Lazy read rows for full diff.
KN4CK3R 66ea833
Moved code to csv package.
KN4CK3R 828a4b9
Fixed method name.
KN4CK3R 34f4a9b
Merge branch 'master' into feature-csv-diff
KN4CK3R 1038cc5
Revert unrelated change.
KN4CK3R 88b69aa
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 1b34215
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
KN4CK3R 8f17016
Added unit tests for various csv changes.
KN4CK3R 3835cf0
Removed line-height.
KN4CK3R 23e1a0c
Merge branch 'master' into feature-csv-diff
lunny 33e325d
Merge branch 'master' into feature-csv-diff
zeripath 4808a16
Merge branch 'master' into feature-csv-diff
zeripath 260dd93
Don't use unthemed color.
KN4CK3R 15dca09
Added possible @ delimiter.
KN4CK3R 925596e
Changed copyright notice.
KN4CK3R 05a29ae
Encode content as UTF8.
KN4CK3R 5f8ce65
Merge branch 'master' into feature-csv-diff
6543 7f3728c
Merge branch 'master' into feature-csv-diff
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2018 The Gitea Authors. All rights reserved. | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package base | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
"errors" | ||
"regexp" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/translation" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
var quoteRegexp = regexp.MustCompile(`["'][\s\S]+?["']`) | ||
|
||
// CreateCsvReader creates a CSV reader with the given delimiter. | ||
func CreateCsvReader(rawBytes []byte, delimiter rune) *csv.Reader { | ||
rd := csv.NewReader(bytes.NewReader(rawBytes)) | ||
rd.Comma = delimiter | ||
rd.TrimLeadingSpace = true | ||
return rd | ||
} | ||
|
||
// CreateCsvReaderAndGuessDelimiter creates a CSV reader with a guessed delimiter. | ||
func CreateCsvReaderAndGuessDelimiter(rawBytes []byte) *csv.Reader { | ||
delimiter := guessDelimiter(rawBytes) | ||
return CreateCsvReader(rawBytes, delimiter) | ||
} | ||
|
||
// guessDelimiter scores the input CSV data against delimiters, and returns the best match. | ||
// Reads at most 10k bytes & 10 lines. | ||
func guessDelimiter(data []byte) rune { | ||
maxLines := 10 | ||
maxBytes := util.Min(len(data), 1e4) | ||
text := string(data[:maxBytes]) | ||
text = quoteRegexp.ReplaceAllLiteralString(text, "") | ||
lines := strings.SplitN(text, "\n", maxLines+1) | ||
lines = lines[:util.Min(maxLines, len(lines))] | ||
|
||
delimiters := []rune{',', ';', '\t', '|'} | ||
bestDelim := delimiters[0] | ||
bestScore := 0.0 | ||
for _, delim := range delimiters { | ||
score := scoreDelimiter(lines, delim) | ||
if score > bestScore { | ||
bestScore = score | ||
bestDelim = delim | ||
} | ||
} | ||
|
||
return bestDelim | ||
} | ||
|
||
// scoreDelimiter uses a count & regularity metric to evaluate a delimiter against lines of CSV | ||
func scoreDelimiter(lines []string, delim rune) float64 { | ||
countTotal := 0 | ||
countLineMax := 0 | ||
linesNotEqual := 0 | ||
|
||
for _, line := range lines { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
countLine := strings.Count(line, string(delim)) | ||
countTotal += countLine | ||
if countLine != countLineMax { | ||
if countLineMax != 0 { | ||
linesNotEqual++ | ||
} | ||
countLineMax = util.Max(countLine, countLineMax) | ||
} | ||
} | ||
|
||
return float64(countTotal) * (1 - float64(linesNotEqual)/float64(len(lines))) | ||
} | ||
|
||
// FormatCsvError converts csv errors into readable messages. | ||
func FormatCsvError(err error, locale translation.Locale) (string, error) { | ||
var perr *csv.ParseError | ||
if errors.As(err, &perr) { | ||
if perr.Err == csv.ErrFieldCount { | ||
return locale.Tr("repo.error.csv.invalid_field_count", perr.Line), nil | ||
} | ||
return locale.Tr("repo.error.csv.unexpected", perr.Line, perr.Column), nil | ||
} | ||
|
||
return "", err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package base | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateCsvReader(t *testing.T) { | ||
rd := CreateCsvReader([]byte{}, ',') | ||
assert.Equal(t, ',', rd.Comma) | ||
} | ||
|
||
func TestCreateCsvReaderAndGuessDelimiter(t *testing.T) { | ||
input := "a;b;c\n1;2;3\n4;5;6" | ||
|
||
rd := CreateCsvReaderAndGuessDelimiter([]byte(input)) | ||
assert.Equal(t, ';', rd.Comma) | ||
} | ||
|
||
func TestGuessDelimiter(t *testing.T) { | ||
var kases = map[string]rune{ | ||
"a": ',', | ||
"1,2": ',', | ||
"1;2": ';', | ||
"1\t2": '\t', | ||
"1|2": '|', | ||
"1,2,3;4,5,6;7,8,9\na;b;c": ';', | ||
"\"1,2,3,4\";\"a\nb\"\nc;d": ';', | ||
"<br/>": ',', | ||
} | ||
|
||
for k, v := range kases { | ||
assert.EqualValues(t, guessDelimiter([]byte(k)), v) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.