From 430ede3a86c4e956666412d6f3947b84b2d3b1ea Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:23:32 +0100 Subject: [PATCH] Git - Add "Checkout" action to graph --- extensions/git/package.json | 24 +++++++++++++++--------- extensions/git/src/commands.ts | 21 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index 66e8dd4981d70..4c3ea11c81f3a 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -1997,24 +1997,24 @@ ], "scm/historyItem/context": [ { - "command": "git.createTag", + "command": "git.checkoutRefDetached", "when": "scmProvider == git", - "group": "1_create@1" + "group": "1_checkout@2" }, { - "command": "git.branch", + "command": "git.createTag", "when": "scmProvider == git", - "group": "1_create@2" + "group": "2_create@1" }, { - "command": "git.cherryPickRef", + "command": "git.branch", "when": "scmProvider == git", - "group": "2_modify@1" + "group": "2_create@2" }, { - "command": "git.checkoutRefDetached", + "command": "git.cherryPickRef", "when": "scmProvider == git", - "group": "2_modify@2" + "group": "3_modify@1" }, { "command": "git.copyCommitId", @@ -2027,7 +2027,13 @@ "group": "9_copy@2" } ], - "scm/historyItemRef/context": [], + "scm/historyItemRef/context": [ + { + "command": "git.checkoutRef", + "when": "scmProvider == git", + "group": "1_checkout@1" + } + ], "editor/title": [ { "command": "git.openFile", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index d3e852ceab863..46e3f116c21d2 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -2545,13 +2545,28 @@ export class CommandCenter { } @command('git.checkoutRef', { repository: true }) - async checkoutRef(repository: Repository, historyItem?: SourceControlHistoryItem, historyItemRefId?: string): Promise { + async checkoutRef(repository: Repository, historyItem?: SourceControlHistoryItem, historyItemRefId?: string): Promise { const historyItemRef = historyItem?.references?.find(r => r.id === historyItemRefId); if (!historyItemRef) { - return false; + return; } - return this._checkout(repository, { treeish: historyItemRefId }); + const config = workspace.getConfiguration('git', Uri.file(repository.root)); + const pullBeforeCheckout = config.get('pullBeforeCheckout', false) === true; + + // Branch, tag + if (historyItemRef.id.startsWith('refs/heads/') || historyItemRef.id.startsWith('refs/tags/')) { + await repository.checkout(historyItemRef.name, { pullBeforeCheckout }); + return; + } + + // Remote branch + const branches = await repository.findTrackingBranches(historyItemRef.name); + if (branches.length > 0) { + await repository.checkout(branches[0].name!, { pullBeforeCheckout }); + } else { + await repository.checkoutTracking(historyItemRef.name); + } } @command('git.checkoutRefDetached', { repository: true })