Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Various fixes for diffs and file diff rendering#44805

Merged
eseliger merged 13 commits into
mainfrom
es/diff-fixes
Nov 28, 2022
Merged

Various fixes for diffs and file diff rendering#44805
eseliger merged 13 commits into
mainfrom
es/diff-fixes

Conversation

@eseliger

@eseliger eseliger commented Nov 25, 2022

Copy link
Copy Markdown
Member

Started into an investigation after this thread: https://sourcegraph.slack.com/archives/CMMTWQQ49/p1669287139450499

Found quite a few things on the way and fixed them. See inline comments.

Before:

Screenshot 2022-11-25 at 21 07 57@2x

After:

Screenshot 2022-11-25 at 21 09 41@2x

Test plan

Added and adjusted test suites.

@cla-bot cla-bot Bot added the cla-signed label Nov 25, 2022
Comment thread cmd/frontend/graphqlbackend/preview_repository_comparison_test.go Outdated
@eseliger eseliger changed the title Es/diff fixes Various fixes for diffs and file diff rendering Nov 25, 2022
@sg-e2e-regression-test-bob

sg-e2e-regression-test-bob commented Nov 25, 2022

Copy link
Copy Markdown

Bundle size report 📦

Initial size Total size Async size Modules
0.00% (0.00 kb) 0.00% (+0.25 kb) 0.00% (+0.25 kb) 0.00% (0)

Look at the Statoscope report for a full comparison between the commits cb21493 and ca2c501 or learn more.

Open explanation
  • Initial size is the size of the initial bundle (the one that is loaded when you open the page)
  • Total size is the size of the initial bundle + all the async loaded chunks
  • Async size is the size of all the async loaded chunks
  • Modules is the number of modules in the initial bundle

Comment on lines +3732 to +3735
"""
Specifies which format/highlighting technique to use.
"""
format: HighlightResponseFormat = HTML_HIGHLIGHT

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument is usually allowed for highlighting but was missing for diff hunks.
This can be used to do a fast-pass render without highlighting first for faster diff loads.

Comment on lines -880 to -881
// This is unparseable by go-diff. Once it isn't anymore, the test should fail, reminding
// us of the TODO comment in repository_comparison to reenable it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +801 to +804
{kind: "UNCHANGED", html: "func AddFeature(key string, isEnabled bool) {"},
{kind: "UNCHANGED", html: "features[strings.ToLower(key)] = isEnabled"},
{kind: "UNCHANGED", html: "}"},
{kind: "UNCHANGED", html: ""},
{kind: "DELETED", html: ""},

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what was going on here before, but that assertion was very broken.

"func IsEnabled(key string) bool {",
"return features[strings.ToLower(key)]",
"}",
"",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

"func AddFeature(key string, isEnabled bool) {",
"features[strings.ToLower(key)] = isEnabled",
"}",
"",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

"## Installation",
"Wowza!",
"See [the main README](https://github.com/dominikh/go-tools#installation) for installation instructions.",
"",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

"## Installation",
"",
"See [the main README](https://github.com/dominikh/go-tools#installation) for installation instructions.",
"",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

"<div><span class=\"hl-text hl-html hl-markdown\">\n</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">\n</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">(c) Copyright Sourcegraph 2013-2021.</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">\n</span></div>",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

"<div><span class=\"hl-text hl-html hl-markdown\">\n</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">Detailed documentation can be found on\n</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">[staticcheck.io](https://staticcheck.io/docs/).\n</span></div>",
"<div><span class=\"hl-text hl-html hl-markdown\">\n</span></div>",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pass KeepFinalNewline to the highlighter, a newline at the end is expected.

Comment on lines +599 to +605
type highlightedDiffHunkLineKind int

const (
highlightedDiffHunkLineKindUnchanged highlightedDiffHunkLineKind = iota
highlightedDiffHunkLineKindAdded
highlightedDiffHunkLineKindDeleted
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this int so we don't have to hoard so much memory for larger files.


const viewFilesCommitElement = node.tree && (
<div className="d-flex justify-content-between">
<div className="d-flex justify-content-between align-items-start">

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The button was skewed

size={small ? 'sm' : undefined}
variant="secondary"
outline={diffMode !== 'unified'}
className="mb-0"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes a spacing issue

size={small ? 'sm' : undefined}
variant="secondary"
outline={diffMode !== 'split'}
className="mb-0"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes a spacing issue

async event => {
event.preventDefault()

if (!state) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes that the button is not clickable again after an error

fragment ChangesetDiffFields on ExternalChangeset {
currentSpec {
description {
__typename

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The download patch button was broken because a typename assertion didn't hold. Sadly our type generator always includes typename but the backend doesn't 🙃

}

func applyPatch(fileContent string, fileDiff *diff.FileDiff) string {
// applyPatch takes the contents of a file and a file diff to apply to it. It

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed issues in this method around patches without newlines in either base or head.

paths: string[] | null
}): Observable<RepositoryComparisonDiff['comparison']['fileDiffs']> {
return queryGraphQL(
return requestGraphQL<RepositoryComparisonDiffResult, RepositoryComparisonDiffVariables>(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was deprecated, moved to new method.

@eseliger eseliger marked this pull request as ready for review November 25, 2022 20:20
@eseliger eseliger requested review from a team and mrnugget November 25, 2022 20:20
@sourcegraph-bot

sourcegraph-bot commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff a24d341...cb21493.

Notify File(s)
@courier-new client/web/src/enterprise/batches/detail/backend.ts
client/web/src/enterprise/batches/detail/changesets/DownloadDiffButton.tsx

@LawnGnome LawnGnome left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big set of cleanups!

The applyPatch changes are the kind of thing that I generally find mind melting, so good job there in particular. I do have a question around test coverage, but nothing that would stop us from merging this.

// Note: Has no newline at the end.
wantFile: `No newline after this
Also no newline after this`,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testing coverage is great, so please don't hurt me for asking, but do you think we need test cases for some of those hunk-related special cases as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, will add one, thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait, I thought this was pointing to a different test - what exactly do you want to see covered here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few branches we're not really exercising right now. (Which, to be clear, I realise you inherited in some cases, rather than being new code, but this feels like a good opportunity to make our test coverage a bit more complete here.)

I think the one that caught my eye was adding the original lines if the last hunk isn't at the end of the file — I remember writing the equivalent code for the CVS importer and not quite getting the nuances right the first time, so based on a sample size of 1 case of my own incompetence, I think we might want a test or two to cover that. 😄

For the record, I just threw gobco at this and it spat out the following branches we're not exercising:

cmd/frontend/graphqlbackend/preview_repository_comparison.go:150:5: condition "diffPathOrNull(fileDiff.NewName) == nil" was once false but never true
cmd/frontend/graphqlbackend/preview_repository_comparison.go:161:5: condition "len(fileContent) > 0" was once true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:175:10: condition "i == len(fileDiff.Hunks)-1" was 3 times true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:181:6: condition "hunk.OrigStartLine != 0 && hunk.OrigStartLine != currentLine" was once true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:181:6: condition "hunk.OrigStartLine != 0" was once true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:181:33: condition "hunk.OrigStartLine != currentLine" was once true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:198:9: condition "strings.HasPrefix(line, \"+\")" was 3 times true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:199:8: condition "isLastHunk(i) && hunkHasFinalNewline" was 3 times true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:199:8: condition "isLastHunk(i)" was 3 times true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:199:25: condition "hunkHasFinalNewline" was 3 times true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:215:44: condition "origLines > 0 && origLines != currentLine-1" was once false but never true
cmd/frontend/graphqlbackend/preview_repository_comparison.go:215:44: condition "origLines > 0" was once true but never false
cmd/frontend/graphqlbackend/preview_repository_comparison.go:215:61: condition "origLines != currentLine-1" was once false but never true
cmd/frontend/graphqlbackend/preview_repository_comparison.go:218:6: condition "origHasFinalNewline" was never evaluated
cmd/frontend/graphqlbackend/preview_repository_comparison.go:231:5: condition "lastHunkHadNewlineInLastAddition" was once true but never false

To be clear, I do not think we should add tests for all of these. For example, the first one probably has no value in being tested. But I do think it's worth glancing over each of these and ensuring we're comfortable not having test coverage for them, or adding a test as needed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, thanks for the detailed outline (and showing gobco, very cool), I'll address that :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All covered now :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good. 😃

@eseliger

Copy link
Copy Markdown
Member Author

The applyPatch changes are the kind of thing that I generally find mind melting, so good job there in particular

Trust me, that was a hard one 😀

// Note: Has no newline at the end.
wantFile: `No newline after this
Also no newline after this`,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good. 😃

@eseliger eseliger enabled auto-merge (squash) November 28, 2022 19:12
@eseliger eseliger merged commit 6cb6e19 into main Nov 28, 2022
@eseliger eseliger deleted the es/diff-fixes branch November 28, 2022 19:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants