Skip to content

Commit 4130136

Browse files
authored
Git - Stage/Unstage optimistic UI updates (microsoft#165716)
1 parent 348a658 commit 4130136

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

extensions/git/src/repository.ts

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,10 +1225,41 @@ export class Repository implements Disposable {
12251225
}
12261226

12271227
async add(resources: Uri[], opts?: { update?: boolean }): Promise<void> {
1228-
await this.run(Operation.Add, async () => {
1229-
await this.repository.add(resources.map(r => r.fsPath), opts);
1230-
this.closeDiffEditors([], [...resources.map(r => r.fsPath)]);
1231-
});
1228+
await this.run(
1229+
Operation.Add,
1230+
async () => {
1231+
await this.repository.add(resources.map(r => r.fsPath), opts);
1232+
this.closeDiffEditors([], [...resources.map(r => r.fsPath)]);
1233+
},
1234+
() => {
1235+
const resourcePaths = resources.map(r => r.fsPath);
1236+
const indexGroupResourcePaths = this.indexGroup.resourceStates.map(r => r.resourceUri.fsPath);
1237+
1238+
// Collect added resources
1239+
const addedResourceStates: Resource[] = [];
1240+
for (const resource of [...this.mergeGroup.resourceStates, ...this.untrackedGroup.resourceStates, ...this.workingTreeGroup.resourceStates]) {
1241+
if (resourcePaths.includes(resource.resourceUri.fsPath) && !indexGroupResourcePaths.includes(resource.resourceUri.fsPath)) {
1242+
addedResourceStates.push(resource);
1243+
}
1244+
}
1245+
1246+
// Add new resource(s) to index group
1247+
const indexGroup = [...this.indexGroup.resourceStates, ...addedResourceStates];
1248+
1249+
// Remove resource(s) from merge group
1250+
const mergeGroup = this.mergeGroup.resourceStates
1251+
.filter(r => !resourcePaths.includes(r.resourceUri.fsPath));
1252+
1253+
// Remove resource(s) from working group
1254+
const workingTreeGroup = this.workingTreeGroup.resourceStates
1255+
.filter(r => !resourcePaths.includes(r.resourceUri.fsPath));
1256+
1257+
// Remove resource(s) from untracked group
1258+
const untrackedGroup = this.untrackedGroup.resourceStates
1259+
.filter(r => !resourcePaths.includes(r.resourceUri.fsPath));
1260+
1261+
return { indexGroup, mergeGroup, workingTreeGroup, untrackedGroup };
1262+
});
12321263
}
12331264

12341265
async rm(resources: Uri[]): Promise<void> {
@@ -1245,12 +1276,43 @@ export class Repository implements Disposable {
12451276
}
12461277

12471278
async revert(resources: Uri[]): Promise<void> {
1248-
await this.run(Operation.RevertFiles, async () => {
1249-
await this.repository.revert('HEAD', resources.map(r => r.fsPath));
1250-
this.closeDiffEditors([...resources.length !== 0 ?
1251-
resources.map(r => r.fsPath) :
1252-
this.indexGroup.resourceStates.map(r => r.resourceUri.fsPath)], []);
1253-
});
1279+
await this.run(
1280+
Operation.RevertFiles,
1281+
async () => {
1282+
await this.repository.revert('HEAD', resources.map(r => r.fsPath));
1283+
this.closeDiffEditors([...resources.length !== 0 ?
1284+
resources.map(r => r.fsPath) :
1285+
this.indexGroup.resourceStates.map(r => r.resourceUri.fsPath)], []);
1286+
},
1287+
() => {
1288+
const resourcePaths = resources.length === 0 ?
1289+
this.indexGroup.resourceStates.map(r => r.resourceUri.fsPath) : resources.map(r => r.fsPath);
1290+
1291+
// Collect removed resources
1292+
const trackedResources: Resource[] = [];
1293+
const untrackedResources: Resource[] = [];
1294+
for (const resource of this.indexGroup.resourceStates) {
1295+
if (resourcePaths.includes(resource.resourceUri.fsPath)) {
1296+
if (resource.type === Status.INDEX_ADDED) {
1297+
untrackedResources.push(resource);
1298+
} else {
1299+
trackedResources.push(resource);
1300+
}
1301+
}
1302+
}
1303+
1304+
// Remove resource(s) from index group
1305+
const indexGroup = this.indexGroup.resourceStates
1306+
.filter(r => !resourcePaths.includes(r.resourceUri.fsPath));
1307+
1308+
// Add resource(s) to working group
1309+
const workingTreeGroup = [...this.workingTreeGroup.resourceStates, ...trackedResources];
1310+
1311+
// Add resource(s) to untracked group
1312+
const untrackedGroup = [...this.untrackedGroup.resourceStates, ...untrackedResources];
1313+
1314+
return { indexGroup, workingTreeGroup, untrackedGroup };
1315+
});
12541316
}
12551317

12561318
async commit(message: string | undefined, opts: CommitOptions = Object.create(null)): Promise<void> {

0 commit comments

Comments
 (0)