diff --git a/package-lock.json b/package-lock.json index 66934d7..084a258 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-differ", - "version": "0.2.3", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "git-differ", - "version": "0.2.3", + "version": "1.0.0", "license": "MIT", "devDependencies": { "@commitlint/cli": "^19.5.0", diff --git a/package.json b/package.json index 8a74624..937ce3e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,11 @@ "category": "Git", "command": "git-differ.compareWithCommit", "title": "Git Differ: Compare with Commit" + }, + { + "category": "Git", + "command": "git-differ.compareWithMaster", + "title": "Git Differ: Compare with Master" } ], "configuration": { @@ -43,6 +48,11 @@ "command": "git-differ.compareWithCommit", "group": "Git@15", "when": "!explorerResourceIsFolder" + }, + { + "command": "git-differ.compareWithMaster", + "group": "Git@15", + "when": "!explorerResourceIsFolder" } ], "editor/title/context": [ @@ -55,6 +65,11 @@ "command": "git-differ.compareWithCommit", "group": "Git@15", "when": "true" + }, + { + "command": "git-differ.compareWithMaster", + "group": "Git@15", + "when": "true" } ] } diff --git a/src/cmd/compareWithBranch.ts b/src/cmd/compareWithBranch.ts index ffc8c22..a83a428 100644 --- a/src/cmd/compareWithBranch.ts +++ b/src/cmd/compareWithBranch.ts @@ -9,6 +9,39 @@ type Branch = { remote?: string; }; +export async function executeDiffComparison( + gitApi: API, + uri: vscode.Uri, + branchName: string +): Promise { + const gitUri = gitApi.toGitUri(uri, branchName); + const filePath = uri.path.split("/").pop() || uri.path; + + await vscode.commands.executeCommand("vscode.diff", gitUri, uri, `${branchName} ↔ ${filePath}`); +} + +export async function findMasterBranch(gitApi: API, uri: vscode.Uri): Promise { + try { + const localBranches = await getBranches(gitApi, uri, false); + + let masterBranch = localBranches.find((branch) => branch.name === "master"); + if (!masterBranch) { + masterBranch = localBranches.find((branch) => branch.name === "main"); + } + + if (!masterBranch) { + const remoteBranches = await getBranches(gitApi, uri, true); + masterBranch = remoteBranches.find( + (branch) => branch.name === "origin/master" || branch.name === "origin/main" + ); + } + + return masterBranch || null; + } catch (error) { + return null; + } +} + export function newCompareWithBranch(gitApi: API): vscode.Disposable { return vscode.commands.registerCommand( "git-differ.compareWithBranch", @@ -47,15 +80,11 @@ export function newCompareWithBranch(gitApi: API): vscode.Disposable { return; } - const gitUri = gitApi.toGitUri(uri, branch.name); - const filePath = uri.path.split("/").pop() || uri.path; - - vscode.commands.executeCommand( - "vscode.diff", - gitUri, - uri, - `${branch.name} compared with "${filePath}"` - ); + try { + await executeDiffComparison(gitApi, uri, branch.name); + } catch (error) { + vscode.window.showErrorMessage(`An error occurred: ${error}`); + } } ); } diff --git a/src/cmd/compareWithMaster.ts b/src/cmd/compareWithMaster.ts new file mode 100644 index 0000000..a3f34c9 --- /dev/null +++ b/src/cmd/compareWithMaster.ts @@ -0,0 +1,31 @@ +import * as vscode from "vscode"; +import { API } from "../git"; +import { findMasterBranch, executeDiffComparison } from "./compareWithBranch"; + +export function newCompareWithMaster(gitApi: API): vscode.Disposable { + return vscode.commands.registerCommand( + "git-differ.compareWithMaster", + async (uri: vscode.Uri | undefined) => { + if (!uri) { + uri = vscode.window.activeTextEditor?.document.uri; + } + if (!uri) { + vscode.window.showErrorMessage("No file selected"); + return; + } + + try { + const masterBranch = await findMasterBranch(gitApi, uri); + + if (!masterBranch || !masterBranch.name) { + vscode.window.showErrorMessage("Could not find master or main branch"); + return; + } + + await executeDiffComparison(gitApi, uri, masterBranch.name); + } catch (error) { + vscode.window.showErrorMessage(`An error occurred: ${error}`); + } + } + ); +} diff --git a/src/extension.ts b/src/extension.ts index 52fa2ed..2c5f6e7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,7 @@ import * as vscode from "vscode"; import { newCompareWithBranch } from "./cmd/compareWithBranch"; import { newCompareWithCommit } from "./cmd/compareWithCommit"; +import { newCompareWithMaster } from "./cmd/compareWithMaster"; import { API, GitExtension } from "./git"; export function activate(context: vscode.ExtensionContext) { @@ -8,10 +9,10 @@ export function activate(context: vscode.ExtensionContext) { const gitApi: API = gitExtension.getAPI(1); const compareWithCommit = newCompareWithCommit(gitApi); - const compareWithBranch = newCompareWithBranch(gitApi); + const compareWithMaster = newCompareWithMaster(gitApi); - context.subscriptions.push(compareWithBranch, compareWithCommit); + context.subscriptions.push(compareWithBranch, compareWithCommit, compareWithMaster); } export function deactivate() {}