提交 3a2c97c5 编写于 作者: J Joao Moreno

refactor autostash behaviour

上级 5c8c42e8
......@@ -546,11 +546,6 @@ export class Repository implements Disposable {
private isFreshRepository: boolean | undefined = undefined;
private disposables: Disposable[] = [];
private _trackedCount: number = 0;
public get trackedCount(): number {
return this._trackedCount;
}
constructor(
private readonly repository: BaseRepository,
globalState: Memento
......@@ -944,26 +939,17 @@ export class Repository implements Disposable {
}
async pullFrom(rebase?: boolean, remote?: string, branch?: string): Promise<void> {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const fetchOnPull = config.get<boolean>('fetchOnPull');
const autoStash = config.get<boolean>('autoStash');
await this.run(Operation.Pull, async () => {
const doStash = autoStash && this.trackedCount > 0;
if (doStash) {
await this.repository.createStash();
}
await this.maybeAutoStash(async () => {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const fetchOnPull = config.get<boolean>('fetchOnPull');
if (fetchOnPull) {
await this.repository.pull(rebase);
} else {
await this.repository.pull(rebase, remote, branch);
}
if (doStash) {
await this.repository.popStash();
}
if (fetchOnPull) {
await this.repository.pull(rebase);
} else {
await this.repository.pull(rebase, remote, branch);
}
});
});
}
......@@ -1009,38 +995,29 @@ export class Repository implements Disposable {
pushBranch = `${head.name}:${head.upstream.name}`;
}
const config = workspace.getConfiguration('git', Uri.file(this.root));
const fetchOnPull = config.get<boolean>('fetchOnPull');
const autoStash = config.get<boolean>('autoStash');
await this.run(Operation.Sync, async () => {
const doStash = autoStash && this.trackedCount > 0;
if (doStash) {
await this.repository.createStash();
}
if (fetchOnPull) {
await this.repository.pull(rebase);
} else {
await this.repository.pull(rebase, remoteName, pullBranch);
}
await this.maybeAutoStash(async () => {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const fetchOnPull = config.get<boolean>('fetchOnPull');
if (doStash) {
await this.repository.popStash();
}
if (fetchOnPull) {
await this.repository.pull(rebase);
} else {
await this.repository.pull(rebase, remoteName, pullBranch);
}
const remote = this.remotes.find(r => r.name === remoteName);
const remote = this.remotes.find(r => r.name === remoteName);
if (remote && remote.isReadOnly) {
return;
}
if (remote && remote.isReadOnly) {
return;
}
const shouldPush = this.HEAD && (typeof this.HEAD.ahead === 'number' ? this.HEAD.ahead > 0 : true);
const shouldPush = this.HEAD && (typeof this.HEAD.ahead === 'number' ? this.HEAD.ahead > 0 : true);
if (shouldPush) {
await this.repository.push(remoteName, pushBranch);
}
if (shouldPush) {
await this.repository.push(remoteName, pushBranch);
}
});
});
}
......@@ -1354,20 +1331,17 @@ export class Repository implements Disposable {
this.indexGroup.resourceStates = index;
this.workingTreeGroup.resourceStates = workingTree;
let totalCount = merge.length + index.length + workingTree.length;
this._trackedCount = totalCount - workingTree.filter(r =>
r.type === Status.UNTRACKED ||
r.type === Status.IGNORED
).length;
// set count badge
const countBadge = workspace.getConfiguration('git').get<string>('countBadge');
let count = merge.length + index.length + workingTree.length;
switch (countBadge) {
case 'off': this._sourceControl.count = 0; break;
case 'all': this._sourceControl.count = totalCount; break;
case 'tracked': this._sourceControl.count = this._trackedCount; break;
case 'off': count = 0; break;
case 'tracked': count = count - workingTree.filter(r => r.type === Status.UNTRACKED || r.type === Status.IGNORED).length; break;
}
this._sourceControl.count = count;
// Disable `Discard All Changes` for "fresh" repositories
// https://github.com/Microsoft/vscode/issues/43066
const isFreshRepository = !this._HEAD || !this._HEAD.commit;
......@@ -1400,6 +1374,22 @@ export class Repository implements Disposable {
}
}
private async maybeAutoStash<T>(runOperation: () => Promise<T>): Promise<T> {
const config = workspace.getConfiguration('git', Uri.file(this.root));
const shouldAutoStash = config.get<boolean>('autoStash')
&& this.workingTreeGroup.resourceStates.some(r => r.type !== Status.UNTRACKED && r.type !== Status.IGNORED);
if (!shouldAutoStash) {
return await runOperation();
}
await this.repository.createStash(undefined, true);
const result = await runOperation();
await this.repository.popStash();
return result;
}
private onFSChange(_uri: Uri): void {
const config = workspace.getConfiguration('git');
const autorefresh = config.get<boolean>('autorefresh');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册