Skip to content
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

Displaying a prompt when user clones repository that exists locally #59714

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions extensions/git/src/api/git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export interface RepositoryUIState {
readonly onDidChange: Event<void>;
}

export interface KnownRepository {
fetchUrl: string;
rootLocation: string;
lastUpdated: number;
}

export interface Repository {

readonly rootUri: Uri;
Expand Down
16 changes: 16 additions & 0 deletions extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ export class CommandCenter {
let defaultCloneDirectory = config.get<string>('defaultCloneDirectory') || os.homedir();
defaultCloneDirectory = defaultCloneDirectory.replace(/^~/, os.homedir());

const existingRepoLocation = await this.model.getRepositoryLocation(url);

if (existingRepoLocation) {
const open = localize('openrepo', "Open Repository");

const result = await window.showInformationMessage(
localize('repoExists', "This repository already exists would you like to open it instead?"),
open,
);
if (result === open) {
const exisitingUri = Uri.file(existingRepoLocation);
commands.executeCommand('vscode.openFolder', exisitingUri);
return;
}
}

const uris = await window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
Expand Down
12 changes: 11 additions & 1 deletion extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as path from 'path';
import * as fs from 'fs';
import * as nls from 'vscode-nls';
import { fromGitUri } from './uri';
import { GitErrorCodes } from './api/git';
import { GitErrorCodes, KnownRepository } from './api/git';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -336,6 +336,16 @@ export class Model {
return liveRepository && liveRepository.repository;
}

getRepositoryLocation(fetchUrl: string) {
const knownrepos = this.globalState.get<Array<KnownRepository>>('knownRepositories');
if (!knownrepos) { return false; }
const repoLocation = knownrepos.find(knownrepo => knownrepo.fetchUrl === fetchUrl);
if (repoLocation && fs.existsSync(repoLocation.rootLocation)) {
return repoLocation.rootLocation;
}
return false;
}

private getOpenRepository(repository: Repository): OpenRepository | undefined;
private getOpenRepository(sourceControl: SourceControl): OpenRepository | undefined;
private getOpenRepository(resourceGroup: SourceControlResourceGroup): OpenRepository | undefined;
Expand Down
37 changes: 35 additions & 2 deletions extensions/git/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as path from 'path';
import * as nls from 'vscode-nls';
import * as fs from 'fs';
import { StatusBarCommands } from './statusbar';
import { Branch, Ref, Remote, RefType, GitErrorCodes, Status } from './api/git';
import { Branch, Ref, Remote, RefType, GitErrorCodes, Status, KnownRepository } from './api/git';

const timeout = (millis: number) => new Promise(c => setTimeout(c, millis));

Expand Down Expand Up @@ -541,9 +541,11 @@ export class Repository implements Disposable {
private isFreshRepository: boolean | undefined = undefined;
private disposables: Disposable[] = [];

private readonly knownRepositoriesLimit = 50;

constructor(
private readonly repository: BaseRepository,
globalState: Memento
private globalState: Memento
) {
const fsWatcher = workspace.createFileSystemWatcher('**');
this.disposables.push(fsWatcher);
Expand Down Expand Up @@ -1282,6 +1284,8 @@ export class Repository implements Disposable {
this.indexGroup.resourceStates = index;
this.workingTreeGroup.resourceStates = workingTree;

this.updateKnownRepositories(remotes);

// set count badge
const countBadge = workspace.getConfiguration('git').get<string>('countBadge');
let count = merge.length + index.length + workingTree.length;
Expand All @@ -1305,6 +1309,35 @@ export class Repository implements Disposable {
this._onDidChangeStatus.fire();
}

private updateKnownRepositories(remotes: Remote[]) {
let knownRepositories = this.globalState.get<Array<KnownRepository>>('knownRepositories', []);

if (knownRepositories.length >= this.knownRepositoriesLimit) {
knownRepositories = knownRepositories
.sort((a, b) => a.lastUpdated - b.lastUpdated)
.slice(0, this.knownRepositoriesLimit);
}

const knownFetchUrls = knownRepositories.map(kr => kr.fetchUrl);

for (let remote of remotes) {
if (!remote.fetchUrl) {
continue;
}
if (knownFetchUrls.includes(remote.fetchUrl)) {
let matchingRepoIndex = knownRepositories.findIndex(knownRepository => knownRepository.fetchUrl === remote.fetchUrl!);
knownRepositories[matchingRepoIndex].lastUpdated = Date.now();
continue;
}
knownRepositories.push({
fetchUrl: remote.fetchUrl,
lastUpdated: Date.now(),
rootLocation: this.repository.root
});
}
this.globalState.update('knownRepositories', knownRepositories);
}

private async getRebaseCommit(): Promise<Commit | undefined> {
const rebaseHeadPath = path.join(this.repository.root, '.git', 'REBASE_HEAD');
const rebaseApplyPath = path.join(this.repository.root, '.git', 'rebase-apply');
Expand Down