From 8bcf177236f407fca6529a74627f07c8fa942364 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 29 Jun 2020 17:51:36 -0400 Subject: [PATCH] Fixes issues with create/copy/rename/delete --- extensions/github-browser/src/changeStore.ts | 22 +++++++++++++------- extensions/github-browser/src/extension.ts | 10 ++++++++- extensions/github-browser/src/github/api.ts | 17 ++++++++------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/extensions/github-browser/src/changeStore.ts b/extensions/github-browser/src/changeStore.ts index de56a6c7024..641bf1524fd 100644 --- a/extensions/github-browser/src/changeStore.ts +++ b/extensions/github-browser/src/changeStore.ts @@ -5,7 +5,7 @@ 'use strict'; import { commands, Event, EventEmitter, FileStat, FileType, Memento, TextDocumentShowOptions, Uri, ViewColumn } from 'vscode'; -import { getRootUri, getRelativePath } from './extension'; +import { getRootUri, getRelativePath, isChild } from './extension'; import { sha1 } from './sha1'; const textDecoder = new TextDecoder(); @@ -191,21 +191,29 @@ export class ChangeStore implements IChangeStore, IWritableChangeStore { return entries; } + const folderPath = getRelativePath(rootUri, uri); + const operations = this.getChanges(rootUri); for (const operation of operations) { switch (operation.type) { case 'changed': continue; + case 'created': { - const file = getRelativePath(rootUri, operation.uri); - entries.push([file, FileType.File]); + const filePath = getRelativePath(rootUri, operation.uri); + if (isChild(folderPath, filePath)) { + entries.push([filePath, FileType.File]); + } break; } + case 'deleted': { - const file = getRelativePath(rootUri, operation.uri); - const index = entries.findIndex(([path]) => path === file); - if (index !== -1) { - entries.splice(index, 1); + const filePath = getRelativePath(rootUri, operation.uri); + if (isChild(folderPath, filePath)) { + const index = entries.findIndex(([path]) => path === filePath); + if (index !== -1) { + entries.splice(index, 1); + } } break; } diff --git a/extensions/github-browser/src/extension.ts b/extensions/github-browser/src/extension.ts index fae080cb735..63cb1441a48 100644 --- a/extensions/github-browser/src/extension.ts +++ b/extensions/github-browser/src/extension.ts @@ -48,13 +48,21 @@ export function activate(context: ExtensionContext) { } export function getRelativePath(rootUri: Uri, uri: Uri) { - return uri.fsPath.substr(rootUri.fsPath.length + 1); + return uri.path.substr(rootUri.path.length + 1); } export function getRootUri(uri: Uri) { return workspace.getWorkspaceFolder(uri)?.uri; } +export function isChild(folderPath: string, filePath: string) { + return isDescendent(folderPath, filePath) && filePath.substr(folderPath.length + (folderPath.endsWith('/') ? 0 : 1)).split('/').length === 1; +} + +export function isDescendent(folderPath: string, filePath: string) { + return folderPath.length === 0 || filePath.startsWith(folderPath.endsWith('/') ? folderPath : `${folderPath}/`); +} + // function openWorkspace(uri: Uri, name: string, location: 'currentWindow' | 'newWindow' | 'addToCurrentWorkspace') { // if (location === 'addToCurrentWorkspace') { // const count = (workspace.workspaceFolders && workspace.workspaceFolders.length) || 0; diff --git a/extensions/github-browser/src/github/api.ts b/extensions/github-browser/src/github/api.ts index 8deeca7d958..c90d874bfab 100644 --- a/extensions/github-browser/src/github/api.ts +++ b/extensions/github-browser/src/github/api.ts @@ -152,19 +152,21 @@ export class GitHubApi implements Disposable { updatedTree.push({ path: operation.path, mode: '100644', type: 'blob', content: operation.content }); break; - case 'changed': - const item = updatedTree.find(item => item.path === operation.path); - if (item !== undefined) { - updatedTree.push({ ...item, content: operation.content }); + case 'changed': { + const index = updatedTree.findIndex(item => item.path === operation.path); + if (index !== -1) { + const { path, mode, type } = updatedTree[index]; + updatedTree.splice(index, 1, { path: path, mode: mode, type: type, content: operation.content }); } break; - - case 'deleted': + } + case 'deleted': { const index = updatedTree.findIndex(item => item.path === operation.path); if (index !== -1) { updatedTree.splice(index, 1); } break; + } } } } else { @@ -179,7 +181,8 @@ export class GitHubApi implements Disposable { case 'changed': const item = treeResp.data.tree.find(item => item.path === operation.path) as GitCreateTreeParamsTree; if (item !== undefined) { - updatedTree.push({ ...item, content: operation.content }); + const { path, mode, type } = item; + updatedTree.push({ path: path, mode: mode, type: type, content: operation.content }); } break; }