diff --git a/extensions/git/package.json b/extensions/git/package.json index fdfa3452de9..013d1375ea3 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -712,11 +712,6 @@ "command": "git.manageUnsafeRepositories", "title": "%command.manageUnsafeRepositories%", "category": "Git" - }, - { - "command": "git.openRepositoriesInParentFolders", - "title": "%command.openRepositoriesInParentFolders%", - "category": "Git" } ], "continueEditSession": [ @@ -1167,10 +1162,6 @@ { "command": "git.manageUnsafeRepositories", "when": "config.git.enabled && !git.missing && git.unsafeRepositoryCount != 0" - }, - { - "command": "git.openRepositoriesInParentFolders", - "when": "config.git.enabled && !git.missing && git.parentRepositoryCount != 0" } ], "scm/title": [ @@ -2683,22 +2674,6 @@ "experimental" ] }, - "git.openRepositoryInParentFolders": { - "type": "string", - "enum": [ - "always", - "never", - "prompt" - ], - "enumDescriptions": [ - "%config.openRepositoryInParentFolders.always%", - "%config.openRepositoryInParentFolders.never%", - "%config.openRepositoryInParentFolders.prompt%" - ], - "default": "prompt", - "markdownDescription": "%config.openRepositoryInParentFolders%", - "scope": "resource" - }, "git.publishBeforeContinueOn": { "type": "boolean", "default": true, @@ -2852,14 +2827,14 @@ { "view": "scm", "contents": "%view.workbench.scm.empty%", - "when": "config.git.enabled && !git.missing && workbenchState == empty && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0", + "when": "config.git.enabled && !git.missing && workbenchState == empty && git.unsafeRepositoryCount == 0", "enablement": "git.state == initialized", "group": "2_open@1" }, { "view": "scm", "contents": "%view.workbench.scm.emptyWorkspace%", - "when": "config.git.enabled && !git.missing && workbenchState == workspace && workspaceFolderCount == 0 && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0", + "when": "config.git.enabled && !git.missing && workbenchState == workspace && workspaceFolderCount == 0 && git.unsafeRepositoryCount == 0", "enablement": "git.state == initialized", "group": "2_open@1" }, @@ -2876,25 +2851,15 @@ { "view": "scm", "contents": "%view.workbench.scm.folder%", - "when": "config.git.enabled && !git.missing && git.state == initialized && workbenchState == folder && scmRepositoryCount == 0 && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0 && remoteName != 'codespaces'", + "when": "config.git.enabled && !git.missing && git.state == initialized && workbenchState == folder && scmRepositoryCount == 0 && git.unsafeRepositoryCount == 0 && remoteName != 'codespaces'", "group": "5_scm@1" }, { "view": "scm", "contents": "%view.workbench.scm.workspace%", - "when": "config.git.enabled && !git.missing && git.state == initialized && workbenchState == workspace && workspaceFolderCount != 0 && scmRepositoryCount == 0 && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0 && remoteName != 'codespaces'", + "when": "config.git.enabled && !git.missing && git.state == initialized && workbenchState == workspace && workspaceFolderCount != 0 && scmRepositoryCount == 0 && git.unsafeRepositoryCount == 0 && remoteName != 'codespaces'", "group": "5_scm@1" }, - { - "view": "scm", - "contents": "%view.workbench.scm.repositoryInParentFolders%", - "when": "config.git.enabled && !git.missing && git.state == initialized && git.parentRepositoryCount == 1" - }, - { - "view": "scm", - "contents": "%view.workbench.scm.repositoriesInParentFolders%", - "when": "config.git.enabled && !git.missing && git.state == initialized && git.parentRepositoryCount > 1" - }, { "view": "scm", "contents": "%view.workbench.scm.unsafeRepository%", @@ -2908,13 +2873,15 @@ { "view": "explorer", "contents": "%view.workbench.cloneRepository%", - "when": "config.git.enabled && git.state == initialized && scmRepositoryCount == 0", + "when": "config.git.enabled", + "enablement": "git.state == initialized", "group": "5_scm@1" }, { "view": "explorer", "contents": "%view.workbench.learnMore%", - "when": "config.git.enabled && git.state == initialized && scmRepositoryCount == 0", + "when": "config.git.enabled", + "enablement": "git.state == initialized", "group": "5_scm@10" } ] diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index 728ab7f2870..24acb32bac3 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -104,7 +104,6 @@ "command.timelineSelectForCompare": "Select for Compare", "command.timelineCompareWithSelected": "Compare with Selected", "command.manageUnsafeRepositories": "Manage Unsafe Repositories", - "command.openRepositoriesInParentFolders": "Open Repositories In Parent Folders", "command.api.getRepositories": "Get Repositories", "command.api.getRepositoryState": "Get Repository State", "command.api.getRemoteSources": "Get Remote Sources", @@ -243,10 +242,6 @@ "config.useIntegratedAskPass": "Controls whether GIT_ASKPASS should be overwritten to use the integrated version.", "config.mergeEditor": "Open the merge editor for files that are currently under conflict.", "config.optimisticUpdate": "Controls whether to optimistically update the state of the Source Control view after running git commands.", - "config.openRepositoryInParentFolders": "Control whether a repository in the parent folders of the workspace, open file(s) should be opened.", - "config.openRepositoryInParentFolders.always": "Always open a repository in the parent folders of the workspace, open file(s).", - "config.openRepositoryInParentFolders.never": "Never open a repository in the parent folders of the workspace, open file(s).", - "config.openRepositoryInParentFolders.prompt": "Prompt before opening a repository in the parent folders of the workspace, open file(s).", "config.publishBeforeContinueOn": "Controls whether to publish unpublished git state when using Continue Working On from a git repository.", "submenu.explorer": "Git", "submenu.commit": "Commit", @@ -339,24 +334,6 @@ "view.workbench.scm.scanWorkspaceForRepositories": { "message": "Scanning workspace for git repositories..." }, - "view.workbench.scm.repositoryInParentFolders": { - "message": "A git repository was found in one of the parent directories of the workspace, open file(s).\n[Open Repository](command:git.openRepositoriesInParentFolders)\nUse [this setting](command:workbench.action.openSettings?%5B%22git.openRepositoryInParentFolders%22%5D) to control how git repositories in parent directories of the workspace, open file(s) are opened. To learn more [read our docs](https://aka.ms/vscode-git-external-repository).", - "comment": [ - "{Locked='](command:git.openRepositoriesInParentFolders'}", - "{Locked='](command:workbench.action.openSettings?%5B%22git.openRepositoryInParentFolders%22%5D'}", - "Do not translate the 'command:*' part inside of the '(..)'. It is an internal command syntax for VS Code", - "Please make sure there is no space between the right bracket and left parenthesis: ]( this is an internal syntax for links" - ] - }, - "view.workbench.scm.repositoriesInParentFolders": { - "message": "Git repositories were found in one of the parent directories of the workspace, open file(s).\n[Open Repository](command:git.openRepositoriesInParentFolders)\nUse [this setting](command:workbench.action.openSettings?%5B%22git.openRepositoryInParentFolders%22%5D) to control how git repositories in parent directories of the workspace, open file(s) are opened. To learn more [read our docs](https://aka.ms/vscode-git-external-repository).", - "comment": [ - "{Locked='](command:git.openRepositoriesInParentFolders'}", - "{Locked='](command:workbench.action.openSettings?%5B%22git.openRepositoryInParentFolders%22%5D'}", - "Do not translate the 'command:*' part inside of the '(..)'. It is an internal command syntax for VS Code", - "Please make sure there is no space between the right bracket and left parenthesis: ]( this is an internal syntax for links" - ] - }, "view.workbench.scm.unsafeRepository": { "message": "The detected git repository is potentially unsafe as the folder is owned by someone other than the current user.\n[Manage Unsafe Repositories](command:git.manageUnsafeRepositories)\nTo learn more about unsafe repositories [read our docs](https://aka.ms/vscode-git-unsafe-repository).", "comment": [ diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 9fe7ff2b275..6c2877a4af8 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -191,7 +191,7 @@ class FetchAllRemotesItem implements QuickPickItem { } } -class RepositoryItem implements QuickPickItem { +class UnsafeRepositoryItem implements QuickPickItem { get label(): string { return `$(repo) ${this.path}`; } @@ -3335,38 +3335,6 @@ export class CommandCenter { repository.closeDiffEditors(undefined, undefined, true); } - @command('git.openRepositoriesInParentFolders') - async openRepositoriesInParentFolders(): Promise { - const parentRepositories: string[] = []; - - const title = l10n.t('Open Repositories In Parent Folders'); - const placeHolder = l10n.t('Pick a repository to open'); - - const allRepositoriesLabel = l10n.t('All Repositories'); - const allRepositoriesQuickPickItem: QuickPickItem = { label: allRepositoriesLabel }; - const repositoriesQuickPickItems: QuickPickItem[] = Array.from(this.model.parentRepositories.keys()).sort().map(r => new RepositoryItem(r)); - - const items = this.model.parentRepositories.size === 1 ? [...repositoriesQuickPickItems] : - [...repositoriesQuickPickItems, { label: '', kind: QuickPickItemKind.Separator }, allRepositoriesQuickPickItem]; - - const repositoryItem = await window.showQuickPick(items, { title, placeHolder }); - if (!repositoryItem) { - return; - } - - if (repositoryItem === allRepositoriesQuickPickItem) { - // All Repositories - parentRepositories.push(...this.model.parentRepositories.keys()); - } else { - // One Repository - parentRepositories.push((repositoryItem as RepositoryItem).path); - } - - for (const parentRepository of parentRepositories) { - await this.model.openParentRepository(parentRepository); - } - } - @command('git.manageUnsafeRepositories') async manageUnsafeRepositories(): Promise { const unsafeRepositories: string[] = []; @@ -3377,13 +3345,13 @@ export class CommandCenter { const allRepositoriesLabel = l10n.t('All Repositories'); const allRepositoriesQuickPickItem: QuickPickItem = { label: allRepositoriesLabel }; - const repositoriesQuickPickItems: QuickPickItem[] = Array.from(this.model.unsafeRepositories.keys()).sort().map(r => new RepositoryItem(r)); + const repositoriesQuickPickItems: QuickPickItem[] = Array.from(this.model.unsafeRepositories.keys()).sort().map(r => new UnsafeRepositoryItem(r)); quickpick.items = this.model.unsafeRepositories.size === 1 ? [...repositoriesQuickPickItems] : [...repositoriesQuickPickItems, { label: '', kind: QuickPickItemKind.Separator }, allRepositoriesQuickPickItem]; quickpick.show(); - const repositoryItem = await new Promise( + const repositoryItem = await new Promise( resolve => { quickpick.onDidAccept(() => resolve(quickpick.activeItems[0])); quickpick.onDidHide(() => resolve(undefined)); @@ -3399,7 +3367,7 @@ export class CommandCenter { unsafeRepositories.push(...this.model.unsafeRepositories.keys()); } else { // One Repository - unsafeRepositories.push((repositoryItem as RepositoryItem).path); + unsafeRepositories.push((repositoryItem as UnsafeRepositoryItem).path); } for (const unsafeRepository of unsafeRepositories) { diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 133877a09de..4e2878b4d48 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -34,13 +34,18 @@ class RepositoryPick implements QuickPickItem { constructor(public readonly repository: Repository, public readonly index: number) { } } -abstract class RepositoryMap extends Map { +/** + * Key - normalized path used in user interface + * Value - path extracted from the output of the `git status` command + * used when calling `git config --global --add safe.directory` + */ +class UnsafeRepositoryMap extends Map { constructor() { super(); this.updateContextKey(); } - override set(key: string, value: T): this { + override set(key: string, value: string): this { const result = super.set(key, value); this.updateContextKey(); @@ -54,30 +59,11 @@ abstract class RepositoryMap extends Map { return result; } - abstract updateContextKey(): void; -} - -/** - * Key - normalized path used in user interface - * Value - path extracted from the output of the `git status` command - * used when calling `git config --global --add safe.directory` - */ -class UnsafeRepositoryMap extends RepositoryMap { - updateContextKey(): void { + private updateContextKey(): void { commands.executeCommand('setContext', 'git.unsafeRepositoryCount', this.size); } } -/** - * Key - normalized path used in user interface - * Value - value indicating whether the repository should be opened - */ -class ParentRepositoryMap extends RepositoryMap { - updateContextKey(): void { - commands.executeCommand('setContext', 'git.parentRepositoryCount', this.size); - } -} - export interface ModelChangeEvent { repository: Repository; uri: Uri; @@ -152,18 +138,14 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand private _onDidChangePostCommitCommandsProviders = new EventEmitter(); readonly onDidChangePostCommitCommandsProviders = this._onDidChangePostCommitCommandsProviders.event; + private showRepoOnHomeDriveRootWarning = true; private pushErrorHandlers = new Set(); private _unsafeRepositories = new UnsafeRepositoryMap(); - get unsafeRepositories(): UnsafeRepositoryMap { + get unsafeRepositories(): Map { return this._unsafeRepositories; } - private _parentRepositories = new ParentRepositoryMap(); - get parentRepositories(): ParentRepositoryMap { - return this._parentRepositories; - } - private disposables: Disposable[] = []; constructor(readonly git: Git, private readonly askpass: Askpass, private globalState: Memento, private logger: LogOutputChannel, private telemetryReporter: TelemetryReporter) { @@ -186,7 +168,6 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand private async doInitialScan(): Promise { const config = workspace.getConfiguration('git'); const autoRepositoryDetection = config.get('autoRepositoryDetection'); - const parentRepositoryConfig = config.get<'always' | 'never' | 'prompt'>('openRepositoryInParentFolders', 'prompt'); const initialScanFn = () => Promise.all([ this.onDidChangeWorkspaceFolders({ added: workspace.workspaceFolders || [], removed: [] }), @@ -200,12 +181,8 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand await initialScanFn(); } - if (this._parentRepositories.size !== 0 && - parentRepositoryConfig === 'prompt') { - // Parent repositories notification - this.showParentRepositoryNotification(); - } else if (this._unsafeRepositories.size !== 0) { - // Unsafe repositories notification + // Unsafe repositories notification + if (this._unsafeRepositories.size !== 0) { this.showUnsafeRepositoryNotification(); } @@ -417,87 +394,65 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand } try { - const { repositoryRoot, unsafeRepositoryMatch } = await this.getRepositoryRoot(repoPath); + const rawRoot = await this.git.getRepositoryRoot(repoPath); + + // This can happen whenever `path` has the wrong case sensitivity in + // case insensitive file systems + // https://github.com/microsoft/vscode/issues/33498 + const repositoryRoot = Uri.file(rawRoot).fsPath; + this.logger.trace(`Repository root: ${repositoryRoot}`); if (this.getRepositoryExact(repositoryRoot)) { this.logger.trace(`Repository for path ${repositoryRoot} already exists`); return; } - if (this.shouldRepositoryBeIgnored(repositoryRoot)) { + if (this.shouldRepositoryBeIgnored(rawRoot)) { this.logger.trace(`Repository for path ${repositoryRoot} is ignored`); return; } - // Handle git repositories that are in parent folders - const isRepositoryOutsideWorkspace = (workspace.workspaceFolders ?? []) - .find(f => pathEquals(f.uri.fsPath, repositoryRoot) || isDescendant(f.uri.fsPath, repositoryRoot)) === undefined; - const parentRepositoryConfig = config.get<'always' | 'never' | 'prompt'>('openRepositoryInParentFolders', 'prompt'); + // On Window, opening a git repository from the root of the HOMEDRIVE poses a security risk. + // We will only a open git repository from the root of the HOMEDRIVE if the user explicitly + // opens the HOMEDRIVE as a folder. Only show the warning once during repository discovery. + if (process.platform === 'win32' && process.env.HOMEDRIVE && pathEquals(`${process.env.HOMEDRIVE}\\`, repositoryRoot)) { + const isRepoInWorkspaceFolders = (workspace.workspaceFolders ?? []).find(f => pathEquals(f.uri.fsPath, repositoryRoot))!!; - if (isRepositoryOutsideWorkspace && parentRepositoryConfig !== 'always' && this.globalState.get(`parentRepository:${repositoryRoot}`) !== true) { - this.logger.trace(`Repository in parent folder: ${repositoryRoot}`); - - if (!this._parentRepositories.has(repositoryRoot)) { - // Show a notification if the parent repository is opened after the initial scan - if (this.state === 'initialized' && parentRepositoryConfig === 'prompt') { - this.showParentRepositoryNotification(); + if (!isRepoInWorkspaceFolders) { + if (this.showRepoOnHomeDriveRootWarning) { + window.showWarningMessage(l10n.t('Unable to automatically open the git repository at "{0}". To open that git repository, open it directly as a folder in VS Code.', repositoryRoot)); + this.showRepoOnHomeDriveRootWarning = false; } - this._parentRepositories.set(repositoryRoot); + this.logger.trace(`Repository for path ${repositoryRoot} is on the root of the HOMEDRIVE`); + return; } - - return; } - // Handle unsafe repositories - if (unsafeRepositoryMatch && unsafeRepositoryMatch.length === 3) { - this.logger.trace(`Unsafe repository: ${repositoryRoot}`); - - // Show a notification if the unsafe repository is opened after the initial scan - if (this._state === 'initialized' && !this._unsafeRepositories.has(repositoryRoot)) { - this.showUnsafeRepositoryNotification(); - } - - this._unsafeRepositories.set(repositoryRoot, unsafeRepositoryMatch[2]); - - return; - } - - // Open repository const dotGit = await this.git.getRepositoryDotGit(repositoryRoot); const repository = new Repository(this.git.open(repositoryRoot, dotGit, this.logger), this, this, this, this.globalState, this.logger, this.telemetryReporter); this.open(repository); repository.status(); // do not await this, we want SCM to know about the repo asap - } catch (err) { - // noop - this.logger.trace(`Opening repository for path='${repoPath}' failed; ex=${err}`); - } - } - - async openParentRepository(repoPath: string): Promise { - // Mark the repository to be opened from the parent folders - this.globalState.update(`parentRepository:${repoPath}`, true); - - await this.openRepository(repoPath); - this.parentRepositories.delete(repoPath); - } - - private async getRepositoryRoot(repoPath: string): Promise<{ repositoryRoot: string; unsafeRepositoryMatch: RegExpMatchArray | null }> { - try { - const rawRoot = await this.git.getRepositoryRoot(repoPath); - - // This can happen whenever `path` has the wrong case sensitivity in case - // insensitive file systems https://github.com/microsoft/vscode/issues/33498 - return { repositoryRoot: Uri.file(rawRoot).fsPath, unsafeRepositoryMatch: null }; - } catch (err) { + } catch (ex) { // Handle unsafe repository - const unsafeRepositoryMatch = /^fatal: detected dubious ownership in repository at \'([^']+)\'[\s\S]*git config --global --add safe\.directory '?([^'\n]+)'?$/m.exec(err.stderr); - if (unsafeRepositoryMatch && unsafeRepositoryMatch.length === 3) { - return { repositoryRoot: path.normalize(unsafeRepositoryMatch[1]), unsafeRepositoryMatch }; + const match = /^fatal: detected dubious ownership in repository at \'([^']+)\'[\s\S]*git config --global --add safe\.directory '?([^'\n]+)'?$/m.exec(ex.stderr); + if (match && match.length === 3) { + const unsafeRepositoryPath = path.normalize(match[1]); + this.logger.trace(`Unsafe repository: ${unsafeRepositoryPath}`); + + // Show a notification if the unsafe repository is opened after the initial repository scan + if (this._state === 'initialized' && !this._unsafeRepositories.has(unsafeRepositoryPath)) { + this.showUnsafeRepositoryNotification(); + } + + this._unsafeRepositories.set(unsafeRepositoryPath, match[2]); + + return; } - throw err; + // noop + this.logger.trace(`Opening repository for path='${repoPath}' failed; ex=${ex}`); } } @@ -782,36 +737,6 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand return [...this.pushErrorHandlers]; } - private async showParentRepositoryNotification(): Promise { - const message = this.parentRepositories.size === 1 ? - workspace.workspaceFolders !== undefined ? - l10n.t('We found a git repository in one of the parent folders of this workspace. Would you like to open the repository?') : - l10n.t('We found a git repository in one of the parent folders of the open file(s). Would you like to open the repository?') : - workspace.workspaceFolders !== undefined ? - l10n.t('We found git repositories in one of the parent folders of this workspace. Would you like to open the repositories?') : - l10n.t('We found git repositories in one of the parent folders of the open file(s). Would you like to open the repositories?'); - - const yes = l10n.t('Yes'); - const always = l10n.t('Always'); - const never = l10n.t('Never'); - - const choice = await window.showWarningMessage(message, yes, always, never); - if (choice === yes) { - // Open Parent Repositories - commands.executeCommand('git.openRepositoriesInParentFolders'); - } else if (choice === always || choice === never) { - // Update setting - const config = workspace.getConfiguration('git'); - await config.update('openRepositoryInParentFolders', choice === always ? 'always' : 'never', true); - - if (choice === always) { - for (const parentRepository of [...this.parentRepositories.keys()]) { - await this.openParentRepository(parentRepository); - } - } - } - } - private async showUnsafeRepositoryNotification(): Promise { // If no repositories are open, we will use a welcome view to inform the user // that a potentially unsafe repository was found so we do not have to show diff --git a/extensions/github/package.json b/extensions/github/package.json index 018c28a1f7f..91d49c77e8a 100644 --- a/extensions/github/package.json +++ b/extensions/github/package.json @@ -80,16 +80,16 @@ ], "file/share": [ { - "command": "github.copyVscodeDevLinkFile", - "when": "github.hasGitHubRepo", - "group": "0_vscode@0" + "command": "github.copyVscodeDevLinkFile", + "when": "github.hasGitHubRepo", + "group": "0_vscode@0" } ], "editor/context/share": [ { - "command": "github.copyVscodeDevLink", - "when": "github.hasGitHubRepo && resourceScheme != untitled", - "group": "0_vscode@0" + "command": "github.copyVscodeDevLink", + "when": "github.hasGitHubRepo && resourceScheme != untitled", + "group": "0_vscode@0" } ] }, @@ -119,12 +119,12 @@ { "view": "scm", "contents": "%welcome.publishFolder%", - "when": "config.git.enabled && git.state == initialized && workbenchState == folder && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0" + "when": "config.git.enabled && git.state == initialized && workbenchState == folder && git.unsafeRepositoryCount == 0" }, { "view": "scm", "contents": "%welcome.publishWorkspaceFolder%", - "when": "config.git.enabled && git.state == initialized && workbenchState == workspace && workspaceFolderCount != 0 && git.parentRepositoryCount == 0 && git.unsafeRepositoryCount == 0" + "when": "config.git.enabled && git.state == initialized && workbenchState == workspace && workspaceFolderCount != 0 && git.unsafeRepositoryCount == 0" } ], "markdown.previewStyles": [