Skip to content

Commit

Permalink
Fix mrcrowl#100: Add purge support
Browse files Browse the repository at this point in the history
  • Loading branch information
hdpoliveira committed Jun 27, 2020
1 parent 07c363f commit 04db67d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@
"title": "%command.pull%",
"category": "Hg"
},
{
"command": "hg.purge",
"title": "%command.purge%",
"category": "Hg",
"icon": "$(discard)"
},
{
"command": "hg.purgeFiles",
"title": "%command.purgeFiles%",
"category": "Hg",
"icon": "$(discard)"
},
{
"command": "hg.purgeAll",
"title": "%command.purgeAll%",
"category": "Hg"
},
{
"command": "hg.push",
"title": "%command.push%",
Expand Down Expand Up @@ -492,6 +509,18 @@
"command": "hg.pull",
"when": "config.hg.enabled && !hg.missing && hgOpenRepositoryCount != 0"
},
{
"command": "hg.purge",
"when": "false"
},
{
"command": "hg.purgeFiles",
"when": "config.hg.enabled && !hg.missing && hgOpenRepositoryCount != 0"
},
{
"command": "hg.purgeAll",
"when": "config.hg.enabled && !hg.missing && hgOpenRepositoryCount != 0"
},
{
"command": "hg.push",
"when": "config.hg.enabled && !hg.missing && hgOpenRepositoryCount != 0"
Expand Down Expand Up @@ -604,6 +633,11 @@
"group": "4_stage",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg"
},
{
"command": "hg.purgeAll",
"group": "4_stage",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg"
},
{
"command": "hg.mergeHeads",
"group": "5_merge",
Expand Down Expand Up @@ -663,6 +697,16 @@
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "1_modification"
},
{
"command": "hg.purgeFiles",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "1_modification"
},
{
"command": "hg.purgeFiles",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "inline1"
},
{
"command": "hg.addAll",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
Expand Down Expand Up @@ -745,11 +789,21 @@
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == working",
"group": "inline"
},
{
"command": "hg.purge",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "inline1"
},
{
"command": "hg.add",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "inline"
},
{
"command": "hg.purge",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
"group": "1_modification"
},
{
"command": "hg.add",
"when": "config.hg.enabled && !hg.missing && scmProvider == hg && scmResourceGroup == untracked",
Expand Down
3 changes: 3 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"command.update": "Update to...",
"command.branch": "Create Branch...",
"command.pull": "Pull",
"command.purge": "Purge",
"command.purgeFiles": "Purge All Untracked Files",
"command.purgeAll": "Purge All Untracked and Ignored Files",
"command.push": "Push",
"command.pushTo": "Push to...",
"command.add": "Add Files",
Expand Down
33 changes: 33 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,39 @@ export class CommandCenter {
}
}

@command('hg.purgeFiles', { repository: true })
async purgeFiles(repository: Repository): Promise<void> {
return await repository.purge();
}

@command('hg.purgeAll', { repository: true })
async purgeAll(repository: Repository): Promise<void> {
return await repository.purgeAll();
}

@command('hg.purge')
async purge(...resourceStates: SourceControlResourceState[]): Promise<void> {
if (resourceStates.length === 0) {
const resource = this.getSCMResource();

if (!resource) {
return;
}

resourceStates = [resource];
}

const scmResources = resourceStates
.filter(s => s instanceof Resource && s.resourceGroup instanceof UntrackedGroup) as Resource[];

if (!scmResources.length) {
return;
}

const resources = scmResources.map(r => r.resourceUri);
await this.runByRepository(resources, async (repository, uris) => repository.purge(...uris));
}

private async smartCommit(repository: Repository, getCommitMessage: () => Promise<string | undefined>, opts: CommitOptions = {}): Promise<boolean> {
// validate no conflicts
const numConflictResources = repository.conflictGroup.resources.length;
Expand Down
23 changes: 23 additions & 0 deletions src/hg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface UnshelveOptions {
keep?: boolean;
}

export interface PurgeOptions {
paths?: string[];
all?: boolean;
}

export interface IMergeResult {
unresolvedCount: number;
}
Expand Down Expand Up @@ -352,6 +357,7 @@ export const HgErrorCodes = {
CantAccessRemote: 'CantAccessRemote',
RepositoryNotFound: 'RepositoryNotFound',
NoSuchFile: 'NoSuchFile',
ExtensionMissing: 'ExtensionMissing',
BranchAlreadyExists: 'BranchAlreadyExists',
NoRollbackInformationAvailable: 'NoRollbackInformationAvailable',
UntrackedFilesDiffer: 'UntrackedFilesDiffer',
Expand Down Expand Up @@ -487,6 +493,10 @@ export class Hg {
else if (/no such file/.test(result.stderr)) {
hgErrorCode = HgErrorCodes.NoSuchFile;
}
else if (/unknown command/.test(result.stderr)) {
hgErrorCode = HgErrorCodes.ExtensionMissing;
result.stderr = result.stderr.replace(/.*(unknown command '.*?').*/s, '$1');
}

if (options.logErrors !== false && result.stderr) {
this.log(`${result.stderr}\n`);
Expand Down Expand Up @@ -883,6 +893,19 @@ export class Repository {
}
}

async purge(opts: PurgeOptions): Promise<void> {
const args = ['purge'];

if (opts.paths && opts.paths.length) {
args.push.apply(args, opts.paths);
}
if (opts.all) {
args.push('--all');
}

await this.run(args);
}

async unshelveAbort(): Promise<void> {
await this.run(['unshelve', '--abort']);
}
Expand Down
4 changes: 4 additions & 0 deletions src/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export namespace interaction {
message = localize('unshelve in progress', "There is already an unshelve operation in progress.");
options.modal = false;
break;
case HgErrorCodes.ExtensionMissing:
message = localize('extension missing', "Extension missing: {0}", err.stderr);
options.modal = false;
break;

default:
const hint = (err.stderr || err.message || String(err))
Expand Down
17 changes: 17 additions & 0 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,23 @@ export class Repository implements IDisposable {
});
}

@throttle
async purge(...uris: Uri[]): Promise<void> {
let resources: Resource[];
if (uris.length === 0) {
resources = this._groups.untracked.resources;
} else {
resources = this.mapResources(uris);
}
const relativePaths: string[] = resources.map(r => this.mapResourceToRepoRelativePath(r));
await this.run(Operation.Clean, () => this.repository.purge({paths: relativePaths}));
}

@throttle
async purgeAll(): Promise<void> {
await this.run(Operation.Clean, () => this.repository.purge({all: true}));
}

@throttle
async branch(name: string, opts?: { allowBranchReuse: boolean }): Promise<void> {
const hgOpts = opts && {
Expand Down

0 comments on commit 04db67d

Please sign in to comment.