-
Notifications
You must be signed in to change notification settings - Fork 210
Cache for git remote #1882
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
Open
jrd
wants to merge
1
commit into
firecow:master
Choose a base branch
from
jrd:cache_for_git_remote
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+24
−18
Open
Cache for git remote #1882
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,9 +53,11 @@ type GitRemoteInfoContext = { | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export class ParserIncludes { | ||||||||||||||||||||||||||||||
| private static count: number = 0; | ||||||||||||||||||||||||||||||
| private static gitRemoteInfoCache: Record<string, string> = {}; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| static resetCount (): void { | ||||||||||||||||||||||||||||||
| this.count = 0; | ||||||||||||||||||||||||||||||
| this.gitRemoteInfoCache = {}; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private static normalizeTriggerInclude (gitlabData: any, opts: ParserIncludesInitOptions) { | ||||||||||||||||||||||||||||||
|
|
@@ -114,13 +116,13 @@ export class ParserIncludes { | |||||||||||||||||||||||||||||
| promises.push(this.downloadIncludeRemote(cwd, stateDir, value["remote"], fetchIncludes, writeStreams)); | ||||||||||||||||||||||||||||||
| } else if (value["component"]) { | ||||||||||||||||||||||||||||||
| const component = this.parseIncludeComponent(value["component"], gitData); | ||||||||||||||||||||||||||||||
| componentParseCache.set(index, component); | ||||||||||||||||||||||||||||||
| if (!component.isLocal) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| promises.push(this.downloadIncludeComponent(opts, component.projectPath, component.effectiveRef, component.componentPath)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| promises.push((async () => { | ||||||||||||||||||||||||||||||
| if (!component.isLocal) { | ||||||||||||||||||||||||||||||
| await this.downloadIncludeComponent(opts, component.projectPath, component.effectiveRef, component.componentPath); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| componentParseCache.set(index, component); | ||||||||||||||||||||||||||||||
| })()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| await Promise.all(promises); | ||||||||||||||||||||||||||||||
|
|
@@ -251,6 +253,20 @@ export class ParserIncludes { | |||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| static getGitRemoteInfo (ctx: GitRemoteInfoContext, ...args: string[]): string { | ||||||||||||||||||||||||||||||
| const cmdArgs = ["git", "ls-remote", ...args]; | ||||||||||||||||||||||||||||||
| if (ctx.gitData.remote.schema == "git" || ctx.gitData.remote.schema == "ssh") { | ||||||||||||||||||||||||||||||
| cmdArgs.push(`git@${ctx.domain}:${ctx.projectPath}`); | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| cmdArgs.push(`${ctx.gitData.remote.schema}://${ctx.domain}:${ctx.port ?? 443}/${ctx.projectPath}.git`); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| const cmdStr = cmdArgs.join(" "); | ||||||||||||||||||||||||||||||
| if (!(cmdStr in this.gitRemoteInfoCache)) { | ||||||||||||||||||||||||||||||
| this.gitRemoteInfoCache[cmdStr] = Utils.syncSpawn(cmdArgs).stdout; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return this.gitRemoteInfoCache[cmdStr]; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
Comment on lines
+263
to
+268
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| static parseIncludeComponent (component: string, gitData: GitData): ParsedComponent { | ||||||||||||||||||||||||||||||
| assert(!component.includes("://"), `This GitLab CI configuration is invalid: component: \`${component}\` should not contain protocol`); | ||||||||||||||||||||||||||||||
| const pattern = /(?<domain>[^/:\s]+)(:(?<port>\d+))?\/(?<projectPath>.+)\/(?<componentName>[^@]+)@(?<ref>.+)/; // https://regexr.com/7v7hm | ||||||||||||||||||||||||||||||
|
|
@@ -279,7 +295,7 @@ export class ParserIncludes { | |||||||||||||||||||||||||||||
| const semanticVersionRangesPattern = /^\d+(\.\d+)?$/; | ||||||||||||||||||||||||||||||
| if (this.reference == "~latest" || semanticVersionRangesPattern.test(this.reference)) { | ||||||||||||||||||||||||||||||
| // https://docs.gitlab.com/ci/components/#semantic-version-ranges | ||||||||||||||||||||||||||||||
| const stdout = getGitRemoteInfo(this, "--tags"); | ||||||||||||||||||||||||||||||
| const stdout = ParserIncludes.getGitRemoteInfo(this, "--tags"); | ||||||||||||||||||||||||||||||
| const tags = stdout.split("\n").map(line => line.split("\t")[1].split("/")[2]); | ||||||||||||||||||||||||||||||
|
jrd marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| const version = resolveSemanticVersionRange(this.reference, tags); | ||||||||||||||||||||||||||||||
| assert(version ?? tags.includes(this.reference), `This GitLab CI configuration is invalid: component: \`${this.name}\` - The reference (${this.reference}) is invalid`); | ||||||||||||||||||||||||||||||
|
|
@@ -305,7 +321,7 @@ export class ParserIncludes { | |||||||||||||||||||||||||||||
| // effectiveRef may already be a sha, if so return it directly | ||||||||||||||||||||||||||||||
| this._cache.sha = this.effectiveRef; | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| const stdout = getGitRemoteInfo(this); | ||||||||||||||||||||||||||||||
| const stdout = ParserIncludes.getGitRemoteInfo(this); | ||||||||||||||||||||||||||||||
| const lines = stdout.split("\n"); | ||||||||||||||||||||||||||||||
| // annotated tags: prefer the deref'd commit sha (refs/tags/x^{}) | ||||||||||||||||||||||||||||||
| const match = lines.find(line => line.endsWith(`refs/tags/${this.effectiveRef}^{}`)) ?? | ||||||||||||||||||||||||||||||
|
|
@@ -509,13 +525,3 @@ export async function resolveIncludeLocal (pattern: string, cwd: string) { | |||||||||||||||||||||||||||||
| const re2js = RE2JS.compile(`^${pattern}`); | ||||||||||||||||||||||||||||||
| return repoFiles.filter((f: any) => re2js.matches(f)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export function getGitRemoteInfo (ctx: GitRemoteInfoContext, ...args: string[]) { | ||||||||||||||||||||||||||||||
| const cmdArgs = ["git", "ls-remote", ...args]; | ||||||||||||||||||||||||||||||
| if (ctx.gitData.remote.schema == "git" || ctx.gitData.remote.schema == "ssh") { | ||||||||||||||||||||||||||||||
| cmdArgs.push(`git@${ctx.domain}:${ctx.projectPath}`); | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| cmdArgs.push(`${ctx.gitData.remote.schema}://${ctx.domain}:${ctx.port ?? 443}/${ctx.projectPath}.git`); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return Utils.syncSpawn(cmdArgs).stdout; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never cleared, though
resetCount()sits right beside it forcount. Stalegit ls-remoteresults could leak between runs in the test suite. Broke nothing I ran, so your call whether it matters for a single-shot CLI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I can reinit the remote info cache in the
resetCountmethod. That would be good enough I think.