mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-03 06:51:53 +01:00
Git - add extension API to get the repository root (#250044)
* Git - add extension API to get the repository root * Pull request feedback
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
import { Model } from '../model';
|
||||
import { Repository as BaseRepository, Resource } from '../repository';
|
||||
import { InputBox, Git, API, Repository, Remote, RepositoryState, Branch, ForcePushMode, Ref, Submodule, Commit, Change, RepositoryUIState, Status, LogOptions, APIState, CommitOptions, RefType, CredentialsProvider, BranchQuery, PushErrorHandler, PublishEvent, FetchOptions, RemoteSourceProvider, RemoteSourcePublisher, PostCommitCommandsProvider, RefQuery, BranchProtectionProvider, InitOptions, SourceControlHistoryItemDetailsProvider } from './git';
|
||||
import { InputBox, Git, API, Repository, Remote, RepositoryState, Branch, ForcePushMode, Ref, Submodule, Commit, Change, RepositoryUIState, Status, LogOptions, APIState, CommitOptions, RefType, CredentialsProvider, BranchQuery, PushErrorHandler, PublishEvent, FetchOptions, RemoteSourceProvider, RemoteSourcePublisher, PostCommitCommandsProvider, RefQuery, BranchProtectionProvider, InitOptions, SourceControlHistoryItemDetailsProvider, GitErrorCodes } from './git';
|
||||
import { Event, SourceControlInputBox, Uri, SourceControl, Disposable, commands, CancellationToken } from 'vscode';
|
||||
import { combinedDisposable, filterEvent, mapEvent } from '../util';
|
||||
import { toGitUri } from '../uri';
|
||||
@@ -371,6 +371,27 @@ export class ApiImpl implements API {
|
||||
return result ? new ApiRepository(result) : null;
|
||||
}
|
||||
|
||||
async getRepositoryRoot(uri: Uri): Promise<Uri | null> {
|
||||
const repository = this.getRepository(uri);
|
||||
if (repository) {
|
||||
return repository.rootUri;
|
||||
}
|
||||
|
||||
try {
|
||||
const root = await this.#model.git.getRepositoryRoot(uri.fsPath);
|
||||
return Uri.file(root);
|
||||
} catch (err) {
|
||||
if (
|
||||
err.gitErrorCode === GitErrorCodes.NotAGitRepository ||
|
||||
err.gitErrorCode === GitErrorCodes.NotASafeGitRepository
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async init(root: Uri, options?: InitOptions): Promise<Repository | null> {
|
||||
const path = root.fsPath;
|
||||
await this.#model.git.init(path, options);
|
||||
|
||||
2
extensions/git/src/api/git.d.ts
vendored
2
extensions/git/src/api/git.d.ts
vendored
@@ -364,6 +364,7 @@ export interface API {
|
||||
|
||||
toGitUri(uri: Uri, ref: string): Uri;
|
||||
getRepository(uri: Uri): Repository | null;
|
||||
getRepositoryRoot(uri: Uri): Promise<Uri | null>;
|
||||
init(root: Uri, options?: InitOptions): Promise<Repository | null>;
|
||||
openRepository(root: Uri): Promise<Repository | null>
|
||||
|
||||
@@ -402,6 +403,7 @@ export const enum GitErrorCodes {
|
||||
NoUserEmailConfigured = 'NoUserEmailConfigured',
|
||||
NoRemoteRepositorySpecified = 'NoRemoteRepositorySpecified',
|
||||
NotAGitRepository = 'NotAGitRepository',
|
||||
NotASafeGitRepository = 'NotASafeGitRepository',
|
||||
NotAtRepositoryRoot = 'NotAtRepositoryRoot',
|
||||
Conflict = 'Conflict',
|
||||
StashConflict = 'StashConflict',
|
||||
|
||||
@@ -342,6 +342,8 @@ function getGitErrorCode(stderr: string): string | undefined {
|
||||
return GitErrorCodes.InvalidBranchName;
|
||||
} else if (/Please,? commit your changes or stash them/.test(stderr)) {
|
||||
return GitErrorCodes.DirtyWorkTree;
|
||||
} else if (/detected dubious ownership in repository at/.test(stderr)) {
|
||||
return GitErrorCodes.NotASafeGitRepository;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user