Repository navigation
feat(sync): add source descriptor storage #993
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 all commits
Commits
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
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,72 @@ | ||
| package local | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| syncdomain "github.com/launchdarkly/ldcli/internal/sync" | ||
| ) | ||
|
|
||
| // This file tells an authored source from a generated, vendored, or minified | ||
| // one, which sync does not edit. | ||
|
|
||
| // publicationProblem is the problem of a source that sync cannot write. | ||
| func publicationProblem(file, message, suggestion string, cause error) error { | ||
| return syncdomain.WrapProblem( | ||
| syncdomain.NewProblem(syncdomain.ErrorSourceNotWritable, message, suggestion).WithFile(file, 0, 0), | ||
| cause, | ||
| ) | ||
| } | ||
|
|
||
| // generatedSource reports whether a path or its content marks a file that a | ||
| // tool produced: a vendored or minified path, or a generated-code header. | ||
| func generatedSource(file string, data []byte) bool { | ||
| // A case-insensitive volume serves `Vendor/` and `.Min.js` too, as the | ||
| // managed-directory check allows for. | ||
| path := strings.ToLower(filepath.ToSlash(file)) | ||
| if strings.Contains("/"+path+"/", "/vendor/") || strings.HasSuffix(path, ".min.js") { | ||
| return true | ||
| } | ||
| prefix := strings.ToLower(string(data)) | ||
| if len(prefix) > 512 { | ||
| prefix = prefix[:512] | ||
| } | ||
| return strings.Contains(prefix, "code generated") && strings.Contains(prefix, "do not edit") | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // generatedTarget reports whether a source whose resolved path is target is | ||
| // generated, vendored, or minified. A link can point into such a tree, so the | ||
| // resolved path counts as well as the name in the descriptor, on every read. | ||
| func generatedTarget(repositoryRoot, file, target string, data []byte) (bool, error) { | ||
| // A root or a target that does not resolve hides whether the source is | ||
| // writable, so the check fails closed. | ||
| root, err := filepath.EvalSymlinks(repositoryRoot) | ||
| if err != nil { | ||
| return false, fmt.Errorf("resolve repository root: %w", err) | ||
| } | ||
| resolved, err := filepath.Rel(root, target) | ||
| if err != nil { | ||
| return false, fmt.Errorf("resolve source file %q: %w", file, err) | ||
| } | ||
| return generatedSource(file, data) || generatedSource(filepath.ToSlash(resolved), data), nil | ||
| } | ||
|
|
||
| // rejectGeneratedTarget reports a source that sync must write but that is | ||
| // generated, vendored, or minified. | ||
| func rejectGeneratedTarget(repositoryRoot, file, target string, data []byte) error { | ||
| generated, err := generatedTarget(repositoryRoot, file, target, data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if generated { | ||
| return generatedProblem(file) | ||
| } | ||
| return nil | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // generatedProblem is the problem of a source that sync must write but that | ||
| // a tool produced. | ||
| func generatedProblem(file string) error { | ||
| return publicationProblem(file, "source is generated, vendored, or minified", "Link the authored source file.", nil) | ||
| } | ||
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.
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.