Skip to content

Commit

Permalink
Git - Add "Checkout" action to graph
Browse files Browse the repository at this point in the history
  • Loading branch information
lszomoru committed Jan 12, 2025
1 parent a2ea514 commit 430ede3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
24 changes: 15 additions & 9 deletions extensions/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
21 changes: 18 additions & 3 deletions extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2545,13 +2545,28 @@ export class CommandCenter {
}

@command('git.checkoutRef', { repository: true })
async checkoutRef(repository: Repository, historyItem?: SourceControlHistoryItem, historyItemRefId?: string): Promise<boolean> {
async checkoutRef(repository: Repository, historyItem?: SourceControlHistoryItem, historyItemRefId?: string): Promise<void> {
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<boolean>('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 })
Expand Down

0 comments on commit 430ede3

Please sign in to comment.