Skip to content

Commit

Permalink
feat: Allow select files to commit when choose "changes" (close #472) (
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 19, 2020
1 parent 43966da commit ba4e806
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ Here are all of the extension settings with their default values. To change any
// Whether auto refreshing is enabled
"svn.autorefresh": true,

// Select all files when commit changes
"svn.commit.changes.selectedAll": true,

// Set file to status resolved after fix conflicts
"svn.conflicts.autoResolve": null,

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,11 @@
"description": "Whether auto refreshing is enabled",
"default": true
},
"svn.commit.changes.selectedAll": {
"type": "boolean",
"description": "Select all files when commit changes",
"default": true
},
"svn.conflicts.autoResolve": {
"type": "boolean",
"description": "Set file to status resolved after fix conflicts",
Expand Down
31 changes: 31 additions & 0 deletions src/changelistItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import IgnoredChangeListItem from "./quickPickItems/ignoredChangeListItem";
import NewChangeListItem from "./quickPickItems/newChangeListItem";
import RemoveChangeListItem from "./quickPickItems/removeChangeListItem";
import { Repository } from "./repository";
import { FileItem } from "./quickPickItems/fileItem";

export function getChangelistPickOptions(
repository: Repository,
Expand Down Expand Up @@ -116,6 +117,36 @@ export async function inputCommitChangelist(repository: Repository) {
return choice;
}

export async function inputCommitFiles(repository: Repository) {
const choice = await inputCommitChangelist(repository);
if (!choice) {
return;
}

if (
choice.id === "changes" &&
choice.resourceGroup.resourceStates.length > 1
) {
const selectedAll = configuration.get("commit.changes.selectedAll", true);

const picks = choice.resourceGroup.resourceStates.map(
r => new FileItem(repository, r, selectedAll)
);
const selected = await window.showQuickPick(picks, {
placeHolder: "Select files to commit",
canPickMany: true
});

if (selected !== undefined && selected.length > 0) {
return selected.map(s => s.state);
}

return;
}

return choice.resourceGroup.resourceStates;
}

export function patchChangelistOptions(repository: Repository) {
const picks: QuickPickItem[] = [];

Expand Down
10 changes: 5 additions & 5 deletions src/commands/commitWithMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "path";
import { window } from "vscode";
import { inputCommitChangelist } from "../changelistItems";
import { inputCommitFiles } from "../changelistItems";
import { Status } from "../common/types";
import { inputCommitMessage } from "../messages";
import { Repository } from "../repository";
Expand All @@ -13,12 +13,12 @@ export class CommitWithMessage extends Command {
}

public async execute(repository: Repository) {
const choice = await inputCommitChangelist(repository);
if (!choice) {
const resourceStates = await inputCommitFiles(repository);
if (!resourceStates || resourceStates.length === 0) {
return;
}

const filePaths = choice.resourceGroup.resourceStates.map(state => {
const filePaths = resourceStates.map(state => {
return state.resourceUri.fsPath;
});

Expand All @@ -32,7 +32,7 @@ export class CommitWithMessage extends Command {
}

// If files is renamed, the commit need previous file
choice.resourceGroup.resourceStates.forEach(state => {
resourceStates.forEach(state => {
if (state instanceof Resource) {
if (state.type === Status.ADDED && state.renameResourceUri) {
filePaths.push(state.renameResourceUri.fsPath);
Expand Down
11 changes: 8 additions & 3 deletions src/quickPickItems/changeListItem.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { QuickPickItem, SourceControlResourceGroup } from "vscode";
import { QuickPickItem } from "vscode";
import { ISvnResourceGroup } from "../common/types";

export default class ChangeListItem implements QuickPickItem {
constructor(protected group: SourceControlResourceGroup) {}
constructor(protected group: ISvnResourceGroup) {}

get label(): string {
return this.group.id.replace(/^changelist-/, "");
}

get id(): string {
return this.group.id;
}

get description(): string {
return this.group.label;
}
get resourceGroup(): SourceControlResourceGroup {
get resourceGroup(): ISvnResourceGroup {
return this.group;
}
}
24 changes: 24 additions & 0 deletions src/quickPickItems/fileItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { QuickPickItem } from "vscode";
import { Repository } from "../repository";
import { Resource } from "../resource";

export class FileItem implements QuickPickItem {
constructor(
protected _repository: Repository,
protected _state: Resource,
public picked = false
) {}

get label(): string {
return this._repository.repository.removeAbsolutePath(
this._state.resourceUri.fsPath
);
}

get description(): string {
return this._state.resourceUri.fsPath;
}
get state(): Resource {
return this._state;
}
}
28 changes: 25 additions & 3 deletions src/test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ suite("Commands Tests", () => {
assert.equal(repository.changes.resourceStates.length, 1);
});

test("Commit File", async function() {
test("Commit Single File", async function() {
const repository = sourceControlManager.getRepository(
checkoutDir
) as Repository;
Expand Down Expand Up @@ -165,11 +165,33 @@ suite("Commands Tests", () => {
assert.equal(repository.changes.resourceStates.length, 0);
});

test("Commit File", async function() {
test("Commit Multiple", async function() {
const file1 = path.join(checkoutDir.fsPath, "file1.txt");
fs.writeFileSync(file1, "test");
await commands.executeCommand("svn.openFile", Uri.file(file1));

const file2 = path.join(checkoutDir.fsPath, "file2.txt");
fs.writeFileSync(file2, "test");
await commands.executeCommand("svn.openFile", Uri.file(file2));

const repository = sourceControlManager.getRepository(
checkoutDir
) as Repository;
repository.inputBox.value = "First Commit";
repository.inputBox.value = "Multiple Files Commit";

await commands.executeCommand("svn.refresh");
await commands.executeCommand(
"svn.add",
repository.unversioned.resourceStates[0]
);
await commands.executeCommand("svn.refresh");
await commands.executeCommand(
"svn.add",
repository.unversioned.resourceStates[0]
);
await commands.executeCommand("svn.refresh");

testUtil.overrideNextShowQuickPick(0);

await commands.executeCommand("svn.commitWithMessage");
});
Expand Down

0 comments on commit ba4e806

Please sign in to comment.