豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 9812a32

Browse files
authored
Sessions - add action to discard changes (#4781)
1 parent 675f3ca commit 9812a32

File tree

10 files changed

+95
-10
lines changed

10 files changed

+95
-10
lines changed

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,13 @@
28332833
"icon": "$(git-commit)",
28342834
"category": "GitHub Copilot"
28352835
},
2836+
{
2837+
"command": "github.copilot.sessions.discardChanges",
2838+
"title": "%github.copilot.command.sessions.discardChanges%",
2839+
"enablement": "!chatSessionRequestInProgress",
2840+
"icon": "$(discard)",
2841+
"category": "GitHub Copilot"
2842+
},
28362843
{
28372844
"command": "github.copilot.chat.createPullRequestCopilotCLIAgentSession.createPR",
28382845
"title": "%github.copilot.chat.createPullRequestCopilotCLIAgentSession.createPR%",
@@ -5246,6 +5253,10 @@
52465253
"command": "github.copilot.sessions.commitChanges",
52475254
"when": "false"
52485255
},
5256+
{
5257+
"command": "github.copilot.sessions.discardChanges",
5258+
"when": "false"
5259+
},
52495260
{
52505261
"command": "github.copilot.sessions.refreshChanges",
52515262
"when": "false"
@@ -5624,6 +5635,12 @@
56245635
"when": "chatSessionType == copilotcli && isSessionsWindow",
56255636
"group": "9_refresh@1"
56265637
}
5638+
],
5639+
"chat/input/editing/sessionChangeToolbar": [
5640+
{
5641+
"command": "github.copilot.sessions.discardChanges",
5642+
"group": "navigation@1"
5643+
}
56275644
]
56285645
},
56295646
"icons": {

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,5 +484,6 @@
484484
"github.copilot.session.providerDescription.cloud": "Delegate tasks to the GitHub Copilot coding agent. The agent iterates via chat and works asynchronously in the cloud to implement changes and pull requests as needed.",
485485
"github.copilot.session.providerDescription.background": "Delegate tasks to a background agent running locally on your machine. The agent iterates via chat and works asynchronously in a Git worktree to implement changes isolated from your main workspace using the GitHub Copilot CLI.",
486486
"github.copilot.command.sessions.commitChanges": "Commit",
487+
"github.copilot.command.sessions.discardChanges": "Discard Changes",
487488
"github.copilot.command.sessions.refreshChanges": "Refresh"
488489
}

src/extension/chatSessions/vscode-node/copilotCLIChatSessions.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,15 +2266,22 @@ export function registerCLIChatCommands(
22662266

22672267
const sessionId = SessionIdForCLI.parse(resource);
22682268
const worktreeProperties = await copilotCLIWorktreeManagerService.getWorktreeProperties(sessionId);
2269+
const workspaceFolder = await copilotCliWorkspaceSession.getSessionWorkspaceFolder(sessionId);
22692270

2270-
if (!worktreeProperties) {
2271+
if (!worktreeProperties && !workspaceFolder) {
22712272
return;
22722273
}
22732274

2274-
await copilotCLIWorktreeManagerService.setWorktreeProperties(sessionId, {
2275-
...worktreeProperties,
2276-
changes: undefined
2277-
});
2275+
if (worktreeProperties) {
2276+
// Worktree
2277+
await copilotCLIWorktreeManagerService.setWorktreeProperties(sessionId, {
2278+
...worktreeProperties,
2279+
changes: undefined
2280+
});
2281+
} else if (workspaceFolder) {
2282+
// Workspace
2283+
copilotCliWorkspaceSession.clearWorkspaceChanges(workspaceFolder);
2284+
}
22782285

22792286
await contentProvider.refreshSession({ reason: 'update', sessionId });
22802287
}));
@@ -2294,6 +2301,24 @@ export function registerCLIChatCommands(
22942301
});
22952302
}));
22962303

2304+
disposableStore.add(vscode.commands.registerCommand('github.copilot.sessions.discardChanges', async (sessionResource: vscode.Uri, ref: string, ...resources: vscode.Uri[]) => {
2305+
if (!isUri(sessionResource) || resources.some(r => !isUri(r))) {
2306+
return;
2307+
}
2308+
2309+
const sessionId = SessionIdForCLI.parse(sessionResource);
2310+
const worktreeProperties = await copilotCLIWorktreeManagerService.getWorktreeProperties(sessionId);
2311+
const workspaceFolder = await copilotCliWorkspaceSession.getSessionWorkspaceFolder(sessionId);
2312+
2313+
const repositoryUri = worktreeProperties ? Uri.file(worktreeProperties.worktreePath) : workspaceFolder;
2314+
const repository = repositoryUri ? await gitService.getRepository(repositoryUri) : undefined;
2315+
if (!repository) {
2316+
return;
2317+
}
2318+
2319+
await gitService.restore(repository.rootUri, resources.map(r => r.fsPath), { ref });
2320+
}));
2321+
22972322
disposableStore.add(vscode.commands.registerCommand('github.copilot.chat.createPullRequestCopilotCLIAgentSession.createPR', async (sessionItemOrResource?: vscode.ChatSessionItem | vscode.Uri) => {
22982323
const resource = sessionItemOrResource instanceof vscode.Uri
22992324
? sessionItemOrResource

src/extension/chatSessions/vscode-node/copilotCLIChatSessionsContribution.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,15 +2319,22 @@ export function registerCLIChatCommands(
23192319

23202320
const sessionId = SessionIdForCLI.parse(resource);
23212321
const worktreeProperties = await copilotCLIWorktreeManagerService.getWorktreeProperties(sessionId);
2322+
const workspaceFolder = await copilotCliWorkspaceSession.getSessionWorkspaceFolder(sessionId);
23222323

2323-
if (!worktreeProperties) {
2324+
if (!worktreeProperties && !workspaceFolder) {
23242325
return;
23252326
}
23262327

2327-
await copilotCLIWorktreeManagerService.setWorktreeProperties(sessionId, {
2328-
...worktreeProperties,
2329-
changes: undefined
2330-
});
2328+
if (worktreeProperties) {
2329+
// Worktree
2330+
await copilotCLIWorktreeManagerService.setWorktreeProperties(sessionId, {
2331+
...worktreeProperties,
2332+
changes: undefined
2333+
});
2334+
} else if (workspaceFolder) {
2335+
// Workspace
2336+
copilotCliWorkspaceSession.clearWorkspaceChanges(workspaceFolder);
2337+
}
23312338

23322339
copilotcliSessionItemProvider.notifySessionsChange();
23332340
}));
@@ -2347,6 +2354,24 @@ export function registerCLIChatCommands(
23472354
});
23482355
}));
23492356

2357+
disposableStore.add(vscode.commands.registerCommand('github.copilot.sessions.discardChanges', async (sessionResource: vscode.Uri, ref: string, ...resources: vscode.Uri[]) => {
2358+
if (!isUri(sessionResource) || resources.some(r => !isUri(r))) {
2359+
return;
2360+
}
2361+
2362+
const sessionId = SessionIdForCLI.parse(sessionResource);
2363+
const worktreeProperties = await copilotCLIWorktreeManagerService.getWorktreeProperties(sessionId);
2364+
const workspaceFolder = await copilotCliWorkspaceSession.getSessionWorkspaceFolder(sessionId);
2365+
2366+
const repositoryUri = worktreeProperties ? Uri.file(worktreeProperties.worktreePath) : workspaceFolder;
2367+
const repository = repositoryUri ? await gitService.getRepository(repositoryUri) : undefined;
2368+
if (!repository) {
2369+
return;
2370+
}
2371+
2372+
await gitService.restore(repository.rootUri, resources.map(r => r.fsPath), { ref });
2373+
}));
2374+
23502375
disposableStore.add(vscode.commands.registerCommand('github.copilot.chat.createPullRequestCopilotCLIAgentSession.createPR', async (sessionItemOrResource?: vscode.ChatSessionItem | vscode.Uri) => {
23512376
const resource = sessionItemOrResource instanceof vscode.Uri
23522377
? sessionItemOrResource

src/extension/prompt/node/test/repoInfoTelemetry.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ suite('RepoInfoTelemetry', () => {
9494
diffIndexWithHEADShortStats: vi.fn(),
9595
fetch: vi.fn(),
9696
getMergeBase: vi.fn(),
97+
restore: vi.fn(),
9798
add: vi.fn(),
9899
createWorktree: vi.fn(),
99100
deleteWorktree: vi.fn(),

src/platform/git/common/gitService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface IGitService extends IDisposable {
6666
diffIndexWithHEADShortStats(uri: URI): Promise<CommitShortStat | undefined>;
6767
fetch(uri: URI, remote?: string, ref?: string, depth?: number): Promise<void>;
6868
getMergeBase(uri: URI, ref1: string, ref2: string): Promise<string | undefined>;
69+
restore(uri: URI, paths: string[], options?: { staged?: boolean; ref?: string }): Promise<void>;
6970

7071
createWorktree(uri: URI, options?: { path?: string; commitish?: string; branch?: string }): Promise<string | undefined>;
7172
deleteWorktree(uri: URI, path: string, options?: { force?: boolean }): Promise<void>;

src/platform/git/vscode-node/gitServiceImpl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ export class GitServiceImpl extends Disposable implements IGitService {
228228
await repository?.add(paths);
229229
}
230230

231+
async restore(uri: URI, paths: string[], options?: { staged?: boolean; ref?: string }): Promise<void> {
232+
const gitAPI = this.gitExtensionService.getExtensionApi();
233+
const repository = gitAPI?.getRepository(uri);
234+
await repository?.restore(paths, options);
235+
}
236+
231237
async log(uri: vscode.Uri, options?: LogOptions): Promise<Commit[] | undefined> {
232238
const gitAPI = this.gitExtensionService.getExtensionApi();
233239
if (!gitAPI) {

src/platform/git/vscode/git.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export interface Repository {
249249
add(paths: string[]): Promise<void>;
250250
revert(paths: string[]): Promise<void>;
251251
clean(paths: string[]): Promise<void>;
252+
restore(paths: string[], options?: { staged?: boolean; ref?: string }): Promise<void>;
252253

253254
apply(patch: string, reverse?: boolean): Promise<void>;
254255
diff(cached?: boolean): Promise<string>;

src/platform/ignore/node/test/mockGitService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class MockGitService implements IGitService {
7070
return Promise.resolve();
7171
}
7272

73+
restore(_uri: URI, _paths: string[], _options?: { staged?: boolean; ref?: string }): Promise<void> {
74+
return Promise.resolve();
75+
}
76+
7377
log(_uri: URI, _options?: LogOptions): Promise<Commit[] | undefined> {
7478
return Promise.resolve(undefined);
7579
}

src/platform/test/node/simulationWorkspaceServices.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ export class TestingGitService implements IGitService {
786786
return;
787787
}
788788

789+
async restore(_uri: URI, _paths: string[], _options?: { staged?: boolean; ref?: string }): Promise<void> {
790+
return;
791+
}
792+
789793
async createWorktree(uri: URI, options?: { path?: string; commitish?: string; branch?: string }): Promise<string | undefined> {
790794
return undefined;
791795
}

0 commit comments

Comments
 (0)