Skip to content

Commit

Permalink
Adds repo watching when outside of the workspace
Browse files Browse the repository at this point in the history
When available via the built-in Git api
  • Loading branch information
eamodio committed Jan 18, 2021
1 parent 92792ab commit 4c573e7
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/git/models/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export class Repository implements Disposable {
readonly index: number;
readonly name: string;
readonly normalizedPath: string;
readonly supportsChangeEvents: boolean = true;

private _branch: Promise<GitBranch | undefined> | undefined;
private readonly _disposable: Disposable;
Expand All @@ -163,6 +162,7 @@ export class Repository implements Disposable {
private _providers: RemoteProviders | undefined;
private _remotes: Promise<GitRemote[]> | undefined;
private _remotesDisposable: Disposable | undefined;
private _repoWatcherDisposable: Disposable | undefined;
private _suspended: boolean;

constructor(
Expand All @@ -179,7 +179,7 @@ export class Repository implements Disposable {
const repoFolder = workspace.getWorkspaceFolder(GitUri.fromRepoPath(path));
if (repoFolder == null) {
// If it isn't within a workspace folder we can't get change events, see: https://github.com/Microsoft/vscode/issues/3025
this.supportsChangeEvents = false;
this._supportsChangeEvents = false;
this.formattedName = this.name = paths.basename(path);
} else {
this.formattedName = this.name = folder.name;
Expand Down Expand Up @@ -222,14 +222,18 @@ export class Repository implements Disposable {
);
this.onConfigurationChanged(configuration.initializingChangeEvent);

if (!this.supportsChangeEvents && Logger.willLog('debug')) {
Logger.debug(
`Repository[${this.name}(${
this.id
})] doesn't support file watching; path=${path}, workspaceFolders=${workspace.workspaceFolders
?.map(wf => wf.uri.fsPath)
.join('; ')}`,
);
if (!this.supportsChangeEvents) {
void this.tryWatchingForChangesViaBuiltInApi();

if (Logger.willLog('debug')) {
Logger.debug(
`Repository[${this.name}(${
this.id
})] doesn't support file watching; path=${path}, workspaceFolders=${workspace.workspaceFolders
?.map(wf => wf.uri.fsPath)
.join('; ')}`,
);
}
}
}

Expand All @@ -244,7 +248,13 @@ export class Repository implements Disposable {
// }

this._remotesDisposable?.dispose();
this._disposable?.dispose();
this._repoWatcherDisposable?.dispose();
this._disposable.dispose();
}

private _supportsChangeEvents: boolean = true;
get supportsChangeEvents(): boolean {
return this._supportsChangeEvents;
}

private _updatedAt: number = 0;
Expand Down Expand Up @@ -767,6 +777,7 @@ export class Repository implements Disposable {
@log()
async stashApply(stashName: string, options: { deleteAfter?: boolean } = {}) {
void (await Container.git.stashApply(this.path, stashName, options));

if (!this.supportsChangeEvents) {
this.fireChange(RepositoryChange.Stash);
}
Expand All @@ -776,6 +787,7 @@ export class Repository implements Disposable {
@log()
async stashDelete(stashName: string, ref?: string) {
void (await Container.git.stashDelete(this.path, stashName, ref));

if (!this.supportsChangeEvents) {
this.fireChange(RepositoryChange.Stash);
}
Expand All @@ -785,6 +797,7 @@ export class Repository implements Disposable {
@log()
async stashSave(message?: string, uris?: Uri[], options: { includeUntracked?: boolean; keepIndex?: boolean } = {}) {
void (await Container.git.stashSave(this.path, message, uris, options));

if (!this.supportsChangeEvents) {
this.fireChange(RepositoryChange.Stash);
}
Expand Down Expand Up @@ -870,10 +883,11 @@ export class Repository implements Disposable {
return { dispose: () => this.stopWatchingFileSystem() };
}

stopWatchingFileSystem() {
stopWatchingFileSystem(force: boolean = false) {
if (this._fsWatcherDisposable == null) return;
if (--this._fsWatchCounter > 0) return;
if (--this._fsWatchCounter > 0 && !force) return;

this._fsWatchCounter = 0;
this._fsWatcherDisposable.dispose();
this._fsWatcherDisposable = undefined;
}
Expand Down Expand Up @@ -988,8 +1002,28 @@ export class Repository implements Disposable {
private runTerminalCommand(command: string, ...args: string[]) {
const parsedArgs = args.map(arg => (arg.startsWith('#') ? `"${arg}"` : arg));
runGitCommandInTerminal(command, parsedArgs.join(' '), this.path, true);

if (!this.supportsChangeEvents) {
this.fireChange(RepositoryChange.Unknown);
setTimeout(() => this.fireChange(RepositoryChange.Unknown), 2500);
}
}

private async tryWatchingForChangesViaBuiltInApi() {
const repo = await GitService.getBuiltInGitRepository(this.path);
if (repo != null) {
const internalRepo = (repo as any)._repository;
if (internalRepo != null && 'onDidChangeRepository' in internalRepo) {
try {
this._repoWatcherDisposable = internalRepo.onDidChangeRepository((e: Uri | undefined) =>
this.onRepositoryChanged(e),
);
this._supportsChangeEvents = true;

if (Logger.willLog('debug')) {
Logger.debug(`Repository[${this.name}(${this.id})] is now using fallback file watching`);
}
} catch {}
}
}
}
}

0 comments on commit 4c573e7

Please sign in to comment.