Skip to content

Commit ba4e806

Browse files
feat: Allow select files to commit when choose "changes" (close #472) (#811)
1 parent 43966da commit ba4e806

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Here are all of the extension settings with their default values. To change any
6969
// Whether auto refreshing is enabled
7070
"svn.autorefresh": true,
7171

72+
// Select all files when commit changes
73+
"svn.commit.changes.selectedAll": true,
74+
7275
// Set file to status resolved after fix conflicts
7376
"svn.conflicts.autoResolve": null,
7477

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,11 @@
10031003
"description": "Whether auto refreshing is enabled",
10041004
"default": true
10051005
},
1006+
"svn.commit.changes.selectedAll": {
1007+
"type": "boolean",
1008+
"description": "Select all files when commit changes",
1009+
"default": true
1010+
},
10061011
"svn.conflicts.autoResolve": {
10071012
"type": "boolean",
10081013
"description": "Set file to status resolved after fix conflicts",

src/changelistItems.ts

+31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import IgnoredChangeListItem from "./quickPickItems/ignoredChangeListItem";
55
import NewChangeListItem from "./quickPickItems/newChangeListItem";
66
import RemoveChangeListItem from "./quickPickItems/removeChangeListItem";
77
import { Repository } from "./repository";
8+
import { FileItem } from "./quickPickItems/fileItem";
89

910
export function getChangelistPickOptions(
1011
repository: Repository,
@@ -116,6 +117,36 @@ export async function inputCommitChangelist(repository: Repository) {
116117
return choice;
117118
}
118119

120+
export async function inputCommitFiles(repository: Repository) {
121+
const choice = await inputCommitChangelist(repository);
122+
if (!choice) {
123+
return;
124+
}
125+
126+
if (
127+
choice.id === "changes" &&
128+
choice.resourceGroup.resourceStates.length > 1
129+
) {
130+
const selectedAll = configuration.get("commit.changes.selectedAll", true);
131+
132+
const picks = choice.resourceGroup.resourceStates.map(
133+
r => new FileItem(repository, r, selectedAll)
134+
);
135+
const selected = await window.showQuickPick(picks, {
136+
placeHolder: "Select files to commit",
137+
canPickMany: true
138+
});
139+
140+
if (selected !== undefined && selected.length > 0) {
141+
return selected.map(s => s.state);
142+
}
143+
144+
return;
145+
}
146+
147+
return choice.resourceGroup.resourceStates;
148+
}
149+
119150
export function patchChangelistOptions(repository: Repository) {
120151
const picks: QuickPickItem[] = [];
121152

src/commands/commitWithMessage.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import { window } from "vscode";
3-
import { inputCommitChangelist } from "../changelistItems";
3+
import { inputCommitFiles } from "../changelistItems";
44
import { Status } from "../common/types";
55
import { inputCommitMessage } from "../messages";
66
import { Repository } from "../repository";
@@ -13,12 +13,12 @@ export class CommitWithMessage extends Command {
1313
}
1414

1515
public async execute(repository: Repository) {
16-
const choice = await inputCommitChangelist(repository);
17-
if (!choice) {
16+
const resourceStates = await inputCommitFiles(repository);
17+
if (!resourceStates || resourceStates.length === 0) {
1818
return;
1919
}
2020

21-
const filePaths = choice.resourceGroup.resourceStates.map(state => {
21+
const filePaths = resourceStates.map(state => {
2222
return state.resourceUri.fsPath;
2323
});
2424

@@ -32,7 +32,7 @@ export class CommitWithMessage extends Command {
3232
}
3333

3434
// If files is renamed, the commit need previous file
35-
choice.resourceGroup.resourceStates.forEach(state => {
35+
resourceStates.forEach(state => {
3636
if (state instanceof Resource) {
3737
if (state.type === Status.ADDED && state.renameResourceUri) {
3838
filePaths.push(state.renameResourceUri.fsPath);

src/quickPickItems/changeListItem.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { QuickPickItem, SourceControlResourceGroup } from "vscode";
1+
import { QuickPickItem } from "vscode";
2+
import { ISvnResourceGroup } from "../common/types";
23

34
export default class ChangeListItem implements QuickPickItem {
4-
constructor(protected group: SourceControlResourceGroup) {}
5+
constructor(protected group: ISvnResourceGroup) {}
56

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

11+
get id(): string {
12+
return this.group.id;
13+
}
14+
1015
get description(): string {
1116
return this.group.label;
1217
}
13-
get resourceGroup(): SourceControlResourceGroup {
18+
get resourceGroup(): ISvnResourceGroup {
1419
return this.group;
1520
}
1621
}

src/quickPickItems/fileItem.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { QuickPickItem } from "vscode";
2+
import { Repository } from "../repository";
3+
import { Resource } from "../resource";
4+
5+
export class FileItem implements QuickPickItem {
6+
constructor(
7+
protected _repository: Repository,
8+
protected _state: Resource,
9+
public picked = false
10+
) {}
11+
12+
get label(): string {
13+
return this._repository.repository.removeAbsolutePath(
14+
this._state.resourceUri.fsPath
15+
);
16+
}
17+
18+
get description(): string {
19+
return this._state.resourceUri.fsPath;
20+
}
21+
get state(): Resource {
22+
return this._state;
23+
}
24+
}

src/test/commands.test.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ suite("Commands Tests", () => {
6161
assert.equal(repository.changes.resourceStates.length, 1);
6262
});
6363

64-
test("Commit File", async function() {
64+
test("Commit Single File", async function() {
6565
const repository = sourceControlManager.getRepository(
6666
checkoutDir
6767
) as Repository;
@@ -165,11 +165,33 @@ suite("Commands Tests", () => {
165165
assert.equal(repository.changes.resourceStates.length, 0);
166166
});
167167

168-
test("Commit File", async function() {
168+
test("Commit Multiple", async function() {
169+
const file1 = path.join(checkoutDir.fsPath, "file1.txt");
170+
fs.writeFileSync(file1, "test");
171+
await commands.executeCommand("svn.openFile", Uri.file(file1));
172+
173+
const file2 = path.join(checkoutDir.fsPath, "file2.txt");
174+
fs.writeFileSync(file2, "test");
175+
await commands.executeCommand("svn.openFile", Uri.file(file2));
176+
169177
const repository = sourceControlManager.getRepository(
170178
checkoutDir
171179
) as Repository;
172-
repository.inputBox.value = "First Commit";
180+
repository.inputBox.value = "Multiple Files Commit";
181+
182+
await commands.executeCommand("svn.refresh");
183+
await commands.executeCommand(
184+
"svn.add",
185+
repository.unversioned.resourceStates[0]
186+
);
187+
await commands.executeCommand("svn.refresh");
188+
await commands.executeCommand(
189+
"svn.add",
190+
repository.unversioned.resourceStates[0]
191+
);
192+
await commands.executeCommand("svn.refresh");
193+
194+
testUtil.overrideNextShowQuickPick(0);
173195

174196
await commands.executeCommand("svn.commitWithMessage");
175197
});

0 commit comments

Comments
 (0)